Book Image

Game Audio Development with Unity 5.X

By : Micheal Lanham
Book Image

Game Audio Development with Unity 5.X

By: Micheal Lanham

Overview of this book

Game Audio is one of the key components in making a game successful and it is quite popular in the gaming industry. So if you are a game developer with an eye on capturing the gamer market then this book is the right solution for you. In this book, we will take you through a step by step journey which will teach you to implement original and engaging soundtracks and SFX with Unity 5.x. You will be firstly introduced to the basics of game audio and sound development in Unity. After going through the core topics of audio development: audio sources, spatial sound, mixing, effects, and more; you will then have the option of delving deeper into more advanced topics like dynamic and adaptive audio. You will also learn to develop dynamic and adaptive audio using the Unity Audio Mixer. Further, you will learn how professional third party tools like FMOD are used for audio development in Unity. You will then go through the creation of sound visualization techniques and creating your own original music using the simple yet powerful audio workstation Reaper. Lastly, you will go through tips, techniques and strategies to help you optimize game audio performance or troubleshoot issues. At the end of the book, you’ll have gained the skills to implement professional sound and music. Along with a good base knowledge audio and music principles you can apply across a range of other game development tools.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
About the Reviewer
Acknowledgments
www.PacktPub.com
Customer Feedback
Dedication
Foreword
Preface

Audio sources and listeners


Before we add audio to our scene, we need to cover a few more basic concepts within Unity.

Of course, the best way to do that is to get hands on and explore more of the scene objects with the following exercise:

  1. If you have taken a break, open back up Unity and reload the GameAudioBasics project, which we saved at the end of the last section. Of course, if you haven't taken a break, good for you, and just continue as you were.
  2. Now, divert your attention to the Hierarchy window and specifically the search field at the top of the window. Click on the down arrow at the left of the search field, to select the form of the search as shown in the following screenshot:

Setting the search type

  1. Now, enter AudioListener; make sure to use the same case in the search field. This will filter the GameObject in the Hierarchy window with an AudioListener component attached, as shown in the following screenshot:

Hierarchy window showing filtered list of game objects with AudioListener

  1. As you can see, this filtered list of objects is showing us the various scene cameras. A camera in Unity is what captures the rendered view of the scene. As such, you can think of a camera as the player's eyes to the game world. Therefore, it only makes sense that we also make the camera be able to listen to the world using the AudioListener component. That way, as the camera moves around the scene, the ears or Audio Listener, will also be tightly coupled.

Note

A GameObject in Unity is the base scene object. What differentiates one object from another is the components attached to the GameObject. A component is a script that applies specialized functionality to a GameObject. A component could include everything from a camera, audio listener, audio source, mesh renderer, and so much more. The Inspector window allows you to view, modify, add, and remove components on a GameObject.

  1. Select Camera_high at the top of the list in the Hierarchy window and take a look at the Inspector window and bottom-right corner of the Scene view as shown in the following screenshot:

Camera preview and Inspector window

  1. As you can see, there are a number of components attached to the Camera_high object in the Inspector window. We won't get into the details of all the components but just take notice of the Camera and Audio Listener. Remember that these two components equate to the scene's eyes and ears. Select the Camera_high object selected and press the Play button to run the scene.
  2. After the scene starts running and switches to the Game view, press the Esc key to unlock the mouse. Then, go to the Inspector window and scroll through the components. Feel free to turn off and on components by unchecking the checkbox beside the component name. You can even alter the component settings if you like. Notice how the Game view changes as you make those changes. Don't be afraid to change several things; as long as you are in play mode, nothing will be saved.
  3. When you are done with exploring the Inspector window, press the Play button again to stop the game running.

Good. As you can see, we already have a number of Audio Listener components set up on the scene. This means that we can certainly hear any audio. However, we are still missing the source of our audio, which hasn't been configured. In the next section, we will add an Audio Source component to our scene.

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.

Note

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

Note

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.

Note

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.