Creating Enemy Explosions

Visual Effects! One of the crucial elements that makes our game to be felt more real. Changing the game from prototype to a game with assets with a thing, but it will never feel complete without proper effects.

In Unity, we can either apply visual effects onto game objects or to the main camera. Unity has some ready effects provided for us that can be added to the game objects as a component, namely: Particle System, Trail Renderer, Line Renderer, Lens Flare, Halo, Projector and Legacy Particles. Despite, in this article, I am going to focus on the effects that are created with animations.

I have actually never enjoyed working on visuals, but testing-trying out the effects for the most real outcome has really given me a joy. Spaceshooter game has already provided us necessary assets in order to create proper animations that will fit in our game — thanks Al Heck!

The enemy sprite, has already came in with multiple sprites, that allows us to create an animation of a spaceship exploding (You can find my article regarding creating animations here). Certainly, we want the enemy to explode once it has been shot, so that we cannot just make it play the animation. In order to make this possible, we select our Enemy object and create a new animation namely enemy_explosion_anim, and drag the relevant sprites to create the animation. Do not forget to uncheck “Loop Time” from the animation settings as the enemy will be destroyed once it plays the animation. Since this will be the one and only animation attached to the enemy game object, it will start to playing as we start the game. Certainly, we do not want the enemy to explode until we actually shoot it.

We can control the animations of our game objects by using Animator window. The arrows in between the states are transition arrows, that control the conditions for switching between different animations. In this case, since the enemy has one and only animation state, it enters the destroy state as the game starts. In order to make the enemy to live until we shoot it, we actually need an empty (or idle animation if you have it) state.

To create a new state, simply right click in the animator and select empty. Now, we need to assign this state to be the default state so that once the game starts, animator will switch to this state automatically.

Right click on the Empty state and select “Set as Layer Default State”

As we want the explosion to occur after the Empty state, we create a transition arrow by right clicking on Empty State — Make Transition. And select the enemy_destroy_anim state.

If you click on the arrow, and select “Settings”, you can observe and change the transition settings. “Has Exit Time” will be checked by default. “Has Exit Time” denotes that the transition will be made after the provided exit time finishes (you may not want to split an animation at half). We uncheck it if we want to switch to the next state instantly.

To assign a condition for the transition, we can add a float-bool or trigger by clicking add parameters. In our case, we created a trigger that we are going to call once the enemy is dead.

We then add the trigger as a condition in the settings of the animation.

So, how are we going to call this transition? As I have mentioned in the previous article about animations, Animator is responsible for controlling our animations. Unity automatically adds Animator component to the game object that we created an animation. By referencing it within the script, we can easily manipulate our game as we want to.

Do not forget to null check

Once we have successfully referenced an instance of the animator component, we can call it once the enemy is going to be destroyed. In order to call the trigger, animator component has a ready function called “SetTriger”. (You can also use SetFloat, SetInt, SetBook methods according to the condition you have chosen in the Animator window).

Latest state of the enemy script is provided below. As you can observe, we have called the trigger by providing the string. Thus, you need to be careful about typos as the animation will not be triggerred in that case.

Unity Developer