We have played enough with collisions and coding to know how to destroy an enemy object. It works and that is what is most important. Now that we are a little further along, however, it couldn’t hurt to spice it up a bit.
We are going to add an explosion animation to our enemies being defeated. The enemies in your game may not always be a spaceship or some other vehicle that would make sense to explode in a fiery burst, but this animation change of animation and timing are still very useful for other needs. Besides, Zelda enemies explode with purple smoke and it looks pretty cool.
You are going to need an enemy prefab and some sort of explosion spritesheet or animation. Go to your enemy prefab. Use the Animation window to create an Animator for the enemy prefab if it does not already have one. Drag all the sprites of the explosion into the window to create the animation.
Go to where you saved the new animation and make sure to turn off the ‘Loop.’ Explosions happen once after all. Now we need to check on the animator controller itself for the enemy. If it was your first animation for the enemy, you’ll notice an issue here. If the explosion is the default animation, it will automatically happen as soon as the enemy appears. That is not what we want.
Create a new empty state by right-clicking in the Animator window. Then right-click that state and make it the default animation. Make a transition from the new default state to our explosion animation. If we leave this as is, this will only delay the explosion automatically playing. So we need to figure out how this transition should happen.
The best thing to suit are needs is a trigger. Still in the Animator window, add a in the top left. Add a trigger and name it some that suits are needs. ‘EnemyDefeated’ for example. Click on the transition arrow between the default and explosion. In the Inspector you’ll notice you can add a condition for the transition. Since we only have one parameter, it should pop up right away when clicking the drop down menu. To smoothen this transition, you should also turn off ‘Has Exit Time’ and zero any duration or offset.
Heading back into code now, open up the Enemy script. We need a reference to that Animator. With the variables declare a private Animator. You can finish the reference in Start() with GetComponent<>.
Once that is complete, go to the place in your script where the enemy is destroyed. Above the destroy call add anim.SetTrigger(“EnemyDefeated”). Obviously replace ‘anim’ and ‘EnemyDefeated’ with whatever you named the Animator variable and the trigger condition, respectively.
If we run this code as in, we’ll still have one more problem. The enemy will be defeated before the animation runs. Luckily, the Destroy() function has a second optional parameter that allows us to delay. So in your Destroy() make sure to add enough time for the animation to play out.
There it is. A gorgeous enemy explosion. With this technique, you can try all kinds of different ideas. Have fun and keep working hard.