Godot Version
Godot 4.5
Question
Hello there!
I’m having trouble with some navigation agent unable to go through the path. I set up a navigation region, baked the mesh and the navigation path generated for the agent looks right.
However it seems the navigation agent get stuck whenever reaching the first next_path_position.
I logged the global_position of my agent and the next_path_position when and it seems to loop when trying to reach the path position:
(-0.607327, 0.15, 4.001292)(-0.5, 0.15, 4.0)
(-0.474003, 0.15, 3.999687)(-0.5, 0.15, 4.0)
(-0.607327, 0.15, 4.001292)(-0.5, 0.15, 4.0)
(-0.474003, 0.15, 3.999687)(-0.5, 0.15, 4.0)
It seems it’s never reaching the exact next_path_position so then it never gets to the next one. Although i have the Path desired distance set up to 5.0m so i would expect that as soon as it approach the next_path_position by 5m it will update to the next one.
My agent is a character3D that has no collision set up so it shouldn’t be a collision issue.
Could someone help me understand what’s happening ?
Here is the code of my character 3d (taken directly from the doc):
extends CharacterBody3D
var movement_speed: float = 8.0
var movement_target_position: Vector3 = Vector3(-100,0,121)
@onready var navigation_agent: NavigationAgent3D = $"../NavigationAgent3D"
func _ready() -> void:
navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
navigation_agent.set_target_position(movement_target_position)
func _physics_process(_delta):
# Do not query when the map has never synchronized and is empty.
if NavigationServer3D.map_get_iteration_id(navigation_agent.get_navigation_map()) == 0:
return
if navigation_agent.is_navigation_finished():
return
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
print(global_position,next_path_position)
if navigation_agent.avoidance_enabled:
navigation_agent.set_velocity(new_velocity)
else:
_on_velocity_computed(new_velocity)
func _on_velocity_computed(safe_velocity: Vector3):
velocity = safe_velocity
move_and_slide()