2D Explosions

As the space shooter I have been going through is …ehm..inspired by true events..ehm… it would be very unrealistic if we did not see some fire when enemies are shot down. Let’s go over the implementation of the explosion effect.

The explosion effect for the enemies works similarly to the other animations in the project and therefore they are created the same as in this article.

There is one difference tough, which is that we do not want the animation to start as soon as the enemy is instantiated. The first step to prevent this is to open the Animator window for the enemy and add an empty state:

Select the new state and rename it. Set the new state to be the Layer Default State and add a transition to the explosion state. When you start the game now the default “animation” will be the empty state instead of the explosion animation:

To be able to use the transition from the empty state to the animation we need a parameter to use. This can be either a float, int, bool or trigger. As we will not change anything else after the animation has run, a trigger is sufficient in this scenario. Add a trigger and change to a suitable name:

As we do not want the explosion animation to start over after it has run, we should also turn off looping:

To ensure that there is no delay in starting the animation, disable Has Exit Time and set transition duration to 0:

Open up the Enemy script and add a private Animator. Assign the Animator in the Start function:

In the collision function call the SetTrigger function on the Animator and pass in the name of the trigger. Set the speed to 0 to stop the enemy from moving. Add a float to the Destroy call, which will delay the destruction. This will ensure that the animation has enough time to finish before the game object is removed:

Final result:

Good luck!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store