Immersion Starts with Sound
Adding Background Music in Unity
Now that we’ve added post processing to make our game look like a AAA title we need to round it out with sound! If we really want to immerse our players into our game sound plays a huge part in it! From the subtle background sounds of birds and foot steps in a jungle based first-person shooter to the sound of that particularly nasty lock puzzle hitting the ground after you’ve solved it in your favorite escape room VR game, sound can be just as important for a players immersion as the visuals they are presented. In our space shooter we are going to start with adding some background music to set the mood!
There are only a few steps to adding our background music and just a few components to work with to make it happen. The first component is our Audio Listener.
For our purposes there are two important things to know about our Audio Listener component. The first is that it is supplied to us out of the box by Unity and it is attached to the Main Camera by default. The second is that we are only supposed to have one Audio Listener in a scene. If we duplicate our Main camera, like we did when we created a second camera for objects that we wanted to exclude from post processing, then we need to remember to remove the duplicated Audio Listener component or Unity will give you warnings.
The next component we need is an Audio Source component. This component gives us most of the functionality we use when controlling our Audio including playback and looping.
The last component is an Audio Clip which is the class that represents the specific sound that we are playing through our Audio Source.
Adding background music
The steps to add background music to our game are pretty straight forward. First we create an empty object to hold our Audio Source for the background music. This is mainly for organizational purposes and isn’t strictly required, but I am encouraging the practice here. We’ll call it “Audio_Manager”, following our previous naming convention for manager objects. Next we add an Audio Source component to our manager. We could optionally add another empty child object to our Audio_Manager just for our background music and add the Audio Source to that could object instead.
Lastly, we can check the loop and play on awake options on our Audio Source and assign our audio file in the Audio Clip property.
Now when we play our game we should hear our snazzy game background music. You may need to adjust the volume on the Audio Source component of the music is loud.
That’s it! Now we have music in our game! Next time we will look at adding more audio to our game in the form of sound effects that play in response to certain actions! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.