How do I play animation on specific instance only?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nokungfu
:warning: Old Version Published before Godot 3 was released.

I have a player and a few enemies. When the player shoots and the bullet collides with the enemy, I want to play a hit animation on the enemy.

Simplified code:

func _on_shot_body_enter(body):
	print("shot enemy", body)
	body.hp=body.hp-1
	body.get_node("body/anim").play("hit")

The collision part works, and the individual hit points work. However; ALL of the enemy instances play the hit animation, not the specific individual instance that was hit. In the Output I get: shot enemy[KinematicBody2D:605]

I tried a few different tactics, even using a hit function and calling

self.get_child(0).get_child(0).play("hit")

but still have all enemies trigger. I suspect it is because of the get_node part, but I am not sure how to trigger only the instance hit’s AnimationPlayer.

That’s weird, the code you posted should work fine to trigger the animation of the specific enemy instance that was hit, and not of the other instances.

How did you setup your scene tree?

Akien | 2016-05-12 08:38

Thanks Akien,

I started adjusting hierarchy of things, but was still having the same result, so I turned to the animation and found my mistake:

When playing the hit animation, instead of keyframing the sprite’s Texture property (.:texture), I keyframed the sprite’s ImageTexture property (.:texture:image).

face palm.

nokungfu | 2016-05-13 04:03