Godot 4.2.1

So the walking animation for my Enemy is very jittery and it seems to be because the velocity of the Enemy instantly goes to 0 when the player is close and it makes the Enemy switch animations rapidly.

If the player keeps on walking close to the enemy the enemy’s velocity will go to 0 and then up again rapidly making the animation switch back and forth from walk and idle. Making it look jittery.

I have tried await animation_finished which kind of fixes it but it makes it look bad because then it will play the movement animation until finished when standing still.

So maybe i need to fix the problem of the velocity instantly going down to 0 when the Enemy comes to a stop? and instead make it slowly go down to 0? Not sure how to do that though.

extends Enemy

@export var animation_player_legs: AnimationPlayer

func _ready():
	super()
	

func _physics_process(delta):
	if velocity != Vector2.ZERO:
		animation_player_legs.play("Walk1Legs")
	elif velocity == Vector2.ZERO:
		if animation_player_legs.is_playing():
			await animation_player_legs.animation_finished
		animation_player_legs.play("Idle1Legs")
	

func move_to_position(target_position: Vector2):
	#set the velocity of the nav_agent to the target_position
	var motion = position.direction_to(target_position).normalized() * current_speed
	nav_agent.set_velocity(motion)


func _on_navigation_agent_2d_velocity_computed(safe_velocity):
	#Modifies the velocity set in move_to_position to implement avoidance
	velocity = safe_velocity
	if state_machine.check_if_can_move():
		move_and_slide()