Project 1: Space Shooter

How to make enemies explode

Adding some effects to the game

Eero Saarinen

--

Enemies are exploding left and right!

The next step of making the game more enjoyable is to add some visual effects to it, namely visualizing the enemy’s demise. The first step is adding the explosion animation. This shouldn’t be too difficult if you’ve read my previous articles.

After having the animation done, it’s time to make some changes to the animation and its controller. For the animation all there is to do is to disable the looping. The controller needs a bit more involved process.

Disabling the animation loop.

The default behavior is that the animation begins almost as soon as the object has been instantiated, which means that the enemies explode once they enter the scene whether or not the player shot them.

Enemies exploding on their own after instantiation.

To change this, I have to first double click the controller to open up the Animator view. There I can add a new state, name it empty and make it the default state. This way the enemies won’t explode once they are instantiated.

Then I have to make a transition from the empty state to the explosion state. If left as is, the explosion animation starts right after the empty state has run its course. That is why I need to decide on a condition of how or when there should be a transition to the explosion.

The simplest way, in this case, is to use a trigger. Once it is called it starts the animation, or at least that is what I want, but the transitions in Unity, as a default, have an exit time and transition duration. If these are left alone there is a noticeable delay before the animation starts, to correct this all there is to do is to uncheck the exit time and make the transition duration 0.

Setting up the animation and its trigger.

The last step is to make a call to the trigger once the laser or the player hits the enemy. I also set the speed of the enemy to 0.75 to have at least some sort of inertia after being hit, which also makes a nice looking effect.

A function called when either a laser or the player hits the enemy.

--

--