Shooting an enemy triggers all instances of the same type to play an animation simultaneously

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By meroborou

Hello everyone.

I’m currently very new to Godot and I’m figuring out how to implement basic FPS mechanics. So far I’ve added:

  • a controller (moving, jumping and dashing)
  • a simple shooting mechanic, where a raycast checks whether the collider is an enemy object or not
  • enemy AI which currently just chases the player.

I added a quick animation, titled “enemy_hit”, to show that the enemy object has been hit. It consists of changing the material (albedo) to a more intense shade of red for 1/3 of a second. The problem is that every instance of that object plays this animation simultaneously.

In my enemy scene, called “EnemyTarget”, I have a function which runs whenever there’s a collision between the object and the player’s raycast when firing:

signal health_drained 
...
func hit(damage_output):
    _set_health(damage_output)
    emit_signal("health_drained", self)
    if (health == 0):
	    emit_signal("health_depleted", name)

The health_drained signal is sent to the main scene. I store the enemy nodes in an array (and I know it’s probably not optimal, but it’s a temporary solution), so when the signal is emitted, I search for the index of the target node and play the animation afterwards. the code is as follows:

func _on_enemy_hit(enemyObj):
    var enIndex = enemies.find(enemyObj, 0)
    enemies[enIndex].get_child(1).get_child(0).play("enemy_hit")

So… any ideas? I have no idea how to make it so that only one instance plays the animation. Maybe it has to do something with the material?

:bust_in_silhouette: Reply From: Inces

Yes, it is only about the material.
Get to material in editor, find resource property, open it, and in its submenu tag local_to_the_scene

By the way, why can’t You just :

func on_enemy_hit(enemyObj) :
        enemyObj.get_child(1).get_child(0).play("enemy_hit")

Why would you go all the way to find this object in array ? :slight_smile:
And why won’t enemies handle their own animations ?

Thank you for the answer!
They originally handled them on their own, but i thought that finding the specific collider in the array would fix this problem. I’ll put that command back in their own script.

meroborou | 2022-10-08 13:11