Can't get these animations to work? How to fix it?

Godot 4.3

I’m currently working on a project that has an animation if you get in the radius of the enemy, it plays another animation, but it doesn’t work…why?

func _ready() -> void:
	$animated.play("idle")


func _on_radius_body_entered(body: Node2D) -> void:
	if body.is_in_group("Player"):
		$animated.play("Tribal scream")
	else:
		$animated.play("idle")

func _process(_delta: float) -> void:
	if $animated.animation_finished:
		if $animated.animation == "Tribal scream":
			$animated.play("idle")

animation_finished is a signal, it won’t be used in a useful way thorugh a if statement. You need to connect to the animation_finished signal, you can do this through the editor or the .connect function in code.

thanks! Could I get and example though?

You can connect “animated”‘s signal similarly to how you connected radius’ body_entered signal through the editor.

it only seems to reset the idle animation, how do I fix this?

func _ready() -> void:
	$animated.play("idle")


func _on_radius_body_entered(body: Node2D) -> void:
	if body.is_in_group("Player"):	
		$animated.play("Tribal scream")

func _process(_delta: float) -> void:
	if $animated.connect("animation_finished", _on_radius_body_entered):
		$animated.play("idle")

Similar to the signal $animated.connect(...) should not be used in a if statement like this, you are checking if the connection was a success, again not checking if the signal fired. Furthermore you are trying to connect the signal to the wrong function, every single frame.

If you want to use .connect you should do so in _ready, but I highly suggest using the editor in this case, exactly the same way you set up the _on_radius_body_entered function by selecting “animated” and going into the Node tab to connect the signal.

thank you!