Navigation Agent Causes Node to "Jitter" Back and Forth Once it Returns to Original Position

Godot Version

Godot 4.2


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(): 
		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:

A node position and the closest position on navigation mesh rarely end up the same so that jitter is you still moving your node to a position that is not the same as the node.

If you update the navigation path so very frequently you also end up with slightly different positions all the time.

The simplest and most obvious solution is to stop moving your node when you dont want it to move.