2D Shooter: Exploding Enemies

Objective: Animate an explosion sequence when and only when the enemy gets destroyed.

Its about time to really start making the game look appealing to play, and one way to do this is by adding special effects! Let’s start by making the enemies explode when they die.

The animation itself will work pretty much like what we’ve done with the powerups and stuff, like what we did here. Once that’s done, we’ll need to adjust some things so the animation doesn’t play automatically.

First, click on the animation and in the inspector, make sure the box next to “Loop time” is unchecked.

Now in the controller, we want to create a new empty state by right-clicking in the space to access the options. Then, rename it to what you want and make it the default state.

Now right click it again and select “make transition”. There should be a line going from that empty state to your cursor, click on the “Enemy_Destroyed” state to attach it. Click on the transition line and make sure Exit Time in the inspector is unchecked and transition duration is set to zero.

Now its time to add parameters. This should be on the left of the controller. Click the plus button, and select “trigger”.

Name it “OnEnemyDeath” and make sure its unchecked.

Now go to the enemy script. Start by making a variable for our animator. Then assign the animator using GetComponent in the start method.

Now its time to set the trigger so then animation will actually start. This should be in two places: when the player hits the enemy, and when the laser hits the enemy. We also want to make sure the enemy stops moving when its hit by setting speed to 0, and make sure the enemy isn’t destroyed until the animation finishes playing. You can do this by adding a float number after “gameObject” in the “Destroy()” command.

Now everything should be working!

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!