Adding Audio to our Prototype

Audio isn’t the first thing that comes to mind for making games, at least not for me.

But it is extremely important!

Not only does audio give the player much needed feedback for their actions, but it sets the mood and atmosphere of your game.

Take any movie clip with it’s original music and then run that movie clip on mute with a totally different type of music.

Do you feel the difference? Its palpable.

We’ll start by creating an AudioManager game object and a BackgroundMusic game object parented to it.

Add the Audio Source component to the BackgroundMusic game object and drag the music_background audio file into the AudioClip field.

Make sure Play on Awake and Loop are checked.

Adding the laser audio clip

When the player fires, we want a laser sound effect. Since our FireLaser() method is on our Player script which is on our PlayerGO, we’ll add a new audio source to the Player GO prefab.

Turn off Play on Awake.

On our Player script, we need to add some variables. We’ll need an AudioSource to play the sound and an AudioClip to choose which sound to play.

We’ll drag our audio clip for the laser shot onto our new LaserAudioClip variable in the Unity editor on the Player game object’s Player script component.

We update the Start() method with an assignment of the _playerAudioSource variable followed by a null check, and another null check for the _laserAudioClip.

Last we update our FireLaser() method to assign the clip to our AudioSource and then Play it.

Run it…

Nice, now let’s move on to our explosion sound effect.

Adding sound to explosions

First, we need to add an audio source component to our Explosion game object prefab.

On our Explosion script, we need to add the necessary variables, update Start() method with null checks, assign the AudioSource, and play the audio clip.

The only time this script is alive is during an explosion, so we just run the explosion sound at start.

Don’t forget to assign the explosion_sound audio file to ExplosionAudioClip on our Explosion script component.

That takes care of our exploding asteroid, but our enemy game object prefabs don’t create an explosion game object prefab on death.

They animate right on the enemy game object, so none of what we did above applies.

Adding explosion sound to enemies

The first thing we have to do is add the AudioSource component to our Enemy game object prefab.

We add our Audio variables once again.

Assign the explosion_sound audio file to our EnemyAudioClip variable on the Enemy prefab game object’s script component.

Once again we update the Start() method and do our null checks.

And last, we update the OnDeath() method to play the audio clip.

Enemy explosion audio should be working now, so give it a shot.

Adding sound to our powerups

Add an audio source to each powerup game object prefab and make sure to turn Play on Awake to false.

Add our audio variables.

We drag and assign our audio clip to the script component on our Powerup game objects.

Update our start method with null checks and assignment.

Last we update our OnTriggerEnter2D() method.

We add the audioclip assignment and play call, but we also add some more code.

By disabling the SpriteRenderer we can make the powerup on screen disappear while the destroy call is now set to wait 1 second before destroying the powerup game object.

That’s it, we’ve added sound to our game!

In the next article we’ll cover Deployment of the game.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store