# Animation after enemy spawns

right now in my game, four enemies spawn in four spawn points with a delay, but what I want to do is that if you don't kill the enemy after a couple seconds, an animation (it's an animation of a scratch, basically to let the player know it has been attacked) appears.

Right now I have a script that display an image (it's an image because I was testing the scripts), but now I want to change the image for the animation but after trying and failing a lot of times I decided to ask here, and hopefully someone can help me.

This is the script of the "attack", and it's attached to the enemy prefab:

public Image TargetImage;

void Start ()
{
TargetImage.enabled = false;
}

public void spawnAttack(){
StartCoroutine (playerIsAttacked (5, TargetImage));
}

public IEnumerator playerIsAttacked (float t, Image im)
{
yield return new WaitForSeconds (t);
TargetImage.enabled = true;
}

}


and this is in the timed spawn script:

public class TimedSpawn : MonoBehaviour {

public GameObject spawnee;
public bool stopSpawning;
public float spawnTime;
public float spawnDelay;

void Start () {
InvokeRepeating ("SpawnObject", spawnTime, spawnDelay);
}

public void SpawnObject (){
GameObject newSpawn = Instantiate (spawnee, transform.position, transform.rotation) as GameObject;
newSpawn.GetComponent <enemyAttack> ().spawnAttack ();

if (stopSpawning) {
CancelInvoke ("SpawnObject");
}
}


}

• "after trying and failing a lot of times I decided to ask here, and hopefully someone can help me." what is the problem? Nov 22 '17 at 4:46

You need an Animator on your enemy object as well as an animation controller. You also need to create an idle clip and an attack clip for it, then an animation trigger "isAttacking".

Set the trigger on a timer of some sort, and set up your animation controller to play the attack animation when the trigger is set off.

https://docs.unity3d.com/ScriptReference/Animator.html

https://docs.unity3d.com/Manual/AnimationClips.html

https://docs.unity3d.com/Manual/AnimatorControllers.html

https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html

Here's the docs, otherwise find a simple tutorial on YouTube. It's not as confusing as I made it seem!

• Hey Jim, thanks for contributing to the website. Can you elaborate on the isAttacking, perhaps a small example? Nov 23 '17 at 20:47