Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Creating explosions for your game in Unity

A quick guide about how to create explosions using animated sprites in Unity

Objective: Create animated explosions for the enemies in a space shooter game with Unity.

In an older post I covered how to animate sprites in Unity, where I added a power-up item to be collected in the game. Now it’s time to animate explosions and trigger them when the enemy gets destroyed.

Creating the explosion

In order to create the enemy explosion, let’s start by opening the prefab view of the enemy:

Then, open an animation window at Window > Animation > Animation to create a new animation sequence:

Then, click on the create button and save the new animation with a proper name in your folder:

Then drag all the files from the sprite sequence into the animation view to assign them as keyframes:

Once assigned, you can test the animation with the play button and you’ll be able to watch it in the scene view:

If you run the game, you’ll notice that the explosions are working as soon as the enemies are spawned into the scene, so we need to trigger the animations from the Enemy script.

But first, let’s select the animation file and disable the animation loop to avoid playing the animation more than once:

Now, let’s double click the controller of the animation to open the animator view:

As you can see in the controller, the animation is played as soon as the enemy is spawned. To change it, we need to add a new empty state to point the default transition into it:

Then, create a new transition pointing to the explosion animation from the new state:

Now, open the parameters tab at the left and create a new Trigger parameter that will work as the trigger for the new transition:

The name of the new Trigger is OnEnemyDeath.

To assign the trigger for the transition pointing at the explosion animation we need to select it and add a new condition in the inspector. Also, don’t forget to disable the Has Exit Time option to avoid waiting time before triggering the transition to the explosion.

By disabling the Has Exit Time option, we make sure that the explosion animation starts without delay.

Triggering the animation from code

In order to trigger the animation from the code let’s open the Enemy script (that is attached to the enemy) and create a new variable to store the reference to the animator component (also attached to the enemy):

Then, let’s assign the reference to the animator component in the Start function and debug an error if there’s a problem after the assignation:

Now, in the OnTriggerEnter2D method, let’s change the trigger value by using the SetTrigger method from the animator and using the name of the Trigger as parameter:

If the enemy collides with the player or the laser, the value of OnEnemyDeath will change.

To avoid destroying the enemy before the animation is over, let’s add a second parameter of type float to the Destroy call, which will add time delay to the destruction of the object. If we check in the animation view, we’ll notice that the animation needs 2.38 seconds to finish, so let’s use that number as the new parameter:

Also, let’s set the speed of the enemy to 0, so that the explosion animation takes place in a static position:

Let’s create a new function to avoid repeating the same lines when the enemy collides with a laser or with the enemy:

As the player stills move after losing a life, we need to create a new condition that prevents the enemy to inflict damage again while exploding. We can make sure that the player doesn’t receive double damage by using a bool that indicates if the enemy is already destroyed in the death sequence:

And then, in the OnTriggerEnter2D method, let’s add the condition and return from the execution if the enemy is already destroyed:

Finally, we’ll be able to see the animation controller flow in the animator while running the game and selecting a spawned enemy.

The enemy starts by doing nothing in the empty state and then, when the trigger is called, it transitions to the explosion animation.

If we run the game in Unity, we’ll be able to see the explosions when destroying the enemies:

And that’s it, you can create explosions for your game in Unity! :D. I’ll see you in the next post, where I’ll be showing how to display damage in the space shooter game with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D



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
Fernando Alcantara Santana

Fernando Alcantara Santana


A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).