Godot 4's navigation collision avoidance - how does it really work?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Macryc

Can someone explain how the Godot 4 collision avoidance with navigation agents system really works? I’ve been testing it and it doesn’t seem like it’s recalculating the path’s next step to bypass an obstacle based on the NavigationObstacle3d’s radius. I thought that was the deal but in my test project, when the navigation agent runs into an obstacle, it slows down and wriggles as opposed to walk around it even though there is plenty of room on the navigation region to do so… I’m assuming the system works differently but I can’t really infer how from the docs. Does anyone know?

:bust_in_silhouette: Reply From: cm

Take a look here

It’s a little outdated (some naming has changed), but the gist is that you set your navigation agent’s velocity in _physics_process, but you don’t apply that velocity to your character. Instead you listen for the nav agent’s velocity_computed signal, and use the supplied “safe” velocity for your character.

Something like this (in this case “npc” is a rigid body):

func _ready() -> void:
    nav_agent.velocity_computed.connect(on_nav_velocity_computed)

func _physics_process(delta: float) -> void:
    var origin = npc.global_transform.origin
    var target = nav_agent.get_next_location()
    var velocity = (target - origin).normalized()
    nav_agent.set_velocity(velocity * SPEED)

func _on_nav_velocity_computed(safe_velocity: Vector3) -> void:
    npc.linear_velocity = safe_velocity
:bust_in_silhouette: Reply From: smix8

If you want to actually have an static “obstacle” that affects the pathfinding you need to change your navigation mesh.

The pathfinding is only concerned with navigation mesh. If you still have navigation mesh below the obstacle occupied surface the paths will insist that the agent moves to a point that it can not reach due to the obstacle and the avoidance velocity pushing it back.

This is why the obstacles are only intended for moving and temporary obstacles as they do not change the navigation mesh used in pathfinding, they only change the avoidance velocities.