Enemy Explosions
LIGHTS AND SOUNDS!
One of the things I love most about Video games is the immersive experience. The vibration in the controller, the backstory and cinematics, the levels and characters. A huge part of that experience is the visual and sound effects.
To get the enemy explosion right, we are going to need 2 parts. First, we need the animation; and second, we need sound.
I’ve covered the process for creating animations in previous articles. For this article we are going to focus on the sound aspect.
Before we get started, it is important to note that our main camera contains an Audio Listener component. This allows it to “receive” the sounds from audio source components.
I have two different explosions that occur in the game.
The first one is the asteroid explosion that starts the game.
For this explosion, we will create an object called explosion. We’ll add the animation and sound to the object, and use our scripts to destroy the object and start the game.
Here, we have our explosion Prefab with our explosion animation attached.
Let’s add an Audio listener component. In the Audio Listener we can assign the sound effects for our explosion.
In our audio folder we have the audio clip for the explosion. Once the Audio source is added to the prefab, open the audio folder and drag the explosion audio clip into the AudioClip reference box.
Moving over to our Asteroid script. Let's create a game object variable for our explosion prefab.
If we add the serialize field attribute, we can assign the reference from the inspector.
Going back to the asteroid script, we need to create the logic for the explosion. If the asteroid is destroyed, then trigger the explosion and start the game.
We’ll use the ‘on trigger enter’ method to check for a collision between the asteroid and the laser. If that happens, we instantiate the explosion prefab, destroy the laser, destroy the asteroid and start spawning enemies. It will look a little like this…
When we instantiate the explosion prefab, we spawn it at the position of the asteroid (transform.position), and we are not concerned with the rotation (Quaternion.identity).
Don’t forget to create a handle to the spawn manager and cache the reference in the start method. Without this, the _spawnManager.StartSpawning() method call will do nothing.
Next, we need a script for our explosion prefab. This script will run when the prefab is instantiated. Since the audio source is already on the prefab, and our clip is attached to the source, it will play when the object is instantiated.
For our script, all we need to do is destroy the object after the clip has played. for this we use the destroy method on start. we pass in this game object since we are on the explosion prefab and delay the destruction by three seconds so the audio clip can play.
On the explosion prefab, in the audio source component, make sure the box is checked for “Play On Awake”.
AND we’re done!
NOTE: You may have noticed that we passed a 0.25 second delay when destroying the asteroid game object. This is because light travels faster than sound. We see the object disappear from the screen before the sound happens. You may have to adjust the delay for your own aesthetic.
NOW FOR THE ENEMIES:
For our enemies, we are going to do things a little bit different. We are going to be working on our enemy prefab and enemy script.
First, we need to add the audio source component to the enemy prefab. Select the enemy prefab from your prefabs folder in the projects tab. In the inspector window, scroll to the bottom and select add component. serch for audio source and select it.
Next drag your explosion audio clip into the audio clip reference block on the audio source component.
Now lets move over to our enemy script.
The first thing we need is a handle to our audio source component.
Next, we’ll cash the reference in the start method using the ‘get component’ function.
Now that we have our reference, the question is… when do we play the explosion sound?
Answer: we play it only when the enemy is destroyed. That happens on two occasions. The first is when our enemy is hit by the laser. The second is when our enemy and player collide.
But, before we do that, let's make sure our animation is set up correctly.
If you haven’t already, create your enemy explosion animation. I named mine “Enemy_destroyed”.
With the asset pack that I am using, we have a specific animation for the enemy explosion, in addition to the general explosion animation used for the asteroid.
Add the animator component to the enemy prefab and attach the animator controller to the reference.
Now that we have our prefab set, let’s go back to the script.
We already created a handle for our audio source. We will also need one for our animation.
When you first open the controller, it will look like the one below. the animation is orange because Unity has identified it as the default state. That means that as long as the object is active, the animation will play. That is NOT what we want. We want the animation to play only when we tell it to.
on our animation we added a parameter called “OnEnemyDeath”. We use this parameter to trigger the animation for the explosion.
Open up the animator controller. On the left-hand side there is a tab for parameters. Select that tab, click the plus icon and select trigger. Name the trigger.
To function properly we added an empty state as the default state. We use our trigger parameter to transition to the animation.
For the empty state, right click inside the controller and select ‘create state’ and ‘empty state’.
to make transitions right click on the box and select ‘make transition’. Drag the transition to the animation box. If you click on the transition, it will turn blue and option will appear in the inspector. Under settings you can add conditions to the transition. Add the “OnEnemyDeath” trigger.
In our script, go to the OnTriggerEnter2D method. Use the animator handle to set the trigger for the animation. We set speed to zero to stop the enemy when it is destroyed. Then we use our audio source handle to play the audio clip. And last, we destroy the object (with a delay long enough to play the animation and sound FX).
We do this for the player collision AND the laser collision.
I ran into an issue where the enemies were damaging the player during their animation. setting the speed to zero helped, but ultimately, I decide to disable the collider on death.
For that I created a handle to the collider2D component, and I used “Get Component” to assign the reference at start.
This function was only used when the enemy collided with the laser. This prevented any damage to the player if the laser collision happened prior to the player collision.