NavMesh Navigation
Unity's NavMesh system provides built-in pathfinding, obstacle avoidance, and agent movement for 3D game worlds.
Setting Up NavMesh
Mark Static Geometry
Select your floor, walls, and obstacles in the scene. Mark them as "Navigation Static" in the Inspector.
Bake the NavMesh
Open Window → AI → Navigation. Click the "Bake" tab and press "Bake". Unity generates the walkable mesh shown as a blue overlay.
Add NavMeshAgent
Add a NavMeshAgent component to your character. This handles pathfinding, movement, and obstacle avoidance automatically.
Set Destination
Call
agent.SetDestination(targetPosition)in your script to move the agent.
NavMeshAgent Properties
| Property | Description | Typical Value |
|---|---|---|
| Speed | Maximum movement speed | 3.5 |
| Angular Speed | Turning speed in degrees/sec | 120 |
| Acceleration | How quickly the agent reaches max speed | 8 |
| Stopping Distance | Distance from target where agent stops | 0.5 |
| Avoidance Priority | Lower numbers get right-of-way (0-99) | 50 |
| Radius | Agent collision radius for avoidance | 0.5 |
using UnityEngine; using UnityEngine.AI; public class ClickToMove : MonoBehaviour { private NavMeshAgent agent; private Camera cam; void Start() { agent = GetComponent<NavMeshAgent>(); cam = Camera.main; } void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = cam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { agent.SetDestination(hit.point); } } } }
Dynamic Obstacles
Use NavMeshObstacle for objects that move at runtime. Enable "Carve" to cut holes in the NavMesh dynamically, forcing agents to path around them.
OffMeshLinks
OffMeshLinks connect disconnected NavMesh areas, enabling agents to jump across gaps, climb ladders, or use teleporters. You can auto-generate them or place them manually for precise control.
NavMesh Surface (New API)
The modern AI Navigation package uses NavMeshSurface components instead of the legacy baking window. Benefits include runtime baking, per-surface configuration, and better support for procedural levels.
Lilly Tech Systems