Adding an audio source
In order to hear any audio in a scene, we need two components added to game objects. They are the Audio Listener - the ears, and the Audio Source - the noise. As you probably already realized, the dead silence of our scene must mean there are no configured Audio Sources. In the following exercise, we are going to rectify that issue by adding an Audio Source:
- Open the Unity editor and make sure the Viking Village scene is loaded. If you are continuing from the last section, be sure to click on the X on the right side of the Hierarchy window search field. This will remove the type filter and show all the scene objects again.
- Locate the GameObject called AccessibleVolume in the Hierarchy window and click on the arrow beside it to expand and show its child objects. Select the Capsule 5 object and press F, to frame select the object as shown in the following screenshot:
- What we want to do is use the Capsule 5 object as an Audio Source for our lake-shore-waves ambient sound we imported earlier. We will use this object as it is conveniently situated close to where we want our Audio Source. Before adding the audio though, let's make a couple changes to the capsule.
- With the capsule object still selected, rename the object Ambient_lake-shore-waves and then set the Z position on the Transform component to 60 as shown in the following screenshot:
- After you changed the Z position of the Transform, you will notice that the object moves to shore of the lake in the Scene view. This is exactly what we want, as this capsule object will be the source for the lake-shore-waves ambient sound.
- Next, click on the button Add Component button at the bottom of the Inspector window. Type audio in the search text and notice how the list filters to components with audio in the name. Select the Audio Listener component as shown in the following screenshot:
- We now have an empty Audio Source component on our object. Click on the target icon located next to the AudioClip property. This will open the Select AudioClip dialog. Select the lake-shore-waves2 clip from the list as shown in the following screenshot:
- Close the dialog after selecting the clip. We won't worry about all the other settings on the component right now. However, we do want to make sure the clip loops, since, after all, it is ambient sound that should keep playing over and over again. Click on the checkbox next to the Loop setting to make sure the audio loops.
- With the Audio Source is added to the scene, press the Play button. Explore the scene by moving around and listening to the audio as you move. When you are done exploring, press Ctrl+P (command + P on Mac) to stop the scene running.
- Select File | Save Scene from the menu, to save the changes we made to the scene. It is a good habit to save your scene often in Unity and especially when you are working with large scenes.
Good, we now have an ambient Audio Source in our scene. Of course, most likely, the first thing you noticed is that the sound was everywhere and that certainly is not what we want. We obviously only want our waves sound to be more audible when we get closer to the lake and certainly not so pronounced everywhere. What we need is to make our Audio Source use 3D or spatial sound and this is exactly what we will cover in the next section.