Unity 2D Prototype part 7 – Music and Sound

      Comments Off on Unity 2D Prototype part 7 – Music and Sound

In this part we’ll be adding background music and sound effects to the game. The camera control comes preset with a component called an AudioListener. This component is the mechanism that detects played sounds and sends them through the audio driver to be played by the players current audio controller (device speakers, headphones, external speakers and so on).

In order to play sounds we’ll be adding AudioSource components to various parts of the scene. First off we need some sound clips. You should be able to find some that work for free by googling or going to the Unity Asset Store. In my case I have some sounds from another game I’m working on that will work for our purposes here. As you can see I’ve created a new folder called SFX and added 3 files to it.

ButtonClick will be played when we click one of our UI buttons

Collide is played when we catch a ball

HealthLost will be played when the ball reaches the bottom of the screen.

After you’ve added your sounds we need to click each one and in the inspector uncheck the 3D Sound option if it’s checked.

If 3D sound is enabled then the sound will take the distance between the object we’re attaching our Audio Source component and the object with the AudioListerner into account when determining the volume and panning between speakers.

Next we need game objects to host an AudioSource with each of our sound files. In the Hierarchy view create an empty object and add an Audio Source component to it. Disable Play on Awake, which would cause the clip to start playing immediately.

This one is named collide audio, so drag the audio file for the enemy being hit into the Audio Clip value.

Repeat the process for each of the sound effects, naming the game objects appropriately.

First we’ll set up the button click since it’s the easiest one to handle. On each button that we have in our various Canvas controls we need to do the following.

In the OnClick section of the button, add a new handler

Drag the game object that has the button click audio source into the object field, and select AudioSource -> Play from the function dropdown.

Now when we hit play in the editor and click the button you’ll hear the sound played. Repeat this step for all the buttons.

For the enemy hit and player hit sounds we’ll add two variables to the GameInfo object where we track the player’s health and score. These will be of type AudioSource and will represent the audiosource components on the 2 sounds we want to play.

We’ll add a bit of code to the DamagePlayer method so that we play the clip when we fail to catch a ball.


And add a similar call to play into the IncrementScore method

Now when you play you’ll find that the sound effects trigger when the score increments or the player health decrements.

Now all that’s left is some background music. Following a similar process find a song that you like. If you plan to release your game publicly make sure that it’s not a copywritten track and you have licensed permission from the creator to use it and the other sound effects that you’ve chosen. Make sure to turn off 3D sound if it was enabled on import.

Create a new empty game object and add an AudioSource to it as we did with the sound effects. I chose to call the music clip I imported BGM and the GameObject BGMAudio

We’re going to disable the PlayOnAwake as we did with the other AudioSource components, but for this one we’ll enable Loop. The background music should only play while the game is running, so let’s add some code to our Scene Manager to play and stop the clip when we enter and leave the game screen.

For a final bit of fun let’s finally put some options in the options screen. In this case we’ll add a slider to control music volume and a slider for sound effect volume.

As you can see I’ve added 2 sliders and 2 text labels and set them up as shown. Now we need to tie the slider value changed to the volume of the various audio sources.

In the On Value Changed section of the Slider add a handler the same way we added functions to be called when we clicked the UI buttons.

On the music slider drag add a handler and drag the gameobject that has the bgm audio source into the object field. In the function dropdown select AudioSource. You’ll notice two sections in the function list; Dynamic Float and Static Parameters. Only the Dynamic Float methods and variables can be used in the case of the slider. We want to add the volume variable. Since the variable is a float and the slider value is a float the two values can be bound together in this way.

Now when the slider moves the volume value on the AudioSource will change as well. The other slider should be wired up the same way but with the AudioSource components of the 3 sound effects in the scene.

During the game the audio will be adjusted, but we also want the settings to persist between game sessions just like our high scores do. We’ll follow a similar paradigm to save the settings when we leave the option screen. And load the settings when the game starts.

The settings manager isn’t too different from the Score manager that was created earlier. The main difference being the addition of the slider controls. Because we’re going to load this when the game starts we want to set the slider values, which because of the bindings of the slider values to the various audio sources, will update those audio sources as well.

As expected, we add the slider controls to the Settings Manager component in the appropriate slots.

And finally in the SceneManager when the NavigateToTitle method is called we check to see if the options screen is active, and if it was that means we’re leaving the options screen and need to save the settings

Run the game and you’ll find that the settings are saving when you start and stop the game, and that the slider volume is set appropriately when the game is started.