Hear the BOOM!

Last time we looked at how to add background music to our game to increase our player’s immersion. Now let's add sound effects! We’re going to add a laser sound when ever the player fires a laser.

Adding a Laser Sound

For this project we’ve been provided with a laser sound, so we’ll use that. If we had not been provided with the sound we could have gone to the Unity Asset store or another source like freesound.org.

We’re going to have the Player object be responsible for playing the laser sound when the user presses the fire key. To start we are going to need add an AudioSource component to the Player object in Unity. We’ll want to make sure that Play On Awake and Loop are both unchecked. Unlike our background music we don’t need to assign our audio clip to this AudioSource because we are going to assign it through code when we play it.

Player Audio Source

Next we will need a reference to the sound we want to play. We will store this in a serialized AudioClip field. We will also need to store a reference to the AudioSource component attached to the Player GameObject, so we will create a private variable for that as well.

Audio Properties

We’re going to go ahead and cache and null check our AudioSource using GetComponent();

Caching and null checking our Audio Source

Now, after null checking our audio source again, we can use the PlayOneShot method of the cached audio source to play the AudioClip that we set through the inspector.

PlayOneShot method call

This leaves us with the following in our Damage Method

Player Damage Method

Next we’ll add the explosion sound to our enemies when they die either by colliding with a laser or with the player. Just like the laser sounds we will need to have a reference to an AudioClip in our Enemy script. Unlike our player, this time we’re not going to attach an AudioSource to our enemy (though we could have used the same technique as above).

Enemy AudioClip
Enemy AudioClip

This time we’ll use the static method PlayClipAtPoint from the AudioSource class to play our clip when the enemy is destroyed.

PlayClipAtPoint method call
PlayClipAtPoint method call

Leaving us with

Enemy collision code
Enemy collision code

Next Time!

Now our player will hear lasers and explosions accordingly when these events happen in our game! Next time, we’ll look at building and testing our game! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.




Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter at OY! Indonesia: Migration Strategy and Architecture

Agile SDLC: Skyrocketing Your Project with Agile Principles

Applying Unit tests to MVVM with Swift

What’s new in HumongouS.io — Feb 2020

Haskell with VsCode DevContainers on M1.

Using Unity and git together

Week Two: “Dream is Collatzing”

Go — Builder Pattern

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
Christopher West

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

More from Medium

Cinemachine: Switching Cameras

A guide to setting up a cross platform input controller using the Unity Input System

Instantiating & Destroying Game Objects in Unity

Asset Bundles for Unity