Godot Version
Godot 4.2
Question
I am trying to code a pathfinding component node to apply to Enemies in my game. When an Enemy spots a Player in its aggro zone (an Area2D), it moves towards it. When the Player leaves a deaggro zone, the Enemy is supposed to return to its home position. However, when it arrives close to its home position, the Enemy “jitters” back and forth and doesn’t stay still. I have no clue why this happens and having some help on why would be very helpful.
# this is a Pathfinding Component used for an Enemy in my game
func _physics_process(delta):
if nav_agent.is_navigation_finished():
return
else:
print(nav_agent.distance_to_target())
next_path_pos = nav_agent.get_next_path_position()
dir = self.get_parent().global_position.direction_to(next_path_pos)
dir = to_local(nav_agent.get_next_path_position()).normalized()
movement = dir * velocityComp.max_speed
movementVar.emit(movement) # emits the vector to the Enemy scene for move_and_slide
# this is called every 0.1 seconds on a timer
func recalc_path():
if target_node:
nav_agent.set_target_position(target_node.global_position)
else:
nav_agent.set_target_position(home_pos)