Explosions — Enemy & Asteroid

In a previous article, we created an enemy that spawns and can be destroyed by the Player, but they disappear when they die. We are going to give them an explosion animation when they die to make it look awesome. Also, we are going to set the Spawn Manager not to spawn till we trigger it.

The first thing to do is create the animation, animator, and animation controller on the Enemy Prefab’s sprite. I went over this in a previous article, but you can watch the GIF below.

After you have made the animation with the Enemy Sprite selected, go to the Animator window, right-click, create an empty state, name it “Idle”, and right-click it, and Set as Layer Default State. That prevents the Enemy Sprite from playing the Explosion animation from the start by setting the empty stat Idle as the default. Next, right-click on the Idle state, select Make Transition and then click on the EnemyExplosion_anim state. A Transition is how you can go from one state to another, and you can also assign the Transitions conditions called Parameters.

Now go over to Parameters, click the plus button, select Trigger and name it “Destroyed”. Click on the Transition to select it, then go over to the Inspector and click the plus button under Conditions. Since Destroyed is the first Parameter, it gets picked automatically. Doing this will make it so the Enemy Sprite will only play the explosion animation when it receives the Destroyed Trigger. A Trigger Parameter is like a button press; it is only active for one frame.

In the Enemy script, add a private variable of the type Animator with a null value. In Start, set _anim to the Animator component on the child of Enemy the Enemy Sprite and then null check it. This reference is how we will tell the Animator to Trigger the Destroyed Parameter.

In OnTriggerEnter2D, in both, if statements checking the tag of the other collider, activate the explosion animation by using SetTrigger method on _anim and pass in the name of the Trigger you want to set off whish is “Destroyed”, and set the _speed to 0 so that the Enemy stops moving. Finally, add a 2.6-second delay to the Destroy, which will make sure the animation can play through all the way.

The enemies now stop moving and explode from both the Laser and collision with the Player.

Now to create a way to trigger the Spawn Manager to spawn enemies by blowing up an asteroid. Drag in the Asteroid, move it in front of the Player, then add a Circle Collider 2D and Rigidbody 2D, set Gravity Scale to 0 and turn on Is Trigger. Create a script named “Asteroid” and attach it to the Asteroid.

Next, create the Explosion prefab with the animation.

In the SpawnManager script, create a new public method named “StartSpawning” and move the StartCoroutine for both Coroutines. That is what the Asteroid script will call when destroyed. Then add a 3-second yield to each Coroutine before the while loop.

In the Asteroid script, create a private float named _rotSpeed with a value of 5; this is the speed at which the Asteroid will rotate. Create a private GameObject named _explosionPrefab with a null value; this will hold the explosion that will spawn when the Asteroid is destroyed. Create a private SpawnManager named _spawnManager with a null value; this will reference the Spawn Manager. In Start, get the reference to the SpawnManager script on the Spawn Manager and null check it.

In Update, we will rotate the Asteroid with transform.Rotate by Vector3.forward (1 on the z-axis) multiplied by _rotSpeed and Time.deltaTime; this will rotate it on the z-axis at 5 degrees a second. Then create an OnTriggerEnter2D method, then inside it, make an if statement that checks if the other collider’s tag is “Laser”. In the if statement, destroy the other gameobject, Instantiate the _explosionPrefab at the Asteroid position, call the StartSpawning method on the _spawnManager, and destroy the gameobject after 0.5 seconds. That will destroy the Laser, spawn the explosion, start the spawn Coroutines, and destroy the Asteroid.

In the Explosion script Start method, have it destroy the gameobject after 3 seconds. That will clean up the instantiated explosion after it has finished the animation.

Hook up the Explosion Prefab to the Asteroid.

Make sure the Exlposion_anim has Loop Time turned off, so it does not loop.

With that, the Asteroid spins around the z-axis, explodes, and starts spawning enemies and powerups when it is destroyed.

In the following article, we will add visual effects to the Player, like thrusters and damage smoke trails.

Unity Developer, Software Engineer, Game Designer