Unity NavMesh

A. Russell
3 min readFeb 21, 2021

This is Unity’s navigation system. It provides a convenient way to convert your scene’s geometry into walkable surfaces that pieces can navigate.

There appears to be 4 typical components to the NavMesh system:

  • NavMesh — the walkable surface.
  • NavMesh Agent — a piece that navigates the NavMesh. It is a behavior that you add to pieces you want to navigate intelligently. The pieces will move around the NavMesh avoiding other NavMesh Agents and NavMesh Obstacles.
  • Off-Mesh Link — jump points! Another component that allows you to connect unconnected portions of the NavMesh. Conceptually these could represent jumping. Also you can connect the NavMeshes for different Scenes with Off-Mesh Links when using Additive Loading.
  • NavMesh Obstacle — a component that is added to pieces that the NavMesh Agents should avoid. These could potentially be things that are moved around during the simulation. So these are things that should not be part of the original baking of the NavMesh.

Setting up the NavMesh can be found in the Navigation Panel. To build it, insure the walkable areas are static and then bake it on the Bake tab.

Setting up the Agents is as easy as adding the NavMesh Agent behavior to the piece you want to move.

And then the whole point for the NavMesh is to have pieces move towards some position. So just grab your Vector3 and pass it to navMeshAgent.SetDestination(position);

NavMesh Agents.

Unity allows you to define multiple NavMeshAgent types. However, while Unity allows you to setup different agents, to actually use more than one you have to import an additional project.
https://github.com/Unity-Technologies/NavMeshComponents.

This would definitely be useful. You could set up two different types , like a human and a giant. The giant has a larger radius, height and step height than a regular human and so they would navigate the same NavMesh differently.

Navigation Areas.

The NavMesh uses the A* algorithm to calculate the shortest path. For A* to work, each area/step needs to have a cost associated with it. For example, an area of water will be more expensive than an area of flat ground. So for pathfinding, different areas will have different costs. If you go to the Areas tab in the Navigation panel, you can see all of the areas that you have defined and their respective costs.

You can also just mark areas as “notWalkable” so it can simply be cut from the navMesh entirely.

Combining the concepts of NavMesh Agent and Navigation Areas, you could allow different areas for different agents. For example, a ghost could go through walls and doors, while a humanoid could not. So if you had marked those areas as “Door” and “Wall”, you could insure they are selected for the Area Mask in the NavMesh Agent for the Ghost, but unselected for the Human.

Things I bumped into.

navMeshAgent.SetDestination(position); only needs to be set once or whenever you want to change the destination.

An agent will always try to navigate within the constraints of the NavMesh. So even if you set the GameObject’s transform.position with code, it will only move as far until it hits something that blocks its way. To move it immediately to another position there are two ways.

  • disable the gameObject setActive(false);, set the position and then reenable it setActive(true);.
  • Use the Warp method;navMeshAgent.Warp(position);

Interesting asides.

https://docs.unity3d.com/Manual/nav-MixingComponents.html

NavMesh Agent and physics!

  • You will have to add a Collider and a Rigidbody.
  • Enable Kinematic for the Rigidbody. Apparently Kinematic means that the the rigidBody is controlled by something other than Physics. If Kinematic is not enabled, the Physics Simulation and the NavMesh Agent components are fighting to control its movement.

NavMesh Agent and Player Controller!

  • There is some extra setup for this, but think of like you want the player piece to respect the navmesh constraints and have the other agents respect you while moving.

--

--