My journey becoming a Unity game developer: How to Play Sound Effects in Unity

Rhett Haynes
6 min readSep 8, 2022

Objective: Using Audio Sources and Audio Clips to play sound effects for the Player’s lasers, explosion effects, and collecting powerups. Plus, adding background music to play throughout the game. With the Explosion and Player_Explosion prefabs, they will have a Explosion script and Audio Source attached to play the explosion sound effects for the Asteroid and Player respectively. While, the Enemy prefab will use the Audio Source’s Play() method, and the PowerUps will use the Audio Source’s PlayClipAtPoint() method.

Powerups collected with sound playing.

The 1st sound we’re going to add to this game is Background music. Create a new Empty game object name Audio Manager that will serve as a parent to the background music. Reset its Transform position to zero, then create another Empty object name Background_Music inside the parent object. Create an Audio Source component on Background_Music, and place the background music audio clip into the Audio Clip slot. Make sure Play on Awake and Loop is turned on as we want the audio clip to play as soon as the game starts with the music playing continuously.

Audio Manager object created with Audio Source and Background music attached to it.

The 2nd sound we’re going to add is a Laser sound. This time we’re going to use a different technique in playing this sound using a script with the Audio Source component.

Inside the PlayerBehavior script, create 2 new variables with one for the Audio Source, and the other with a SerializeField for the Audio Clip. Inside the Start() function, make sure to NULL check for the Audio Source component.

Now inside the FireLaser() method, we want the Laser audio clip to play after the Laser is Instantiated. First, assign the Laser Sound clip variable to the Audio Source’s Audio Clip slot. Next, Play the Laser Sound clip through the Audio Source variable.

Laser Sound clip added to the Player’s Audio Source to play with each laser shot.

In the Hierarchy with the Player selected, add an Audio Source component and turn off Play on Awake. Then in the Player script component, drag the Laser audio clip itself into the Laser Sound Clip’s slot. Play the game and here the Laser audio clip playing after each laser fired.

Laser Sound clip playing with each laser shot.

Select the Asteroid game object, add the Explosion script component to it as the Explosion prefab is assigned as a variable. It has the the Explosion audio clip attached to its Audio Source’s Audio Clip slot, therefore giving us automatic access to the Explosion sound. Play the game and here the Explosion sound play when the explosion happens onscreen.

Asteroid explosion sound added.

For the Enemy explosion sound, go to the EnemyBehavior script. Create a new variable to access the Audio Source component. NULL check for it inside the Start() function. Then, down inside the OnTriggerEnter() function, Play the Audio Source component variable before Destroying the Enemy’s components and Enemies themselves.

Select the Enemy prefab, add an Audio Source component to it, drag the Explosion audio clip into the Audio Clip slot, and turn off Play on Awake.

Enemy explosion sound added.

For the Player’s Explosion sound, select the Player_Explosion prefab as this prefab is already assigned to the Player through their PlayerBehavior script component’s variable. Inside the Player_Explosion prefab, add an Audio Source component and place the Explosion audio clip in it.

Player explosion sound added.

We now have the Player, Enemies, and Asteroid game objects playing explosion sounds after each of them play their explosions while being destroyed.

Asteroid, Enemy, and Player explosion sounds play when each are destroyed.

With the PowerUps sound effects, we’re going to use the Audio Source’s PlayClipAtPoint() which is a static class method. With PlayClipAtPoint() we can use the Audio Source class instead of using an Audio Source component from an object. This way will create an Audio Source component when the audio clip plays, and disposes of it once the clip has finished playing.

Create 2 new SerializeField variables with one handling the Audio Clip to play, and the other having a Type Float to control the Volume level. Inside the IF other CompareTag equals the Player condition, we’re going to play the audio clip after retrieving the Player’s script component. Audio Source’s PlayClipAtPoint() function takes 3 parameters which are the audio clip to play, the transform position where the clip will play, and the Volume level the clip will play at.

With the PlayClipAtPoint(), we passed in the Powerup audio clip, the Main Camera’s transform position, and the Volume variable. We used the Main Camera’s position to hear the sound at a consistent volume level for all powerups collected. The Volume variable gives us flexibility of changing the sound levels for each powerup to give the impression one powerup is more important than the other. Since we can’t hear the audio play from the GIF’s, I used Debug.Log() to show when the powerups are playing.

Powerups to be played using Audio Source’s PlayClipAtPoint() method.

I took a still shot of the gameplay to show a powerup onscreen. If we look at the transform position, nothing jumps out as a problem. But, when the PlayClipAtPoint() is activated, the sound is almost inaudible. Since PlayClipAtPoint() plays at the object’s position, the Main Camera’s Audio Listener treats this powerup as though it’s a far distance away.

Notice the powerup is -8, 2, and zero on all 3 axis. The photo down below shows the Main Camera’s position as 0, 0, -10. The Main Camera was pushed back on the Z-axis to show the entire game screen. Therefore, the PlayClipAtPoint() is playing the audio clip in a 3D World environment which we don’t want. If you use PlayClipAtPoint(), remember the sounds will be picked up according to where there at in the environment. So I have the PlayClipAtPoint() play at the Main Camera’s position to hear the sound accurately and consistently.

Powerups are too far away from the Main Camera to have their sound played properly.
Main Camera’s transform position in the game.

If we’re playing one-off sound effects like the ones being played in this game, we can use a single AudioSource component that can be reused to play different sound effects through scripting. However, if we need to control the quality of how the sound effects play, then we should use an Audio Source component with each sound effect.

--

--