NavMesh Agents
Updated
A NavMesh Agent is a character that finds its own way: to the kitchen, round the table, through the door, never into a wall. It plans a path across the baked navmesh, avoids the other agents as a crowd, and crosses the links on its way. Most agents drive a Character Controller, which walks and animates the character.
NavMesh Agent
Section titled “NavMesh Agent”NavMesh Agent (comp-navmesh-agent) moves its entity over the navmesh and joins the surface’s crowd, so agents avoid each other. It never turns the entity: a Character Controller on the same entity walks it, turns it and reports its state.
| Prop | Type | Default | Description |
|---|---|---|---|
speed |
number | 3.5 |
Walk speed of a moveTo, in world units per second. |
radius |
number | 0.5 |
Capsule radius, in world units. |
height |
number | 1.7 |
Capsule height, in world units. |
| Member | What it does |
|---|---|
moveTo(point) |
Find a path to the navmesh point nearest { x, y, z }, or to an object’s current position. Returns false when the agent is not on the navmesh yet or no route exists. |
stop() |
Cancel a moveTo; the agent slows and holds position. |
walk(dx, dz) |
Step this frame by a world-space displacement, sliding along the navmesh and clamped to its edges, without crowd steering. A non-zero step cancels a moveTo. |
teleport(point) |
Put it on the navmesh point nearest point at once. |
pathTo(point) |
The corners of a path from where it stands to point; empty when unreachable. |
random |
A random reachable point within range of center, or undefined. |
destination |
Where a moveTo is heading, or undefined. |
isMoving |
Whether it is actually moving. |
isGrounded |
false only while crossing a link. |
With a Character Controller on the same entity, the agent owns the position, and you usually call the Character Controller’s moveTo, which pathfinds through the agent and sends CharacterController::arrive.
| Event | Constant | Data | When |
|---|---|---|---|
Nav |
Event |
{ position } |
A moveTo got there. When a Character Controller drives the agent, listen to Character instead. |
A crowd holds up to 128 agents. Agents move only in play, a few frames after it starts.
Examples
Section titled “Examples”1. Click to walk there
Section titled “1. Click to walk there”#pragma lifecycle(startup, update)
let character;let surface;
function startup() { character = findEntity('Hero').getComponent(CharacterControllerComponent); surface = findEntity('NavMesh Surface').getComponent(NavMeshSurfaceComponent);}
function update() { if (!Input.mouse.isButtonReleased(MouseButton.Left)) return; const hit = GraphicsRaycaster.fromScreen(Input.mouse.position()).hit(); if (!hit) return; const goal = surface.closestPoint(hit.point); if (goal && !character.moveTo(goal)) log('No route there');}Each click is cast onto the scene, snapped to the navmesh with closestPoint, and the hero walks there round the walls through its NavMesh Agent and Character Controller.
Click the floor to send the hero there. Drag to orbit the camera, scroll to zoom.
On each left click the script casts a ray from the mouse into the scene, snaps the hit point onto the navmesh with closestPoint, and asks the Character Controller to moveTo it. With a NavMesh Agent on the hero, moveTo finds a path around walls and through doors. See Picking and raycasts for the raycaster.
2. A wandering villager
Section titled “2. A wandering villager”#pragma lifecycle(startup, update)
const HOME_RANGE = 12; // metresconst home = new THREE.Vector3();let agent;let character;let resting = 0;
function startup() { // The villager has a NavMesh Agent, a Character Controller (controls: false) // and a Signal Emitter with the signal 'villager'. agent = self.parent.getComponent(NavMeshAgentComponent); character = self.parent.getComponent(CharacterControllerComponent); self.parent.getWorldPosition(home);
on('villager', (event) => { if (event.eventType === EventType.CharacterController.Arrive) resting = 2 + Math.random() * 3; });}
function update(dt) { if (character.destination || resting > 0) { resting -= dt; return; } const spot = agent.randomPoint(home, HOME_RANGE); if (spot) character.moveTo(spot);}A villager picks a random reachable point near home with randomPoint, walks there with its Character Controller, rests two to five seconds on arrive, and sets off again.
Watch the villager wander around the walls, resting between walks. Drag to orbit the camera, scroll to zoom.
The villager remembers where it started, then repeatedly picks a random reachable point within 12 m of home with randomPoint and walks there. On arrival, the Character Controller sends arrive, and the script rests for two to five seconds before choosing the next spot. While a walk is in progress, destination is set and the script waits.
Snippets
Section titled “Snippets”1. Draw the path a character will take
Section titled “1. Draw the path a character will take”#pragma lifecycle(startup, update)
let agent;let line;let geometry;
function startup() { agent = findEntity('Hero').getComponent(NavMeshAgentComponent); geometry = new THREE.BufferGeometry(); line = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: '#38bdf8' })); self.add(line);}
function update() { const goal = agent.destination; line.visible = Boolean(goal); if (!goal) return; const corners = agent.pathTo(goal); geometry.setFromPoints(corners.map((c) => new THREE.Vector3(c.x, c.y + 0.05, c.z)));}pathTo returns the corners of the path from where the agent stands to its destination. The script draws them as a line just above the ground and hides it when there is no destination. Put this script on an entity at the scene origin with no rotation or scale, so the line’s local coordinates match the world. For a busy scene, update the line only every few frames.
Common mistakes
Section titled “Common mistakes”- Agents do not move at start. They join the navmesh a few frames into play.
moveToreturnsfalseuntil then; retry inupdate. - Characters float or snap. Check that the Character Controller and the NavMesh Agent are on the same entity, and that the capsule
centerfits the model. - Listening to
NavMeshAgent::arriveon a character. When a Character Controller drives the agent, listen toCharacterController::arrive.