From godot-prompter
Implements AI movement in Godot 4.3+ using NavigationAgent2D/3D, steering behaviors, behavior trees, and patrol patterns.
How this skill is triggered — by the user, by Claude, or both
Slash command
/godot-prompter:ai-navigationThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Cover NavigationAgent2D/3D, steering behaviors, behavior trees, and patrol patterns. All examples target Godot 4.3+ with no deprecated APIs.
Cover NavigationAgent2D/3D, steering behaviors, behavior trees, and patrol patterns. All examples target Godot 4.3+ with no deprecated APIs.
Related skills: state-machine for AI state management, component-system for modular AI behaviors, player-controller for movement physics patterns, math-essentials for pathfinding vectors and steering math, limboai for BT + HSM with a visual editor, beehave for lightweight GDScript behavior trees. For a structured behavior tree (rather than steering/navigation), see the comparison tables in limboai and beehave.
World (Node2D or Node3D)
└── NavigationRegion2D (or NavigationRegion3D)
├── TileMapLayer / StaticBody2D (geometry)
└── Enemy (CharacterBody2D with NavigationAgent2D child)
# 2D
$NavigationRegion2D.bake_navigation_polygon()
# 3D
$NavigationRegion3D.bake_navigation_mesh()
// 2D
GetNode<NavigationRegion2D>("NavigationRegion2D").BakeNavigationPolygon();
// 3D
GetNode<NavigationRegion3D>("NavigationRegion3D").BakeNavigationMesh();
Navigation baking can cause frame drops on large maps. Godot 4.4 supports baking on a background thread:
# 2D — bake on a background thread
func rebake_async() -> void:
var nav_region: NavigationRegion2D = $NavigationRegion2D
# Connect to know when baking finishes
NavigationServer2D.bake_from_source_geometry_data_async(
nav_region.navigation_polygon,
NavigationMeshSourceGeometryData2D.new()
)
# Or use the region's built-in signal:
nav_region.bake_finished.connect(_on_bake_finished, CONNECT_ONE_SHOT)
nav_region.bake_navigation_polygon(true) # true = use thread
func _on_bake_finished() -> void:
print("Navigation mesh ready")
# 3D — bake on a background thread
func rebake_async_3d() -> void:
var nav_region: NavigationRegion3D = $NavigationRegion3D
nav_region.bake_finished.connect(_on_bake_finished_3d, CONNECT_ONE_SHOT)
nav_region.bake_navigation_mesh(true) # true = use thread
func _on_bake_finished_3d() -> void:
print("3D navigation mesh ready")
// 3D — bake on a background thread
public void RebakeAsync3D()
{
var navRegion = GetNode<NavigationRegion3D>("NavigationRegion3D");
navRegion.BakeFinished += OnBakeFinished;
navRegion.BakeNavigationMesh(true); // true = use thread
}
private void OnBakeFinished()
{
GD.Print("3D navigation mesh ready");
}
When to use async baking: Procedurally generated levels, destructible terrain, or any scene where the navigation mesh must be rebuilt at runtime. The game continues running while the mesh bakes.
Navigation layers let you separate walkable areas for different agent types (ground troops, flying units, large enemies).
# Assign layer bits on the NavigationRegion (Inspector or code)
# Layer 1 = ground, Layer 2 = air, Layer 3 = large
# On the NavigationAgent, set matching layers:
$NavigationAgent2D.navigation_layers = 1 # ground only
$NavigationAgent2D.navigation_layers = 2 # air only
$NavigationAgent2D.navigation_layers = 1 | 2 # both (bitwise OR)
// Assign layer bits on the NavigationRegion (Inspector or code)
// Layer 1 = ground, Layer 2 = air, Layer 3 = large
var navAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
navAgent.NavigationLayers = 1; // ground only
navAgent.NavigationLayers = 2; // air only
navAgent.NavigationLayers = 1 | 2; // both (bitwise OR)
Set
navigation_layerson both the NavigationRegion and the NavigationAgent so they match. Mismatched layers are one of the most common reasons an agent finds no path.
extends CharacterBody2D
@export var speed: float = 120.0
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
func _ready() -> void:
# velocity_computed fires when avoidance calculates a safe velocity
nav_agent.velocity_computed.connect(_on_velocity_computed)
func _physics_process(delta: float) -> void:
if nav_agent.is_navigation_finished():
return
var next_pos: Vector2 = nav_agent.get_next_path_position()
var direction: Vector2 = (next_pos - global_position).normalized()
var desired_velocity: Vector2 = direction * speed
if nav_agent.avoidance_enabled:
# Hand desired velocity to the avoidance system; wait for the signal
nav_agent.velocity = desired_velocity
else:
velocity = desired_velocity
move_and_slide()
func _on_velocity_computed(safe_velocity: Vector2) -> void:
velocity = safe_velocity
move_and_slide()
func set_target(target_pos: Vector2) -> void:
nav_agent.target_position = target_pos
Key NavigationAgent2D properties:
| Property | Purpose |
|---|---|
target_position | World-space destination |
path_desired_distance | How close to each waypoint counts as reached (default 1) |
target_desired_distance | How close to the final target counts as finished (default 10) |
avoidance_enabled | Enable RVO obstacle avoidance |
radius | Agent collision radius for avoidance |
time_horizon_agents | Seconds of avoidance look-ahead (tune to reduce jitter) |
using Godot;
public partial class Enemy2D : CharacterBody2D
{
[Export] public float Speed { get; set; } = 120f;
private NavigationAgent2D _navAgent;
public override void _Ready()
{
_navAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
_navAgent.VelocityComputed += OnVelocityComputed;
}
public override void _PhysicsProcess(double delta)
{
if (_navAgent.IsNavigationFinished()) return;
Vector2 nextPos = _navAgent.GetNextPathPosition();
Vector2 direction = (nextPos - GlobalPosition).Normalized();
Vector2 desiredVelocity = direction * Speed;
if (_navAgent.AvoidanceEnabled)
_navAgent.Velocity = desiredVelocity;
else
{
Velocity = desiredVelocity;
MoveAndSlide();
}
}
private void OnVelocityComputed(Vector2 safeVelocity)
{
Velocity = safeVelocity;
MoveAndSlide();
}
public void SetTarget(Vector2 targetPos) => _navAgent.TargetPosition = targetPos;
}
extends CharacterBody3D
@export var speed: float = 4.0
@export var gravity: float = 9.8
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
func _ready() -> void:
nav_agent.velocity_computed.connect(_on_velocity_computed)
func _physics_process(delta: float) -> void:
# Apply gravity
if not is_on_floor():
velocity.y -= gravity * delta
if nav_agent.is_navigation_finished():
move_and_slide()
return
var next_pos: Vector3 = nav_agent.get_next_path_position()
var direction: Vector3 = (next_pos - global_position)
direction.y = 0.0
direction = direction.normalized()
var desired_velocity: Vector3 = direction * speed
desired_velocity.y = velocity.y # preserve gravity
if nav_agent.avoidance_enabled:
nav_agent.velocity = desired_velocity
else:
velocity = desired_velocity
move_and_slide()
func _on_velocity_computed(safe_velocity: Vector3) -> void:
velocity = safe_velocity
move_and_slide()
func set_target(target_pos: Vector3) -> void:
nav_agent.target_position = target_pos
using Godot;
public partial class Enemy3D : CharacterBody3D
{
[Export] public float Speed { get; set; } = 4f;
[Export] public float Gravity { get; set; } = 9.8f;
private NavigationAgent3D _navAgent;
public override void _Ready()
{
_navAgent = GetNode<NavigationAgent3D>("NavigationAgent3D");
_navAgent.VelocityComputed += OnVelocityComputed;
}
public override void _PhysicsProcess(double delta)
{
var vel = Velocity;
if (!IsOnFloor()) vel.Y -= Gravity * (float)delta;
if (_navAgent.IsNavigationFinished())
{
Velocity = vel;
MoveAndSlide();
return;
}
Vector3 nextPos = _navAgent.GetNextPathPosition();
var direction = (nextPos - GlobalPosition) with { Y = 0f };
direction = direction.Normalized();
vel.X = direction.X * Speed;
vel.Z = direction.Z * Speed;
if (_navAgent.AvoidanceEnabled)
_navAgent.Velocity = vel;
else
{
Velocity = vel;
MoveAndSlide();
}
}
private void OnVelocityComputed(Vector3 safeVelocity)
{
Velocity = safeVelocity;
MoveAndSlide();
}
public void SetTarget(Vector3 targetPos) => _navAgent.TargetPosition = targetPos;
}
Lightweight per-frame calculations (seek, flee, arrive, wander) that produce natural-looking movement without a navigation mesh. Combine them by summing the returned vectors, or pick one and assign it to velocity each _physics_process tick.
See references/steering-behaviors.md for the full GDScript and C# implementations of seek, flee, arrive (with deceleration ramp), and wander (with circle-projection jitter).
A NavigationAgent2D plus an array of Marker2D waypoints and a Timer for the pause at each point produces a clean patrol loop. Cycle the index on wait_timer.timeout, set nav_agent.target_position to the next waypoint, and gate movement on is_navigation_finished().
See references/patrol-patterns.md for the full GDScript and C# waypoint-chain patrol with wait-timer pauses.
A behavior tree (BT) is a tree of nodes evaluated every tick. Three core node types:
| Type | Succeeds when | Fails when |
|---|---|---|
| Sequence | all children succeed (AND) | any child fails |
| Selector | any child succeeds (OR) | all children fail |
| Action | the leaf action completes | the leaf reports failure |
Sequences model "do A then B then C". Selectors model "try A, else try B, else try C".
See references/behavior-trees.md for the full lightweight BT implementation (BTNode base + Sequence / Selector / Action) and a worked enemy that uses "chase OR patrol", in both GDScript and C#.
Combines NavigationAgent2D with a state machine. See the state-machine skill for the full FSM infrastructure.
| State | Entry condition | Exit condition |
|---|---|---|
| PATROL | default / player escaped | player enters detect_range |
| CHASE | player in detect_range | player in attack_range OR player escaped |
| ATTACK | player in attack_range | player left attack_range |
See references/chase-attack.md for the full GDScript and C# implementation (PATROL → CHASE → ATTACK transitions, attack cooldown timer, patrol-waypoint advancement, escape-range hand-off back to patrol).
For larger projects, extract each state into its own node class using the state-machine skill and inject the
NavigationAgent2Dreference from the parent.
Prior to Godot 4.5, NavigationServer2D was a thin frontend that delegated all work to the 3D navigation server internally. Godot 4.5 splits them into fully independent servers. The change is transparent — no API changes and no code migration is required — but it brings two practical benefits:
# No code change needed — NavigationServer2D calls work identically.
# The split is internal; you continue using NavigationServer2D as before.
# Example: query a path directly via the server (unchanged API).
func get_path_to(target: Vector2) -> PackedVector2Array:
var map: RID = get_world_2d().get_navigation_map()
return NavigationServer2D.map_get_path(
map,
global_position,
target,
true # optimize path
)
// No code change needed — NavigationServer2D calls work identically.
public PackedVector2Array GetPathTo(Vector2 target)
{
var map = GetWorld2D().GetNavigationMap();
return NavigationServer2D.MapGetPath(map, GlobalPosition, target, true);
}
2D-only projects: In Project Settings → Modules, you can disable the
NavigationServer3Dmodule to reduce export size. This is only safe if no 3D navigation nodes (NavigationRegion3D,NavigationAgent3D) are used anywhere in the project.
| Pitfall | Symptom | Fix |
|---|---|---|
| Navigation mesh not baked | Agent stands still; no path found | Bake the NavigationPolygon/NavigationMesh before running, or call bake_navigation_polygon() at runtime after scene loads |
agent_radius too large | Agent can't fit through doorways or narrow corridors | Lower radius on NavigationAgent to be slightly less than half the passage width |
| Avoidance jitter | Agent stutters or oscillates when near other agents | Increase time_horizon_agents (try 2–4 s) or slightly lower max_speed on the agent |
| Path recalculation too frequent | CPU spike each frame; agents lag | Add a Timer (0.2–0.5 s) and only set target_position when the timer fires, not every physics frame |
| Wrong navigation layer | Agent ignores some regions or finds no path | Confirm navigation_layers bitmask matches between the NavigationRegion and the NavigationAgent |
| Target set before NavigationServer is ready | Path is empty on the first frame | Defer target_position assignment to _ready() or await NavigationServer2D.map_changed |
| Gravity ignored in 3D | Agent floats or sinks into the floor | Always accumulate velocity.y from gravity separately; only zero out X/Z from the nav direction |
| Baking causes frame drop | Synchronous bake on large maps blocks the main thread | Use async baking: bake_navigation_mesh(true) (Godot 4.4+); connect bake_finished signal |
navigation_layers bitmask matches between NavigationRegion and NavigationAgentNavigationAgent2D/NavigationAgent3D is a child of the enemy nodeget_next_path_position() called each physics frame, not get_target_position()velocity_computed signal connected when avoidance_enabled is trueis_navigation_finished() checked before moving to avoid jitter at the destinationagent_radius small enough to fit through the narrowest passage in the levelbake_navigation_mesh(true)) to avoid frame drops (Godot 4.4+)NavigationServer3D in Project Settings to reduce export sizenpx claudepluginhub jame581/godotprompter --plugin godot-prompterUnity 6 AI and navigation guide. Use when working with NavMesh, pathfinding, NavMeshAgent, NavMeshSurface, NavMeshObstacle, off-mesh links, or Unity Sentis (ML model inference). Covers AI navigation package, runtime NavMesh baking, and common AI patterns like state machines and behavior trees. Based on Unity 6.3 LTS documentation.
Implements behavior trees in Godot using the Beehave addon (GDScript-only). Covers composites, decorators, leaves, blackboard, and visual debugger.
Walks NPC AI design from perception through action, intent, personality knobs, and defeat handling. Outputs a GDScript state-machine stub and node tree for enemies, bosses, companions, civilians, or wave-spawned mobs. Trigger on 'enemy', 'NPC', 'behavior'.