How to Play Sound Effects in Unity

Lasers, Explosions, and Power Ups. Oh My!

Laser Shot Sound Effect

I could add an Audio source to the Laser Prefab and set it to Play On Awake to true and I would have a sound effect playing. The problem with doing it this way is the Triple Shot Prefab is made up of 3 Laser Prefabs which means that the sound would play 3 times every time the the player fires a Triple Shot Laser. So I am going to put the audio source on the player prefab and have it play the sound every time it fires a laser.

In order for Audio to play in my Game I need an Audio Listener and an Audio Source that contains an Audio Clip to play. I already have the Audio Listener on the Main Camera. So I added an Audio Source to the Player along with the laser sound effect clip. I mad sure that it does not Play On Awake and that it does not Loop.

Since I need an Audio Source to Play Audio I added it to the Ship Behavior along with a bool to keep track of if the Ship has this sound effect. At the end of the Awake Method I get an Audio Source Component from the Game Object. If it has a Audio Source component and the Audio Source has a audio clip to play then I set the Has Laser Sound Effect to True. In the Fire Laser Method I want to check if the Ship has an Audio Source if it does I want to play the sound effect

How do I tell the Audio Source to play the Sound?

You can play a single audio clip using Play, Pause and Stop. Multiple sounds can be played on one AudioSource using PlayOneShot.

The Audio Source as 2 Methods that I can use to Play an Audio Clip Play and Play One Shot. I want to play it every time the Player Fires the laser, I am going to use Play One Shot. Even though it is one sound effect being played it is possible to play the next sound effect before the sound effect finishes so I am going to treat this like multiple sounds being played on one Audio Source.

Play One Shot takes the clip to play and an optional volume scale. I want that sound effect to play at full volume so I will just pass the Audio Clip in. I need to add an audio clip to the Ship I could use the clip already assigned to the audio source.

The Laser Sound Effect plays when I fire the Laser, but there is no sound effect when the triple shot is fired. I made the sound effect variables private, I order to access them from the Player behavior I need to make them protected or add a protected method to play the sound effect. I created a Sound Effect Behavior with a public Play Method, so I can use this to play Any Sound effects that I want.

Now I removed the Audio Source from the Play Game Object and Added a separate Game Object to the Player For the Laser Sound Effect and added the Sound Effect Component to it.

I then changed the Ship fire laser to use the new sound effect behavior.

I set the new parameters on the player ship.

Triple Shot Laser Sound Effect

Adding a sound effect for the Triple Shot is as easy as adding a new Triple Shot Power Sound Effect Object to the Player and adding the sound effect to the Player behavior.

I added an Empty Game Object to the Player, Added the Sound Effect Component. Set the parameters for the Sound Effect. Since I only have one sound effect for the laser I decided to change the pitch on the Triple Shot Audio Source so it sounded slightly different.

Copied the code for the fire laser sound effect from the Ship Behavior and Pasted it into the Player Behavior.

I set the parameters on the Player for the Triple Shot Fire Laser Sound Effect.

Player Hit Sound Effect

I added an Empty Game Object to the Player, Added the Sound Effect Component. Set the parameters for the Sound Effect. Since I only have one sound effect for explosions I decided to change the pitch on the Triple Shot Audio Source so it sounded slightly different.

I copied and pasted the sound effect code into the Damageable Behavior.

I chose to put it here in case I have an Enemy that takes more then one hit to destroy I can still have a sound effect play when it gets damaged without adding any code.

I set the Parameters for the new sound effect on the Player.

The Explosion Sound Effect

If I play a sound when an object is destroyed with the Audio Source on that object what happens is the Audio never plays because the Audio Source is no longer in the Scene. There are several options to get around this, Instantiate a new object in the scene to play it, disable the object and destroy it later, play the Audio on a different Game Object. Unity has already thought of this and made it simple. If you read the Unity Scripting Manual when we started you would see that the Audio Source has a static method that solves our problem for us.

AudioSource.PlayClipAtPoint

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.

I added this in on my Damageable Behavior.

Assigning the Explosion Sound Effect to the Enemy Prefab, this will also assign it to the Enemy Asteroid as well.

I added it to the Level Start Asteroid.

And to the Player Prefab.

The Power Up Collected Sound Effect

To play the sound effect for the power up being collected requires no new code. The power ups all have a damageable Component. All I Have to do is assign the Sound to the prefabs.

The Game now plays sound effects.

The Sound effect that plays from the Damageable Behavior sounds a lot quieter then the rest of the Audio in the Game, because when Unity Creates the Audio Source Component it sets the Spatial Blend to be 3D, To fix the sound level I removed the Audio Listener from the Main Camera and added it to the Audio Manager then I adjusted the volume of the Audio Sources I had for sound effects volume to 0.5f and Adjusted the Volume on the background music to 0.25f.

--

--

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
James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.