Overview
Unity’s NavMesh system is the engine’s built-in navigation framework for 3D agents. It represents walkable space as a navigation mesh, then lets NavMeshAgent components query paths, follow them, and avoid one another locally. For most 3D games, this is the right default before considering custom pathfinding. The underlying ideas are the same ones discussed in navigation-mesh literature and AI textbooks, but Unity packages them into editor components and APIs that are practical for student and production use. (Game AI Pro 360: Guide to Movement and Pathfinding, see source-game-ai-pro-360-movement-pathfinding)
Setup
The core Unity NavMesh toolset consists of four parts:
| Tool | Purpose |
|---|---|
| NavMesh Surface | Defines which geometry contributes to the baked walkable area |
| NavMeshAgent | Moves an NPC over the baked mesh |
| NavMeshObstacle | Marks dynamic blockers and can carve holes in the mesh |
| NavMeshLink | Creates explicit connections such as jumps, drops, or ladders |
Typical setup:
- Add a NavMesh Surface to a manager object.
- Choose which layers/objects count as navigation geometry.
- Set agent radius, height, step height, and slope limits.
- Bake the mesh.
- Add NavMeshAgent to NPCs that need autonomous movement.
Usage
Minimal scripting workflow:
using UnityEngine;
using UnityEngine.AI;
public class EnemyMover : MonoBehaviour
{
[SerializeField] private Transform target;
private NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (target != null)
agent.SetDestination(target.position);
}
}The agent handles path query, waypoint following, turning, and basic local avoidance. Higher-level systems such as behaviour-trees, utility-ai, or goal-oriented-action-planning decide when to move and which destination to request.
Gotchas
- Wrong default for 2D tile games: NavMesh is strongest in 3D or freeform navigation. For strict 2D grids, custom A* over a grid may be cleaner.
- Carving is expensive if abused:
NavMeshObstaclewith carving enabled is not meant for constantly moving objects. - Bake settings are design settings: agent radius or slope values that are slightly wrong can silently remove narrow paths or generate awkward movement.
- NavMesh is not tactics: it gives traversable routes, not smart destination choice. That still belongs to AI logic.
Related
navigation-mesh-construction · pathfinding-algorithms · game-ai-agent-design · buddy-ai · npc-performance-at-scale · source-game-ai-pro-360-movement-pathfinding