Godot Version
v4.3
Question
So, this is not a “stuck by collision” type of stuck, but rather the agent keeps trying to reach a point and lerps between it over and over because the direction to reach it flips, but the agent doesn’t appear to ever “reach” the point to continue towards its target_position.
I would attach a video here but it appears I’m not permitted, as a new user. Basically the object has a destination and I can see it with the red debug line across the path. The agent is never “too close” to any walls, so there are no collisions being hit. When inspecting the code, I noticed the next position never changes, and the agent continues to try and reach it - even if it’s basically right on top of it.
Here’s the physics process code:
var next_position = get_next_path_position()
var direction = _look_towards(next_position, delta)
var curr_speed = chase_speed if _is_chasing else movement_speed
_parent_enemy.velocity = _parent_enemy.velocity.move_toward(direction * curr_speed, 0.25)
_parent_enemy.move_and_slide()
And for reference, this is the _look_towards code:
func _look_towards(global_destination: Vector3, delta: float) -> Vector3:
var direction = (global_destination - _parent_enemy.global_position).normalized()
_parent_enemy.rotation.y = lerp_angle(
_parent_enemy.rotation.y, atan2(direction.x, direction.z), delta * rotation_speed
)
return direction
Finally, the only setting I have changed in the pathfinding section of the agent is the height:
Note I have tweaked with path/target desired distance values in many ways and it seems to not really make a difference (if not make it worse)… So hoping to find some guidance here and see where I might be making a mistake in the code, or if there’s a setting I need to change.
Thank you!
UPDATE EDIT: I decided to re-tackle looking into this today, and noticed something interesting. According to the docs, the target desired distance should be the distance threshold before a target reaches a destination… I decided put in a hot key to press when the agent is getting stuck around this point and going back and forth between each pyhsics frame. I pressed the hot key and confirmed the distance was less than the target distance (literally just print(distance_to_target())
), as expected… So it seems that maybe this is a bug? Unless I’m misunderstanding something on the pathfinding setting, but if the distance to the target is less than the desired distance, should this not emit an target_reached
signal? I have an extra print statement in on my signal handler _on_target_reached
and it doesn’t get printed when the target’s reached this location… Instead the agent keeps going back and forth at that position.
UPDATE EDIT 2 (SOLUTION): Okay… My last edit kind of helped me figures this out. Upon further looking it looks like because the point’s not technically always on the navmesh, the agent gets stuck… The solution was to stop using target_reached
and instead use navigation_finished
.