How to make my enemy go back to idle after an animation finishes?

Godot 4.3

Hello! I’m trying to make my enemy go back to idle after an animation finishes, but it just keeps staying in idle and never playing the attack animation.

func _ready() -> void:
	$AnimationPlayer.play("idle_RIGHT")

func _on_radius_body_entered(body: Node2D) -> void:
	if body.is_in_group("Player"):
		$AnimationPlayer.play("Bite_RIGHT")
		if $AnimationPlayer.animation_finished:
			if $AnimationPlayer.current_animation == "Bite_RIGHT":
				$AnimationPlayer.play("idle_RIGHT")

I’d add print() statements in-between each line of _on_radius_body_entered() including right after the function’s declaration to see how far your code steps down. Also if using a normal AnimationPlayer Node, why not add a Call Method Track on the last keyframe just in general instead of animation_finished? That would be more scaleable long-term and allow you to define many specific functions during the course of an animation relying on its keyframes rather than code-driven checks.

Use AnimationTree with a StateMachine. Here’s a tutorial:

1 Like

animation_finished is a signal, when used in an if statement it evaluates to true. You want to await the signal to stop the function until it triggers.

func _on_radius_body_entered(body: Node2D) -> void:
	if body.is_in_group("Player"):
		$AnimationPlayer.play("Bite_RIGHT")
		await $AnimationPlayer.animation_finished

		# Maybe can avoid this if statement
		if $AnimationPlayer.current_animation == "Bite_RIGHT":
			$AnimationPlayer.play("idle_RIGHT")

You can also assign a automatic transition within the Animation panel by selecting the “Animation” button and using “Edit Transitions…” Another option as mentioned is to use an AnimationTree.

1 Like

Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.