How to Play Sound Effects in Unity

Objective: let’s add some sound effects to various game events in our 2D game project.

https://www.google.com/url?sa=i&url=https%3A%2F%2Fmisterhorse.com%2Fproducts%2Fessential-sound-effects%2F3263&psig=AOvVaw0derSjMsp0uJgEJOSJ9a43&ust=1629199290006000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCND73c62tfICFQAAAAAdAAAAABAE

In the first part of this article (https://myjl-besnard.medium.com/immersion-starts-with-sound-6e59e9f1a6c7) we added some background music to our game project. Now let’s continue our journey into the Game Audio section of our 2D Space Shooter game project from GameDevHQ by adding sounds effects triggered by events in our game. We will now add audio clips to give our laser shot, explosions, and powerup collection some life!

Laser Shot Audio Clip

Next, we will tackle adding specific sounds effects to the various game events, beginning with the Player’s laser shot. Set up a couple of private variables in the Player script. The first one is of type AudioClip which will store a reference to the sound effect that will be played. The second one of type AudioSource which hold the reference to the Audio Source component on the Player that is told to play the stored sound effect in AudioClip.

Create a private variable of type AudioClip to store a reference to the “laser_shot” audio clip in “_playerLaserShotAudioClip”.

In the Start() method, you then get the Audio Source component from the Player and assign it to _audioSource, run a null check, and if it is not null set the clip to _playerLaserShotAudioClip via code.

In order to play the sound, add _audioSource.Play() to the PlayerFireLaser() method. This will play the sound stored in the _playerLaserShotAudioClip through the Audio Source every time the Player fires his lasers.

Add “_audioSource.Play()” to play the audio clip associated with firing the laser.

Select the Player in the Hierarchy, and drag the “laser_shot” audio clip into “Player Laser Shot Audio Clip” script field (_playerLaserShotAudioClip private variable).

Dragging the ‘laser_shot” audio clip into the “Player laser Shot Audio Clip” field.

Again, with the Player selected, add a Audio Source component. It doesn’t matter if you drag the “laser_shot” into the AudioClip field, since via code, if the _audioSource is null, the _audioSource.clip will get set to the value of the _playerLaserShotAudioClip variable (which is “laser_shot”). Make sure you have deselected Play On Awake since the laser shot sound should be triggered only when the Player fires.

Audio Source component to playback the “laser_shot” Audio Clip.

Now doing all this will work great but there’s a slight script modification we can do to our Player’s Audio Source to make it more flexible by creating a method in the Player script which will play whatever audio clip we assign to it. For this, we will use the PlayOneShot(clip) function. This will allow us to play audio clips independently, and they won’t get cancelled out by other audio clips such as explosions or laser shots.

Let’s start by removing the else statement in the Start() method which sets the “_audioSource” to “_playerLaserShotAudioClip”.

Then let’s create a new public method in the Player script called PlayClip() which will look for an “AudioClip” that is stored in “soundEffectClip”.

PlayClip() method in the Player script.

Finally, change the _audioSource.Play() and replace it with PlayClip(_playerLaserShotAudioClip) in the PlayerFireLaser() method.

And with this, we now have a laser sound effect for our valiant Player. Let’s move on to explosions…

Explosion Audio Clip

We have a few occasions where the explosion audio clip needs to be triggered: when the Asteroid blows up, and upon collision detection between the Enemy game object and the Player laser or Player spaceship.

The Asteroid gets replaced by the Explosion prefab, so it makes sense to simply add an Audio Source component to the Explosion prefab with the stored “explosion_sound” audio clip set to Play On Awake when the prefab is instantiated.

Adding the explosion audio clip to the Explosion prefab. Set to Play On Awake when the prefab gets instantiated upon the destruction of the Asteroid.

When dealing with the other scenarios (Enemy dies, Player dies), both get detected in the Enemy script via the OnTriggerEnter2D method, so it makes sense to use this as the location to setup playback of the “explosion_sound” audio clip.

Start by adding a Audio Source to the Enemy prefab (or as in my case I’ve selected both the Enemy and EnemyDodging prefabs since I have multiple adversaries), and drag in the “explosion_sound” audio clip. Set Play On Awake to false.

Once this is done, we need to do as we did in the Player script with the Laser audio clip, and set a reference to the explosion audio clip in the Enemy prefabs. Create a private AudioClip_explosionSoundeffect”, as well as a reference to the Audio Source component with another private AudioSource variable “_audioSource”.

Create the required private variables of type AudioClip and AudioSource.

In the Start() method, get a reference to the AudioSource component and store it in “_audioSource”, followed by a null check. If the “_audioSource” is null, set it to the “_explosionSoundEffect” value to avoid any errors.

Get a reference to the Enemy script Audio Source, null check it, and set its default value if null.

Add _audioSource.Play() to your OnTriggerenter2D method, just before we destroy the Enemy game object, when collisions are detected with both the Player’s Laser and the Player itself.

Adding “_audioSource.Play()” just prior destroying the Enemy game objects to play the explosion audio clip.

Compile your changes. Then, select your enemy prefabs, and drag the “explosion_sound” into the Enemy and DodgingEnemy scripts “Explosion Sound Effect” field.

Adding the “explosion_sound” audio clips to the Enemy prefabs script fields.

This completes the steps required to add the “explosion_sound” audio clip which gets triggered upon Enemy game object collision detection with the Player Laser or Player game object. Let’s wrap it up with the Powerup collection sound effect.

Powerup Sound Effect Audio Clip

The Powerup sound effect offers a different sort of challenge. Whereas before, the other game object were destroyed following a certain time delay which allowed the associated sound effects to be heard. With the Powerup, they get destroyed upon contact with the player game object, so we would never be able to hear any sound effects.

The problem can get resolved in two ways: either create a stand alone game object which gets instantiated at the Powerup transform position, and use this new game object to hold a reference to, and play, the Poweruppower_up_sound” sound effect. These same game objects would self-destruct after the clip has finished to avoid cluttering up the Hierarchy.

Creating a new AudioClip variable in the PowerUps script to store a reference to our “power_up_sound” audio clip.

You then need to drag in the “power_up_sound” audio clip into each PowerUps prefab.

Dragging the “power_up_sound” audio clip into the Power Up Audio Clip field of the PowerUps game object prefabs.

You then need to use the PlayClipAtPoint function found in the AudioSource class. Unity describes this function as “Plays an AudioClip at a given position in world space. This function creates an audio source but automatically disposes of it once the clip has finished playing.

In the PowerUps script, add the following line before destroying the Powerup:

AudioSource.PlayClipAtPoint( _powerUpAudioClip, transform.position)

This will create a temporary Audio Source game object at the transform position of the collected Powerup, play the audio clip, then destroy itself once the audio clip has finished playing.

The second option is to simply use the PlayClip() method that we built in the Player script. This is much more simple since we are already getting a reference to the Player script in the OnTriggerEnter2D() method of the PowerUps script.

To do this, add a private AudioClip_powerupAudioClip” to hold a reference to the “power_up_sound” audio clip in the Player script.

Attaching the _powerupAudioClip to the Player script instead of the PowerUps script.
Drag the “power_up_sound” audio clip into the Power Up Audio Clip field.

Then, in the PowerUps script, remove the PlayClipAtPoint function and replace it with the following line inside the player null check:

player.PlayClip(_powerupAudioClip)

Using the PlayClip() method in the Player script to play the “_powerupAudioClip”.

This allows you to use the PlayOneShot function of the PlayClip() method, playing the “_powerupAudioClip” reference held in the “soundEffectClip”.

Remember the PlayClip() method in the Player script?

This conclude my article on how I implemented various sound effects into my game project. As always, there are multiple solutions available, and it’s important to take the time to understand the solutions you opted to work with. And finally, experiment, have fun and always strive to find various ways to optimize your code. Thanks for reading! :)

Military member with 34+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

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

Recommended from Medium

An Introduction to Distributed SQL: Glossary of Terms — The Distributed SQL Blog

What do creators really need?

Excited for move to WordPress

Why These Python Coders are Joining the napari Community

Why Load Testing is Essential to Take Your ML App to Production

How to Run SpringBoot-App Container with MySql Container and PhpMyAdmin Container together using…

What if we do exhaustive automation testing?

Make your Apple gadgets more exciting with iT Gurus Software iOS apps!

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
Michel Besnard

Michel Besnard

Military member with 34+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

More from Medium

Galaxy Shooter 2D — New Enemy Behavior (Follow)

Hidden Shortcuts to Make Level Design Easier in Unity.

Changing Sprites

Level Design in Unity Part 1: The Floor