Explore Unity – Essentials of Real-time Audio
With Unity, you can not only set background music but also use it to simulate the ways sound behaves in 3D space. Audio is critical to making simulated environments feel real and you need to know how Unity simulates the ways sound waves interact in a physical environment. Audible sound changes depending on the listener’s distance from the source and even on the physical characteristics of the materials between the source and the listener.
For this project, you are shown how to add background music to a scene by using the Inspector to add an Audio Source component to an empty GameObject. Background music added in this way is static and plays at the same volume wherever you stand in the scene. The audio can be modified so that it changes volume or pitch depending on where you move to, known as 3D sound. To know how audio is played and received in a scene is to understand the concept of an Audio Listener in Unity. Just like the camera acts as the player’s eyes, the Audio Listener acts as the player’s ears and detects audible sounds at any given location to play back to the player. Only one Audio Listener can be in a scene and is attached in every default Unity scene to the Main Camera.
Any object that emits sound from an Audio Source can be made to sound louder the closer you get to it by manipulating the Spatial Blend property. By moving the slider to the left, or closer to 0, the sound becomes more static or 2D. By moving the slider to the right, or closer to 1, the sound becomes louder when close to it and softer when farther away, known as 3D audio. You can even further manipulate the way the audio is heard by working with audio roll-off, which defines the range in 3D space the audio can be heard and can be used to make it fade at greater distances and become inaudible. By setting the minimum and maximum distances for an AudioSource GameObject you are also provided with visible wireframe spheres around it that will show you where the audio will be heard loudest and where it will be inaudible.
This project ends with a challenge to add 3D sound to a moving object in a scene. You will need to use scripting to make a GameObject move, add an Audio Source Component to a GameObject, add an Audio Clip to an Audio Source Component, and create a Doppler effect using 3D sound. Once completed, you publish and submit your finished project to the Foundations Audio Project Gallery to show off what you have learned about working with 3D audio in Unity.