My 90 day journey becoming a Unity game developer: Day-28

Objective: Activating the enemy explosion animation using code.

Final version of the enemy explosion animation.

To begin, go to the Enemy script and create a variable that will be a handle to the Enemy’s Animator component. Inside the Start() function check to see if the player is null. Assign the variable to actually get the Animator’s component and also null check for the Animator’s component.

In the OnTriggerEnter2D function, when the player collides with the enemy we want to use the Animator’s component to trigger the enemy’s death explosion animation after the player is damage and before the enemy is destroyed. Also, when the laser collides with the enemy we want to trigger the enemy’s death explosion animation after the player scores, but before the enemy is destroyed.

In the Animator, make sure the enemy death explosion animation trigger is turned off.

As we watch the game play, we will notice the explosions are not playing. This is happening because the enemy is being destroyed right away, therefore the animation doesn’t have the time to play. Back in the Animator, go the the animation window and see how long the animation clip is. Over in the Enemy script under the OnTriggerEnter2d, change the time the enemy is destroyed to the length of the animation clip approximately.

Destroy enemies in 2.4 seconds which is the length of my animation.

Now if we play the game, the explosions are working but we have a new problem to fix. As the enemy is destroyed and the explosion is showing on the screen, the explosion is moving down the screen and can damage the player.

Enemy destroyed but can damage the player still.

That is happening because the enemy’s components are still active as it’s moving. There is 2 ways to deal with this problem, we can either destroy the enemy’s components or we can stop the enemy from moving down the screen after the enemy is damaged. I will use the latter to fix the problem. Set speed to zero to pause the enemy when they’re hit.

Enemy stops moving when they are destroyed.

The only way the player will be damaged now is if they run into the explosion themselves. There will be one more problem to fix if you play the game you will notice a delay in the enemy exploding. To fix this, go to the Enemy’s Animation Controller and click on the transition arrow. You will see Has Exit Time which means it will play one state’s animation completely before transitioning to another state’s animation. Turn off Has Exit Time to transition instantly to the death explosion animation, and set the Transition Duration to zero which will allow the transition to the next animation right away.

Adjustments made in the Enemy’s animator to make the transition between animations instantly.
Enemies explosions working properly after final adjustments.

Next time we will add an asteroid to the game.



Learning to become a Unity game developer.

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