I hear all the things.

Chance Cornell
4 min readJul 23, 2021

In today’s article, we begin to add audio to our game. Let’s open up our project and create a new empty game object and call it ‘Audio_Manager’. Let’s add an ‘Audio Source’ component to this game object. Just for organization, I want to create an empty child game object named ‘Background’. I’m going to add an ‘Audio Source’ component and drag my background music file in the ‘Audio Clip’ field. Make sure ‘Play On Awake’ and ‘Loop’ are checked.

If you play your game now, you will notice that you have a background track. The game already feels much better just from the audio being added.

On to adding laser audio. On the ‘Player’ game object add a new ‘Audio Source’ component. Turn off ‘Loop’, since we only want the sound to play when we press the button.

Now, open the ‘Player’ script and create two new variables. The first, a private ‘AudioSource’ and name it ‘_audioSource’. The next, a serialized private ‘AudioClip’ named ‘_laserAudio’.

In ‘Start’ we will reference our ‘_audioSource’ variable and null check it. We will add an else statement to the null check to set the audio source clip to be our ‘_laserAudio’.

Now, on our ‘ShootLaser’ function, we will have the audio source play after the if statement that we instantiate the lasers.

Back in Unity, highlight your ‘Player’ game object and drag your laser sound file to the ‘Laser Audio’ field. Now test your game and see if your laser plays as well as your background music.

We’re going to add an explosion sound to our enemies when they get hit by a laser or by the player, as well as an explosion sound to the asteroid when it is destroyed.

We will run through this fairly quickly since they are similar to what we have been doing already in this article. Add an ‘Audio Source’ component to both the ‘Explosion’ and ‘Enemy’ prefabs. Go ahead and drag the audio file you want to use to the ‘AudioClip’ on both prefabs. You want ‘Play On Awake’ active on the ‘Explosion’ prefab, but not the ‘Enemy’ prefab.

If you played right now, you would notice that the asteroid audio would work already, but not on the enemy. That’s because we will have to add an ‘Audio Source’ variable to the script and we will name it ‘_audioSource’ like before. This is exactly like the player audio. We will play the audio source clip right before we destroy the game objects in the ‘OnTriggerEnter2D’ function.

Right now, all audio should be working. The damage to the player is using the audio from the enemy when the two collide. Play and see what happens now.

Lastly, we want to add audio to our powerup pickups. This one is a bit more tricky. It’s not as obvious as we would initially think. We will not need an ‘Audio Source’ component on any of the powerup game objects. We will open up the ‘Powerup’ script and add a new serialized private ‘AudioClip’ named ‘_audioClip’. When we go back to Unity, we will need to assign the clip to that field. Next, after we get the player script in our ‘OnTriggerEnter2D’ function, we will access our ‘AudioSource’ and call ‘PlayClipAtPoint’. We will then call the ‘_audioClip’ variable at the object's position. It will look like this when done.

Go play your game and hear all the sounds. You might have to lower the volume of the background music as it is much louder than the other clips.

We’re almost done. Just a few more things and we will build our project.

--

--