Hello everyone! Today is another day for game dev! This time it's about having the enemies explode when destroyed.
Let’s check out the asset we are going to use.
The objective is to have the animation play when the enemy is destroyed.
- Make sure we open the enemy prefab. We have our enemy Sprite Renderer and Animator component within a child object. So let’s select it, and head over to the Animator window.
- We create a new clip in the Animator window. Name and save the clip to a destination folder in your assets. In this case, I named the clip for this guide “Enemy_dest_med_ex.” It stands for “enemy-destroy-medium-example.”
- Select all of the frames that make up the enemy explode. Drag and drop them into the dope sheet animator window.
- In the Animator window, not the Animation window, we need to double-click the clip we created and make sure “Loop Time” is checked off in the inspector. If checked, your animation will loop forever!
- In the Animator window, we need to create a new empty state. Why create a new state? It’s because our animation clip will play automatically, and that is a behavior we do not want. Right-click the empty state and set it as “Set as Layer Default State.” Then, Right-click on the empty state and make a transition to the clip.
- We need to create a new parameter of type “Trigger” in the Animator window to trigger the animation when the enemy is destroyed.
- We select the transition line between the empty state and the animation clip so that we can have access to the inspector. Under conditions, we select the trigger parameter we created.
- Above the condition, let’s make sure “Has Exit” time is checked off. It will play the animation clip immediately without having any delays. Under settings, we also want to make sure “Fixed Duration” is checked off and “Transition Duration” has a value of zero.
- Let’s open up the enemy script and create a reference for the Animator component where the clip is stored.
private Animator _animator = null;
We want to cache it before the game starts in the
Awake() function. We use
GetComponent<>() to communicate between scripts! We also have to check and see if the object is null or not. If it is, we need to let Unity give us a warning.
private void Awake()
_animator = GetComponent<Animator>();
if (_animator == null)
Debug.LogError(“The Animator is NULL”); _player = GameObject.Find(“Player”).GetComponent<Player>();
if (_player == null)
Debug.LogError(“The Player is NULL”);
- We then have to access the animator and set the animation trigger in
OnTriggerEnter()before the enemy is destroyed. In this snippet of code, we are saying:
If the enemy is hit by a projectile game object, then destroy itself.
Destroy(other.gameObject); if (_player != null)
_speed = 1f;
In bold letters above, we set the animation trigger for the clip to play upon the destruction of the enemy!
There was a slight pause during the beginning of the enemy explode animation clip that was bothering me. So I took the liberty to delete the first eight frames to have a smoother animation play.
And here are the results of the steps! We now have our enemies trigger an animation clip of then exploding!
How cool is that?!
That is all for today! Thank you for reading!