02 — Setup and NavMesh
What to install
- 2D demos: nothing. The kit's A* pathfinder is plain C#.
- 3D demo: the AI Navigation package. Unity 6 template projects include it already — check Window → Package Manager → In Project for AI Navigation. If missing: Unity Registry → AI Navigation → Install (no restart needed).
What a NavMesh is
A NavMesh is a pre-computed map of where characters can walk — a mesh laid over your floors, with holes where walls and obstacles are, plus data about slopes and drops. Agents then ask "route from A to B" and get back a path that automatically hugs corners and avoids obstacles. You never write pathfinding code; you bake the map and steer with two lines:
using UnityEngine.AI; // core module, always available
agent.SetDestination(player.position); // that's the whole chase
The three pieces (mirror of the UI Toolkit trio)
- NavMeshSurface (component, from the AI Navigation package) — put it on your level root; its Bake button scans the geometry and builds the mesh. Toggle the scene-view NavMesh overlay (AI Navigation icon in the Scene view toolbar) to see the walkable area in blue.
- NavMeshAgent (component, built-in) — put it on the enemy. Key settings: Speed / Angular Speed / Acceleration (how it moves), Radius/Height (how much room it needs), Stopping Distance (how close counts as "arrived" — the kit's patrol uses this).
- NavMeshData (asset) — the baked result. It must exist for agents to work; "Failed to create agent because there is no valid NavMesh" means you forgot to bake (or the agent spawned off the mesh).
Create 3D NavMesh Enemy Demo adds a NavMeshSurface to its Level object, bakes it, and saves the data asset — before spawning the player and enemy so their capsules don't get baked into the map as obstacles (a real gotcha when you bake by hand: set moving things to not be included, or bake before placing them).
EnemyKitSetup.cs calls NavMeshSurface via reflection rather than using Unity.AI.Navigation;. That's purely defensive: it keeps the kit compiling even in a project where the package isn't installed. In your own project code, use the normal using — it's shown in a comment right at the bake method.
Settings worth teaching
- Agent radius too big → enemies can't fit through doorways (the #1 "my agent won't move" cause after forgetting to bake).
- Bake → Agent Radius also affects how close the mesh hugs walls: rebake after changing it.
- Stopping Distance ≈ attack range − 0.5 for melee enemies stops the awkward "enemy stands inside the player" look.
- Runtime rebaking (
surface.BuildNavMesh()at runtime) works for destructible levels but is expensive — mention it, defer it.
Checklist
Next: 03 - The 3D Enemy