Navigation with the Nav Mesh Part 1: Setup and Basic Navigation

In this series of tutorials, we’ll go through setting up Unity’s built-in Nav Mesh, which enables pathfinding and navigation without the need for complex code.

Before you Begin

For Intermediate Unity Developers

This tutorial series is for intermediate Unity users. I won’t go into detail on Unity basics, and assume you know your way around Unity’s interface and core features. Please refer to the Pong tutorial for beginners if you are not yet familiar enough with Unity to follow this tutorial.

Unity Version

This project was created with Unity version 2017.3.1f1, which is the latest version at the time of writing. You should be OK with any Unity version that is not too much older or newer.

Download

A download link is at the bottom of this tutorial. It contains a project as it should be if all the instructions in this part of the tutorial are followed.

Navigation Basics

Unity comes with a great navigation system that can be set up quickly and easily, and gives your characters the ability to navigate a complex environment by pathfinding and avoiding obstacles.

Nav Mesh

Unity’s navigation solution is the Nav Mesh. The Nav Mesh is a map of the areas in your game where a character can walk. When you tell your character to walk to a position on the Nav Mesh, they will automatically find a path to that position (if possible) and move there. If the character can’t reach the target position, they will get as close as possible.

Nav Agent

For a character to use a Nav Mesh, they must have a Nav Mesh Agent component on their GameObject. This agent contains the capabilities needed for navigation on the Nav Mesh, and you call methods on this object to make the character move, and use the various settings to specify the precise behaviour you want.

Create a Basic Navigating Character

To start with, we will create a simple scene with a navigating character who moves from their starting position to a target position.

The Scene

If you want to skip the basic non-AI related stuff, download the starter project, which includes a pre-built scene with the basics already done for you so you can get straight into the good stuff.

The base project contains a single scene ‘MainScene’, with some ground, an Agent (who we will set up to navigate the world), and a target GameObject which we will use as the place the player will navigate towards.

Create the Base Project and Scene

If you would rather set up the base project yourself, here are the steps required (skip this part if you downloaded the base project, and simply open that project then continue to Add a Nav Mesh).

  1. Start a new 3D Unity project.
  2. Create a scene called ‘MainScene’.
  3. Add a large Plane object to act as the ground
  4. Add a small sphere or cube called ‘Agent’, and make sure it is above the ground. Place the Agent near one of the corners.
  5. Create another GameObject called ‘Target’, and give it any 3D shape, such as a cylinder or a cube, and place it far from the Agent object (we will be making the Agent navigate towards the Target).
  6. Add materials to the objects to separate the objects visually, and to make your scene more interesting.
  7. Setup the camera so that you can see the whole of the floor and have a good view of the Agent and Target objects.

Here is what my base scene looks like:

Add a Nav Mesh

A Nav Mesh is used differently to the typical Unity components. Instead of adding it to objects, you add objects to it. For this reason, there is a Navigation window that you must use to setup your Nav Mesh.

If you don’t already have the Navigation window visible, go to the Unity menu and select Window > Navigation:

This window has four tabs that present different options and customisations; these are:

  • Agents – customise the behaviour of AI characters (agents) using the mesh.
  • Areas – customise the different types of terrain (e.g. terrain that is slower to walk on or terrain that can’t be walked on).
  • Bake – this is where you apply all your settings and create the Nav Mesh.
  • Object – where you select which objects are included in your mesh, and some of their properties.

You will also see that there is a Scene Filter option. This allows you to hide objects in the scene Hierarchy while working on navigation. For example, if you choose the Mesh Renderers option, everything without a Mesh Renderer will be hidden in the Hierarchy, making it easier to find the objects you want to use for navigation. This will come in handy for complex scenes with lots of objects, but for this tutorial we don’t need to worry ourselves about this.

We won’t go into a lot of detail right now. For now, let’s just get something working.

In the Navigation window:

  1. Select the Object tab.
  2. Select the ground object in the Hierarchy to make it the active object.
  3. Check that the ground object is now selected in the Navigation window.
  4. Set the settings as below:
    1. Tick Navigation Static.
    2. Select Walkable in the Navigation Area drop-down box.
    3. Ignore Generate OffMesh Links for now (we’ll look at it later).

You’ve now told the Nav Mesh that you want the ground to be walkable, and that it is ‘navigation static’ (i.e. the Nav Mesh will ‘see’ it when determining the walkable areas). This is the most basic Nav Mesh setup you need.

Bake It

Although we’ve added the ground to the Nav Mesh and made it walkable and navigation static, we have not actually created the Nav Mesh. We need to ‘bake’ it. Baking is a process of doing something that is too complex or time-consuming to do at runtime during the development process. There is no need to create a navigation mesh during runtime, since the terrain doesn’t change much in a game (and small changes don’t require a complete rebuilding of the mesh). The same is done for complex lighting in many games.

  1. Select the Bake tab in the Navigation window.
  2. Click the Bake button.
  3. If you do not already have the Scene window open, open it to see your Nav Mesh.

In the Scene window, the Nav Mesh is presented as a blue overlay on the ground, which represents where Nav Mesh Agents are able to walk:

In our current project, it will just be a simple blue square, but this will change as we later add some complexity.

Add an Agent

A Nav Mesh is pointless without someone to walk around it. We will now turn the Agent object into a ‘Nav Mesh Agent’ who can navigate the Nav Mesh.

  1. Select the Agent object in the Hierarchy to make it the active object.
  2. Add a Nav Mesh Agent component to the object in the Inspector window.
  3. Set the Speed property to any number (between 5 and 10 would be ideal).

You’ll notice a lot of settings on the Nav Mesh Agent component. Some of these are quite obvious (e.g. Speed), and some are not quite so obvious. For now we’ll ignore these settings.

Script

We need some code to make the player navigate, but it’s probably not as much code as you think. The Nav Mesh Agent takes care of movement and navigation, and we only need to tell the agent where to walk to.

To keep things simple for now, we’ll set a static target for the player to move towards (the Target GameObject in our scene).

Now, create a new C# script called ‘Agent’. I won’t go into detail about the code, as most of it doesn’t directly have anything to do with navigation (and it’s pretty basic Unity code). The only line of code that is navigation specific is:

agent.SetDestination(target.position);

That line tells the agent where it should try to navigate to, and will trigger the agent to start moving if they are not already at the target.

Here is the full code for the Agent script:

using UnityEngine;
using UnityEngine.AI;

public class Agent : MonoBehaviour {
  [SerializeField] Transform target;
  NavMeshAgent agent;

  void Start()
  {
    // get a reference to the player's Nav Mesh Agent component
    agent = GetComponent<NavMeshAgent>();
    // set the agent's destination
    agent.SetDestination(target.position);
 }
}
  1. Save the script.
  2. Attach the script to the Agent GameObject.
  3. Drag the Target GameObject into the Agent’s Target field in the Inspector:

Yes, that’s all you need for now to get the player navigating towards the target!

Run the Scene

Run the scene and watch the player automatically move towards the target. You can try experimenting with some of the player’s settings, though some of them won’t have any effect on such a basic scene, as the navigation is going to always be in a straight line.

It’s perhaps not too impressive to see the character move in a straight line from start to finish, so in the next part, we’ll add some obstacles and see the pathfinding in action.

Download

Here is the download for the project as it should be at the end of this part of the tutorial. Download it if you get stuck or want to compare your project to mine.

1 thought on “Navigation with the Nav Mesh Part 1: Setup and Basic Navigation

Leave a Comment