My journey becoming a Unity game developer: Creating Enemy Explosions

Objective: Creating explosion effects at the location of each enemy destroyed. Also, when the explosion animations are playing, the enemies will not only be destroyed, but their behavior scripts and colliders will be destroyed to prevent them from being active in the game to damage the player.

Explosions playing when the enemies are destroyed.

To begin we need to open the Enemy prefab, and create a new Animation name Enemy_Destroyed. Add the explosion animation sprites into the dopesheet. A new Animator will be added to the Enemy prefab with the Enemy_Destroyed controller included in the Controller slot. Make sure the new animation clip created has the same name as the controller.

Enemy Destroyed explosion animation created.

In the Animations folder, select the Enemy_Destroyed animation clip and turn off Loop Time to prevent from looping over and over again. When we play the game, notice that the enemies are exploding automatically.

Enemy Destroyed animation plays automatically even with Loop Time turned off.

The enemies are exploding automatically because the Entry state is playing the Enemy_Destroyed animation clip as soon as the game starts. Therefore, we need to create a new Empty state name Empty, and set this state as the Layer Default State. This will have the Entry state start the game going through the Empty state before it can go to the Enemy_Destroyed state. When we play the game now, notice the enemies are moving on the screen in a normal manner.

Empty state created to shop explosion animation from playing instantly.

Now we need to create a Transition from the Empty state to the Enemy_Destroyed state. To play the Enemy_Destroyed animation we need to 1st create a Parameter to Trigger the animation to play name OnEnemyDeath. I made a mistake in naming it OnPlayerDeath, but I did fixed it after writing this article.

Click on the Transition arrow, TURN OFF HAS EXIT TIME which I didn’t do initially. With this turned on, the animation clip transition doesn’t rely on a parameter. Instead, it will rely on the normalized time of the state. Open Settings to make this transition happen at the specific time specified in Exit Time.

Down under Conditions, click on the “+” sign to add a new condition. The OnEnemyDeath trigger parameter will be added automatically to play the explosion animation when the trigger is activated.

OnEnemyDeath Trigger created for the transition between the Empty and Enemy Destroyed states.

To activate the OnEnemyDeath trigger, go to the EnemyBehavior script. We need to create 3 variable handles to the EnemyBehavior script, Collider2D, and Animator components. In the Start() function, make sure to use the handles to Get each Component, and to NULL check for each component.

Created handles to components attached to the Enemy object.

Inside the OnTriggerEnter() function, after the Player receives Damage() we will use the _anim variable to call the SetTrigger() method for the OnEnemyDeath trigger. Then, we need to stop the Enemy from moving down the screen invisibly by setting the _speed to zero. Also, we need to stop the Enemy’s behavior from performing such as shooting a laser at the player by Destroying the _enemy script component. Finally, remove the Destroy(this.gameObject) from the IF NULL check condition, and place it under the _enemy component being Destroyed. Destroy the Enemy between 2.5f to 2.8f by looking at the timing of the Enemy ship being destroyed and the explosion appearing. With the Laser condition, we need to add the Destroy _collider component before destroying the Enemy to prevent the collider from being active in the game to detect collisions while waiting for the Enemy object to be Destroyed in 2.5f seconds.

Explosions triggered when colliding with the Player or the Player’s lasers.

Play the game and notice the Player is able to destroy the enemies, the explosions triggered right as the Enemy ships are destroyed, and the Player isn’t receiving damage from any invisible Enemy ships.

Explosions playing without the player being destroyed from colliding into them.

We can improve the transitions between the Enemy_Destroyed and Empty states. With the Transition arrow selected, go to Settings and set Transition Duration and Transition Offset to zero so there will be no delays in the transition.

Play the game and notice the explosions play as soon as the enemies are destroyed.

Explosion transition with Enemy being destroyed more in sync.



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