In this section, we are going to introduce the concept of 3D sound and show how this is implemented at the basic level within Unity. Since the introduction of virtual reality, there has been other implementations of 3D or spatial sound, which Unity can support through plugins, but that is outside the scope of this section. Either way, perhaps it is best to start with a definition of 2D and 3D sound using the following diagram:
In Unity, the basic simulation of 3D sound is achieved by reducing the audio to a single channel and then attenuating (altering the volume), depending on the distance and location to the listener. There are several variables that allow us to customize how an Audio Source is spatialized in Unity, which we will cover when we convert our lake waves 2D ambient sound to 3D in the exercise here:
- Continuing from the last section, be sure the Ambient_lake-shore-waves object is selected in the Hierarchy window.
- Go to the Inspector window and scroll the component list to the bottom so that all of the Audio Source component is visible.
- Drag the Spatial Blend slider all the way to the right in order to make the sound entirely 3D as shown in the following screenshot:
Setting the spatial blend on an Audio Source
Depending on the audio effect you are after, you may not want a full 2D or 3D Audio Source. Fortunately, with the Spatial Blend slider, you can select a mixture of both 2D and 3D sound. An example of an Audio Source that may use both 2D and 3D is an audio effect that needs to be heard everywhere but also has an obvious source.
- At this point, we won't make any other changes to the Audio Source but take note of the 3D sound settings and especially the graph at the bottom of the component. Observe the line representing the listener position on the graph. Pay particular attention to that line, and press the Play button to run the game.
- Move around the scene and move to the water down the boardwalk. Notice how the sound does get louder as you approach the water or if you turn the direction will also change to represent where the Audio Source is. However, the sound probably isn't quite as loud as we want and the falloff is just too gradual.
- Stop the scene from running by pressing Ctrl+P (command + P on Mac).
- Let's alter the 3D settings on our Audio Source. Change the Max Distance on the 3D Sound Settings of the Audio Source to 100. Notice how this changes the graph as shown in the following screenshot:
Changing the max distance of the Audio Source
- Press play and move around the scene again. Notice how the sound now attenuates better as you move close to the shore. Feel free to change the various 3D sound settings and see what effect they have on the audio playback.
The 3D Sound Settings volume/distance graph curves and lines can also be altered by clicking on them and dragging the line or keys to alter their position and shape. Double clicking on a curve or line will introduce a new key which will allow you to alter the shape of the curve even further. With this ability to alter an audio sources volume, spatial, spread, and reverb over distance is very powerful and can allow you to create some very interesting effects.
- When you are finished altering the Audio Source settings stop the scene. At this point, if you find a combination of different audio settings, you think, might work better, feel free to set those values now. After you are done making the changes, be sure to save the scene by selecting File - Save Scene from the menu.
As you can appreciate, now there are numerous ways to configure a sound to be 2D, 3D, or some mixture of both. How about we add another ambient sound for all the torches in our village by following this exercise:
- Switch your attention to the Hierarchy window and click on the search type dropdown on the left of search field. Then, be sure to select by Name as shown in the following screenshot:
Changing search filter type to by Name
- Enter prop_torch in the search field. Notice how the Hierarchy window filters the objects to just display those with the matching name. Select all the objects in the window by clicking on the top item and then while holding the Shift key, select each of the remaining items in the list. So, all the torch items are selected as shown in the following screenshot:
Selecting all the torch objects
- Making sure that all the objects are still selected, go to the Inspector window and click on the Add Component button at the bottom of the window. Then, as you did before, type audio in the search field to filter the list to audio components. Select the Audio Source component from the list to add it to all the prop_torch GameObjects.
In our example, we are modifying several copies of the same object, the torch. Yet that doesn't have to be the case. You can use this same technique to modify several different objects as long as they share a common component.
- After the Audio Source is added to the objects, set the properties on the component to match those in the following screenshot:
Torch Audio Source properties
- If you find yourself struggling to set any of the properties on the Audio Source component, refer to the preceding sections in this chapter. After you are done making the edits, press the Play button to run the scene.
- Be sure to explore the scene and be sure to move close to the water and around the torches. Feel free to make changes to the Audio Source settings as you explore. As always, if you find some settings sound better, remember those and make those edits again after you exit the play mode.
Have you noticed that the exercise instructions become less specific as you move through the exercises? Well, of course that is intentional and not only will it test your knowledge and make sure you are paying attention, but it also forces you to explore the Unity editor and find your own way to do something. There are numerous ways to do even the simplest tasks in Unity, and as you gain experience in the editor, you will find your own preferred ways of doing things.
- When you are done testing and tweaking audio, be sure to the save the scene and project.
Well, that completes this section and our introduction to 3D sound in Unity. We covered this material quickly and did not go into details of the 3D Sound Settings. There will be plenty of time to cover additional details about those settings in upcoming chapters.