Hazel

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:

PieceWhat it does
NavMesh VolumeA box that marks the walkable area. The navmesh is built inside it.
NavMesh ObstaclePut on anything with a collider that characters must walk around.
NavMesh AgentPut on a character that should walk the navmesh.

1. Add a NavMesh Volume

  1. In the Hierarchy, click Add Entity and pick NavMesh Volume (in the Navigation group).
  2. Move it so its centre sits on your ground.
  3. 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, 25 makes 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

FieldWhat it changes
Half ExtentsThe size of the walkable area, as half its width, height and depth. Default 25, 5, 25.
Agent RadiusHow 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 ShiftLifts the navmesh slightly above the ground. Default 0.2.
Build > SimplifySmooths the outline of obstacles. Higher values give fewer, larger triangles. Default 0.005.
Build > Merge StepsMerges small triangles together. Default 0.
Include TerrainMakes 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 StepWith Include Terrain on: how finely the terrain is checked. 1 checks every point (slow), 4 is a good default, 8 is coarse.
Debug DrawShows the navmesh triangles and agent paths in the viewport.

2. Mark obstacles

Walls, crates, trees and anything else in the way need two things:

  1. A collider, such as a Box Collider (see Physics).
  2. 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.

FieldWhat it changes
SpeedWalking speed, in units per second. Default 5.
Turn SpeedHow quickly it turns to face where it is going. Default 8.
Stopping DistanceHow close to the destination counts as arrived. Default 0.2.
TargetWhere it is walking to. Set by a script while the game runs.
Has TargetShows 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");
}
FieldMeaning
has_targetIt has a destination.
has_pathA route to the destination was found.
is_at_destinationIt has arrived (or was never sent anywhere).
distance_to_destinationStraight-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:

ControlWhat it does
Show Agent PathsDraws each moving agent's route as a green line with a dot at every turn.
Auto RebuildRebuilds the navmesh whenever something changes. Turn it off in a busy scene and rebuild by hand.
Rebuild AllRebuilds every volume now.
Reset AgentsClears every agent's destination and path.
Bake to DiskSaves the current navmesh beside the scene as a .navmesh file.
Debug Draw / RebuildPer 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:

MessageWhat to do
Agent start ... is not on the navmeshThe agent is standing on an obstacle or outside the volume. Move it onto the red triangles.
Target ... is not on the navmeshThe 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.