Day 24 | Creating Enemy Explosions

Ted Lim
3 min readApr 22, 2021

After implementing the functional components of your game, you’re now interested in adding VFX to some of your game’s actions. In this article, we’ll use the 2D Space Shooter project I’ve been working on to demonstrate how to spice up shooting enemies with some explosion VFX.

Getting Started with Enemy Explosions

In my 2D Space Shooter project, I have a nice explosion sprite animation for when the enemy spaceship is destroyed. I want the explosion animation to play when a laser or player destroys an enemy. There are a couple of ways to achieve this effect, but I’ll use an animator controller and a transition parameter to create the effect.

Setting up the Explosion Animation

Since this is the enemy’s animation, we’ll select our Enemy prefab, select the Enemy gameObject, open the Animation window and create an animation called Enemy_Destroyed_anim. After creating the animation and its respective controller, we’ll select all the sprites for the explosion animation and drag and drop them into the Enemy_Destroyed_anim dope sheet.

Now, when you hit the play button in the animation window, the explosion animation will run. Next, we’ll open the Animator window and double-click the Enemy_destroyed controller in the Enemy gameObject’s Animator component.

In the Animator window, we’ll create an ‘Empty’ state by right-clicking in the Animator window and creating a new state. We’ll make the ‘Empty’ state the default state by right-clicking the ‘Empty’ state, and then, we’ll create a transition from the ‘Empty’ state to the Enemy_Destroyed_anim by right-clicking it again.

With the states prepared, go to the parameter tab on the left and click the ‘+’ button. We’ll create a trigger and call it OnEnemyDeath. We’ll also add this trigger as a condition in the Inspector window on the right. Our script will use this trigger later to turn on the explosion effect. Finally, in the Inspector window, we’ll turn off the Exit Time and set the Transition Duration to 0 since we want the animation to play right after the enemy is destroyed.

Activating the Animation through Code

With the animation states setup, we can now go into our EnemyScript and find where the enemy dies. In this game, the enemy dies when either the player or a laser collides with it. Therefore, right before we destroy the enemy, we’ll refer to the enemy’s Animator component and turn on the trigger for the explosion animation. We’ll also reduce the enemy’s speed to 0 and delay the destruction of the enemy to let the explosion animation complete. We can do that by adding the following lines of code.

With that added, we can now run our game to see the explosion animation in action.

--

--