Skip to main content

Navmesh (pathfinding)

By default, characters that go around walls (the Follow target behavior with Pathfinding, and the Move to position block) search a flat grid on the floor they stand on. That is fine for a yard with walls and rocks, but it cannot climb a staircase to the next floor or jump a gap.

The navmesh can. Turn it on and the game works out, from your static colliders, every place a character of its size can stand: ramps, stairs, upper floors, the ground under a bridge. Paths are then searched over that.

Turn it on​

Project settings → Navigation → Baked navmesh.

SettingMeans
Cell sizeHow finely the floor is sampled. Smaller is more exact and slower to bake (0.5 m is fine for most games).
Steepest slopeGround steeper than this is not walkable.
Step heightEdges up to this high are stepped over (kerbs, stairs).

The mesh is made the first time a character needs a path (a moment of work on a big level) and is kept until the scene ends. A mesh is made per character size, measured from the character's model, so a tall guard and a small dog get their own. Only static colliders count: moving bodies are not walls, and a wall that appears later is not seen until you call game.navmesh.rebuild().

In the Grave Guard showcase, skeletons and zombies chase the player round headstones this way.

What it understands​

  • Slopes and stairs: ground within the slope limit, joined by steps up to the step height.
  • Several floors: floors above each other are separate; a character reaches the upper one only by walking up something (stairs, a ramp) or over a link. The ground under an upper floor stays walkable.
  • Room for the character: places where its body would not fit (under a low ceiling, between close walls) are left out.
  • Goals off the mesh: a goal in a wall or across a gap makes the character walk to the reachable spot closest to it.

Walk to another floor​

Move to position has an optional Floor height Y. Left empty, the character goes to a place on the floor it is on; give a height to pick another floor at the same X and Z. (In scripts: entity.moveTo(x, z, speed, { y: 4 }).) Follow target goes to wherever the target stands.

A gap, a ledge down, a ladder: places the mesh does not join. Add a Nav link component to an object at the take-off spot and set Lands on to an object at the landing spot. Characters then jump from one to the other, on an arc, when that is the way. Both ways lets them jump back; Reach is how far from each object they may start and land.

From scripts​

const r = game.navmesh.findPath(enemy.position, player.position, { radius: 0.4, height: 1.8 });
if (r && r.complete) {
for (const p of r.points) console.log(p.x, p.y, p.z, p.leap ? 'jump' : '');
}
const floor = game.navmesh.sample(player.position); // nearest place to stand
game.navmesh.rebuild(); // after walls changed

See the mesh​

In the editor: Gizmos → Navmesh in the scene view's toolbar draws the walkable area as green cells on your floors, before you press Play. It follows what you move: walls cut holes, steps and slopes join or split areas. It is made from the objects' shapes, so it is a close picture, not the exact mesh the game builds. The tops of cubes show as separate green islands when they are too high to step onto.

In the game: Press Play, then the Show colliders button. The places a character can walk are drawn as green cells on the floor, with the colliders outlined. A wall that has no green hole under it and no outline has no collider, or it is not static: add a Collider and set the Rigidbody to Static (or leave out the Rigidbody) and it blocks paths.

This is the same idea as Unity's NavMesh display. Unity bakes the objects you mark Navigation Static and agents never leave that surface; here the objects that count are the static colliders, and the green cells are the baked surface.

A goal that is off the mesh (the player is outside the ground, or inside a wall) sends the character to the nearest place it can stand, and it stops there. It does not walk off the ground in a straight line.

Good to know​

  • With the navmesh off, nothing changes: the grid search is used.
  • If the mesh cannot be made (no static colliders), characters fall back to the grid search.
  • Not there yet: baking ahead of time, avoiding other moving characters, different surface costs (mud, road).