Day 26: Creating Enemy Explosions

Objective: Do explosion animation when enemy is destroyed.

We already have the sprite sheet for enemy explosions. The next thing to do is create the animation with unity. The same way we animated the power ups is the first step. Go into the Enemy prefab and open the animation window. Click create name the animation and save.

Grab all of the sprites for the animation and drag into the animation window.

With everything loaded and you hit play the explosion happens right away. That’s not what we want. To fix this we will start by going to the Animator window. Get there by going Windows -> Animation -> Animator.

This is where we can control when the animation runs. You’ll see that the animation you created is in orange. The orange means that it is the default animation and will run when the game starts. We need to right click and create state and click on empty.

We need to make the new state the default and rename it to Empty. You make it default by right clicking on it and selecting “Set as Layer Default State”.

Next is to get the animation to transition to the explosion animation. Right click on “Make Transition”, you’ll get a white arrow/line attached to the cursor, then click on the explosion animation. The white arrow/line with attach its self to the animation.

If we leave it like this, the explosion animation will still play automatically. What we need to do is set a trigger for the transition to happen. On the left side of the Animator window you will see Layers and Parameters tabs. We will move the the parameters tab. Click the plus sign and click trigger. Next is to name it.

Next is to click on the transition line between empty and the explosion animation. Here we need to set the condition. When ever the trigger is called the condition will be true and the animation will run. With the transition selected, on the right hand side is where we can set the conditions.

We need to have “Has Exit Time” unchecked. If we don’t it will finish it’s current animation before moving on to the next. We want the animation to play as soon as the enemy dies. I set the transition duration to zero so the animation will transition to the explosion animation quicker.

Now all the animation configurations is done. Next will be to call the trigger. In the Enemy script we need to get a reference to the animator.

Next is to set the reference by using GetComponent. We don’t have to find the object due to the component is on the same object as the script. And it’s always good to null check after calling GetComponent.

It’s time to call the animation. Where the enemy is destroyed we will call the SetTrigger function. We’ll type out “_anim.SetTrigger(“OnEnemyDeath”). The string that is passed into SetTrigger needs to be spelled and capitalized the same as the parameter that you created in the animator window.

I put a delay on the enemy being destroyed by adding a time in the destroy function. This is to let the animation finish running before it is destroyed. To make it look a little better we will set the speed to zero so the explosion doesn’t move down after the enemy is destroyed.

That’s it! We created an explosion animation that runs any time a enemy is destroyed.

I am passonate on learning to program and use Unity to become a skillful Unity Developer