Game Audio Development with Unity 5.X
上QQ阅读APP看书,第一时间看更新

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:

  1. 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.
  2. 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:
Frame selecting the Capsule 5 GameObject
  1. 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.
We are going to add the audio to the scene following the audio layers and groups we defined earlier. Therefore, the first layer we will be adding the audio to is for the ambient and environmental background noises.
  1. 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:
Renaming and altering the position of the Capsule 5 GameObject
Giving your game objects an appropriate name is essential, especially as your scene becomes more complex. Notice how we prefixed the object name with our layer name and then followed that with the audio clip name.
  1. 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.
  2. 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:
Adding the Audio Source component
  1. 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:
Setting the AudioClip on the component
  1. 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.
Audio clips will typically be designed to either play forever, called a loop, or play just once, called a single shot. In most cases, you will want your ambient sounds to loop, but not always. Whereas, audio-like weapons or footsteps will generally be a single shot. A looping audio clip is one that is designed to play over and over again with no noticeable break or change when the clip plays over.
  1. 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.
  1. 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.