Godot: NavigationAgent3D
Godot 4 has navigation built in, no plugin needed. The pieces map onto Navigation Meshes and Path Smoothing:
| Node or API | Role |
|---|---|
NavigationRegion3D |
Holds a NavigationMesh resource and bakes it from the geometry under it |
NavigationAgent3D |
A helper node you put under a character: it requests paths, tracks waypoints and does avoidance. It doesn't move the body; your script does |
NavigationLink3D |
Connects two points the bake left apart (jumps, ladders) |
NavigationObstacle3D |
Something agents avoid, or that affects the bake |
NavigationServer3D |
The server underneath: maps, regions, path queries |
Godot also has decision-making add-ons (behavior trees, state machines) from the Asset Library, but no built-in behavior tree; see Libraries & Middleware.
Baking
Add a NavigationRegion3D, give it a new NavigationMesh resource, make the level geometry its children, and select the region and press Bake Navmesh in the toolbar. The resource's agent settings (radius, height, max climb, max slope) must match your character, or agents will clip corners and get stuck on steps. To rebake at runtime (a door opens, a bridge falls), call bake_navigation_mesh() on the region; it bakes on a thread by default and emits bake_finished.
Moving a character
The agent gives you the next position to walk to. Each physics frame, you steer toward it:
extends CharacterBody3D
@export var speed := 4.0
@onready var agent: NavigationAgent3D = $NavigationAgent3D
func _ready() -> void:
agent.velocity_computed.connect(_on_velocity_computed)
func set_target(where: Vector3) -> void:
agent.target_position = where
func _physics_process(_delta: float) -> void:
# The map is empty until the navigation server has synchronized once.
if NavigationServer3D.map_get_iteration_id(agent.get_navigation_map()) == 0:
return
if agent.is_navigation_finished():
return
var next := agent.get_next_path_position()
var desired := global_position.direction_to(next) * speed
if agent.avoidance_enabled:
agent.set_velocity(desired) # avoidance answers via velocity_computed
else:
_on_velocity_computed(desired)
func _on_velocity_computed(safe_velocity: Vector3) -> void:
velocity = safe_velocity
move_and_slide()Three things in that script trip up almost everyone:
- The first frame. Setting
target_positionin_ready()queries a map that hasn't synchronized yet. The path comes back empty andis_navigation_finished()is immediately true, so the character never moves. Wait for the map (the iteration check above, or a deferred call) before the first query. - Call
get_next_path_position()once per physics frame. It also advances the agent's internal waypoint index, so it belongs in_physics_process, not in_processor in several places. - With avoidance on, move in the callback.
set_velocity()hands your desired velocity to the avoidance system, and the safe velocity arrives through thevelocity_computedsignal. Moving in both places makes the character jitter.
Two distances control when the agent is "there". path_desired_distance says how close counts as reaching a waypoint (then it switches to the next one), and target_desired_distance how close counts as reaching the target. This model walks a path through a door and prints when each waypoint is consumed:
using System;
using System.Collections.Generic;
var path = new List<(double x, double y)> { (0, 0), (4, 0), (4, 3), (8, 3) }; // from the path query
var agent = new Agent(path, pathDesired: 1.0, targetDesired: 0.5);
var pos = (x: 0.0, y: 0.0);
const double speed = 1.5;
for (int frame = 0; frame < 12 && !agent.IsNavigationFinished; frame++)
{
var next = agent.GetNextPathPosition(pos);
if (agent.IsNavigationFinished) { Console.WriteLine(quot;frame {frame,2}: target reached at ({pos.x:F2}, {pos.y:F2})"); break; }
double dx = next.x - pos.x, dy = next.y - pos.y, len = Math.Sqrt(dx * dx + dy * dy);
double step = Math.Min(speed, len);
pos = (pos.x + dx / len * step, pos.y + dy / len * step);
Console.WriteLine(quot;frame {frame,2}: heading to ({next.x}, {next.y}), now at ({pos.x:F2}, {pos.y:F2})");
}
class Agent
{
readonly List<(double x, double y)> path;
readonly double pathDesired, targetDesired;
int index = 1;
public bool IsNavigationFinished;
public Agent(List<(double x, double y)> path, double pathDesired, double targetDesired)
{ this.path = path; this.pathDesired = pathDesired; this.targetDesired = targetDesired; }
static double Dist((double x, double y) a, (double x, double y) b) => Math.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
public (double x, double y) GetNextPathPosition((double x, double y) from)
{
// Skip every waypoint we are already close enough to (path_desired_distance).
while (index < path.Count - 1 && Dist(from, path[index]) <= pathDesired)
{
Console.WriteLine(quot; waypoint {index} ({path[index].x}, {path[index].y}) reached");
index++;
}
if (index == path.Count - 1 && Dist(from, path[index]) <= targetDesired) IsNavigationFinished = true;
return path[index];
}
}frame 0: heading to (4, 0), now at (1.50, 0.00)
frame 1: heading to (4, 0), now at (3.00, 0.00)
waypoint 1 (4, 0) reached
frame 2: heading to (4, 3), now at (3.47, 1.42)
frame 3: heading to (4, 3), now at (3.95, 2.85)
waypoint 2 (4, 3) reached
frame 4: heading to (8, 3), now at (5.45, 2.90)
frame 5: heading to (8, 3), now at (6.95, 2.96)
frame 6: heading to (8, 3), now at (8.00, 3.00)
frame 7: target reached at (8.00, 3.00)
Notice that the corner waypoints count as reached from 1 unit away, so the character starts turning early and rounds the corner instead of touching it, while the final target needs 0.5. If path_desired_distance is larger than the gap between two waypoints, the agent skips waypoints and can cut through walls; if target_desired_distance is smaller than the distance the character moves per frame, it can overshoot and orbit the target.
Asking the server directly
For planning without moving anything (is this spot reachable? how long is the route to each cover point?), query the server:
var map := get_world_3d().navigation_map
var route := NavigationServer3D.map_get_path(map, global_position, cover_point, true)
var length := 0.0
for i in range(1, route.size()):
length += route[i - 1].distance_to(route[i])The last argument enables path optimization (string pulling, as in Path Smoothing). For many queries, NavigationServer3D.query_path() with NavigationPathQueryParameters3D lets you reuse parameter and result objects.
Debugging
Turn on Debug > Visible Navigation in the editor menu to see navmeshes, links and agent paths while the game runs, and set debug_enabled on a NavigationAgent3D to draw its current path. The Debugger > Monitors tab includes navigation counters (maps, regions, agents). See Debugging AI and Godot 4 with GDScript.
Test yourself
A character sets agent.target_position in _ready() and never moves. Why?
- NavigationAgent3D needs a plugin
- The navigation map hasn't synchronized yet, so the first path is empty and navigation counts as finished
- target_position must be set every frame
- _ready() runs after _physics_process
With avoidance_enabled on, where should the character actually move?
- In _process
- Right after calling set_velocity
- In the velocity_computed signal handler, using the safe velocity
- In _ready
What does path_desired_distance control?
- The maximum path length
- How close the agent must get to a waypoint before moving on to the next one
- The avoidance radius
- How far the agent can see