Navigation
Navigation lets characters find their own way around your level. You mark the area they may walk on, the engine works out a navmesh (a map of the walkable ground), and any character you give a destination walks there by the shortest route, going around walls, crates and steep hills on the way.
It takes three pieces:
| Piece | What it does |
|---|---|
| NavMesh Volume | A box that marks the walkable area. The navmesh is built inside it. |
| NavMesh Obstacle | Put on anything with a collider that characters must walk around. |
| NavMesh Agent | Put on a character that should walk the navmesh. |
1. Add a NavMesh Volume
- In the Hierarchy, click Add Entity and pick NavMesh Volume (in the Navigation group).
- Move it so its centre sits on your ground.
- In the Inspector, set Half Extents so the box covers the area characters should be able to reach. Half extents are measured from the centre, so
25, 5, 25makes a 50 by 50 area.
The navmesh builds straight away and is drawn as red wireframe triangles while Debug Draw is on. Wherever there are triangles, an agent can stand.
Use one volume per scene. If you need a bigger area, make the one volume bigger.
Volume settings
| Field | What it changes |
|---|---|
| Half Extents | The size of the walkable area, as half its width, height and depth. Default 25, 5, 25. |
| Agent Radius | How wide your characters are. The navmesh keeps this far away from every obstacle, so a character never scrapes a wall. Default 0.5. |
| Build > Upward Shift | Lifts the navmesh slightly above the ground. Default 0.2. |
| Build > Simplify | Smooths the outline of obstacles. Higher values give fewer, larger triangles. Default 0.005. |
| Build > Merge Steps | Merges small triangles together. Default 0. |
| Include Terrain | Makes steep parts of a terrain inside the volume unwalkable. Off by default. |
| Max Slope (degrees) | With Include Terrain on: slopes steeper than this are blocked. Default 45. |
| Terrain Sample Step | With Include Terrain on: how finely the terrain is checked. 1 checks every point (slow), 4 is a good default, 8 is coarse. |
| Debug Draw | Shows the navmesh triangles and agent paths in the viewport. |
2. Mark obstacles
Walls, crates, trees and anything else in the way need two things:
- A collider, such as a Box Collider (see Physics).
- The NavMesh Obstacle component. Select the entity, click Add in the Inspector and pick NavMesh Obstacle.
The obstacle's collider is cut out of the navmesh. Move the obstacle and the navmesh updates to match.
Only things marked as obstacles are cut out. That is on purpose: your floor has a collider too, and you don't want it to block the whole level.
3. Add an agent
Select your character, click Add in the Inspector and pick NavMesh Agent.
| Field | What it changes |
|---|---|
| Speed | Walking speed, in units per second. Default 5. |
| Turn Speed | How quickly it turns to face where it is going. Default 8. |
| Stopping Distance | How close to the destination counts as arrived. Default 0.2. |
| Target | Where it is walking to. Set by a script while the game runs. |
| Has Target | Shows whether it currently has somewhere to go. |
Agents only walk while the game is playing. In the editor they stand still, even if they have a target saved.
An agent moves across the ground and turns to face its direction of travel. It keeps its own height while it walks, so it does not climb up or down slopes by itself.
4. Tell an agent where to go
A script on the agent sends it somewhere with the nav_set_destination action, and stops it with nav_clear_destination. New to scripts? Start with Rust scripts.
This guard walks back and forth between two points:
use bevy::prelude::*;
use renzora::{ScriptAction, ScriptCtx};
const POINTS: [Vec3; 2] = [Vec3::new(-8.0, 0.0, 0.0), Vec3::new(8.0, 0.0, 0.0)];
/// Which point the guard is heading for.
#[derive(Component)]
struct Patrol {
next: usize,
}
fn go_to(ctx: &mut ScriptCtx, point: Vec3) {
let me = ctx.entity();
ScriptAction::new("nav_set_destination", me)
.arg("target", point)
.send(ctx.world());
}
fn update(ctx: &mut ScriptCtx) {
let Some(pos) = ctx.get::<Transform>().map(|t| t.translation) else {
return;
};
// First frame: start walking to the first point.
let Some(next) = ctx.get::<Patrol>().map(|p| p.next) else {
ctx.insert(Patrol { next: 0 });
go_to(ctx, POINTS[0]);
return;
};
// Close enough? Turn round and head for the other point.
let target = POINTS[next];
let here = Vec2::new(pos.x, pos.z);
if here.distance(Vec2::new(target.x, target.z)) < 0.5 {
let next = (next + 1) % POINTS.len();
if let Some(mut patrol) = ctx.get_mut::<Patrol>() {
patrol.next = next;
}
go_to(ctx, POINTS[next]);
}
}
renzora::script!(update);
To stop an agent where it is:
ScriptAction::new("nav_clear_destination", ctx.entity()).send(ctx.world());
Asking how the trip is going
Every agent also gets a small status component, NavReadState, updated each frame. Read it by name:
use renzora::reflection::get_reflected_field;
use renzora::PropertyValue;
let me = ctx.entity();
if let Some(PropertyValue::Float(left)) =
get_reflected_field(ctx.world(), me, "NavReadState", "distance_to_destination")
{
info!("{left:.1} units to go");
}
| Field | Meaning |
|---|---|
has_target | It has a destination. |
has_path | A route to the destination was found. |
is_at_destination | It has arrived (or was never sent anywhere). |
distance_to_destination | Straight-line distance left, ignoring height. |
The Navmesh panel
Open the Navmesh panel from the + on any panel's tab bar (it is under Navigation). It lists every volume in the scene with its build status (Built, Building..., Failed) and has a few tools:
| Control | What it does |
|---|---|
| Show Agent Paths | Draws each moving agent's route as a green line with a dot at every turn. |
| Auto Rebuild | Rebuilds the navmesh whenever something changes. Turn it off in a busy scene and rebuild by hand. |
| Rebuild All | Rebuilds every volume now. |
| Reset Agents | Clears every agent's destination and path. |
| Bake to Disk | Saves the current navmesh beside the scene as a .navmesh file. |
| Debug Draw / Rebuild | Per volume: show its triangles, or rebuild just that one. |
The game builds its navmesh when the scene loads, so you don't need to bake before exporting.
When an agent won't move
The Console says why, under NavMesh:
| Message | What to do |
|---|---|
| Agent start ... is not on the navmesh | The agent is standing on an obstacle or outside the volume. Move it onto the red triangles. |
| Target ... is not on the navmesh | The destination is inside an obstacle or outside the volume. Pick a point on the triangles. |
| No path from ... to ... | Both points are on the navmesh but not joined. A gap is probably narrower than the Agent Radius: widen it, or lower the radius. |
Also check that the game is playing: agents never walk in the editor.