Creating Enemy Explosions

Day 25- Making 2D Space Shooter Game

Objective: To make our Enemy explode when destroyed.

To make your enemy explode, you first need to have an animation to play after the enemy is destroyed.

Lets start by creating a animation showing enemy explosion.

Select your Enemy, go to Animation and select enter, give the animation a name then you are ready for animation.

Select the record button(red button in animation panel) then drag your enemy explosion animation sprites. select record button again to stop recording and set your animation loop to inactive by selecting the animation and deselecting from inspector.

If you play the game as it is then the animation will start as soon as the enemy spawns so to avoid that we set the condition in the animator i.e. IsEnemyDead

Then go to your enemy script and create a Animator variable and use GetComponent to access the Animator component. Also it is good practice to check if the component is null or not so be sure to do that as well.

Now you can use SetTrigger to set your bool variable created in Animator to true which then will start the animation. Also delay the destroy to allow the game to play the animation as well.

Unity Developer