Damage VFX using Animated Sprites in Unity

Brought to you by ACME

The work on our 2D space shooter continues. The space shooting is feeling good. Now it’s time to get it looking good. And nothing looks gooder than a good-old-fashioned explosion. Time to make the game go boom.

I covered simple animations in a previous article. This time I’ll dig a little deeper and set up some triggered events. When you first create a new animation, the controller defaults to something like this:

The animation is played immediately, and loops forever. Neither of those things are good for our enemy explosions. Well, unless you really like explosions. Instead, I want the animation to play only when the enemy is destroyed, and only once.

First, we need to set up the elements of the animation we will be referring to in code later. There needs to be a trigger, first and foremost. On the Animator window there are two tabs: click the one marked “Parameters” and click the + to create a new bool parameter. I’ll call this one OnEnemyDeath.

I will create an empty state, and make that the default state on entry.

Then, I’ll right click on the empty state and create a new transition, linking it to the Enemy_Destroyed_anim. Once that link is made, click on the link and examine it in the Inspector window. Under Conditions click the +, and since there’s only one parameter it should automatically select OnEnemyDeath.

Next, our Enemy Object will need an AudioSource component. No need to adjust any settings.

Over in the Enemy script I’ll create variable handles for the AudioSource and Animator components, and a serialized variable for the explosion audio clip.

Finally, we’ll add four lines to the OnTriggerEnter2D method for each of the two possible ways our enemy can blow up.

So, we set the trigger of OnEnemyDeath, load the audio clip into the AudioSource and play it. At the bottom you’ll notice I also added a new element to the Destroy command — since my enemy explosion animation is about 2.5 seconds long. Without this wait time the object would self destruct before the animation had time to start.

Boom!

Looks great.

In the next article we’ll make the game look more professional by adding some post processing to the scene.

Unity Developer / Game Developer / Artist / Problem Solver