Skip to content

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 (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.
randomPoint(center, range) 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
NavMeshAgent::arrive EventType.NavMeshAgent.Arrive { position } A moveTo got there. When a Character Controller drives the agent, listen to CharacterController::arrive instead.

A crowd holds up to 128 agents. Agents move only in play, a few frames after it starts.

ClickToWalk.jx
#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');
}

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.

Villager.jx
#pragma lifecycle(startup, update)
const HOME_RANGE = 12; // metres
const 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);
}

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.

PathPreview.jx
#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.

  • Agents do not move at start. They join the navmesh a few frames into play. moveTo returns false until then; retry in update.
  • Characters float or snap. Check that the Character Controller and the NavMesh Agent are on the same entity, and that the capsule center fits the model.
  • Listening to NavMeshAgent::arrive on a character. When a Character Controller drives the agent, listen to CharacterController::arrive.