My journey becoming a Unity game developer: Audio Manager: Play the audio clips with stable sound quality
Objective: Create an Audio Manager object and script to play the audio clips assigned to each Voice Over Trigger game object producing stable sound quality.
As we play the game, we will notice that when the player walks through each Voice Over Trigger game object, the sound starts off with clear quality of Oliver speaking to Darren. However, as Darren moves further away from the Voice Over activated trigger objects, Oliver’s voice begins to trail off in the distance. This is happening because we assigned the audio clips to play from the Main Camera’s original position at the point where the voice trigger game objects are located. Therefore, as the Main Camera moves further away from the Voice Over triggers, Oliver’s voice will keep sounding muffled. We need to fix this problem by using an Audio Manager.
The Audio Manager will be used to play any audio clips using this manager and the Audio Source assigned to a game object to play in a 2D Spatial area instead of the 3D areas the audio clips are playing in currently. Also, we will be able to tweak the volume settings in the Audio Source using the Audio Manager as well.
Create an Audio Manager using an Empty game object. Set its Transform positions to zero. Create another Empty game object name VO for voice over. Set its Transform positions to zero as well. Place the VO game object as a child inside of the Audio Manager game object. Add an Audio Source component to the VO object. Create a new Audio Manager script name AudioManager, and attach it to the Audio Manager game object.
Inside the AudioManager script we will use the Singleton pattern. Create a new static variable Type AudioManager name instance for the Audio Manager. Then, create a new static property name Instance to retrieve the Audio Manager instance variable. In its Getter check if the instance is NULL, if it is send a Debug.LogError message notifying the Audio Manager is NULL. If it’s not, return the instance. Use the Awake() function to set the instance to This script.
Next, we want to create a public variable with a Type Audio Source name voiceOver to be a place holder for the VO game object since it has the Audio Source attached to it. Place this variable above the Awake() function.
In the Audio Manager game object, add the VO game object into the Voice Over slot under the Audio Manager script component.
Now create a method name PlayVoiceOver() which will have an Audio Clip name clipToPlay passed in as a parameter. Assign the voiceOver clip to be the clipToPlay passed in. Then, Play the clip through the voiceOver Audio Source.
In the Voice Over Trigger script, we originally set the audio clips to be played from the point where the player passed through the Voice Over Trigger object at the Main Camera’s position.
Instead, we’re going to change the Audio Clip variable name from audioClip to clipToPlay. Then inside of the IF its the Player Tag condition, delete the previous line of code and replace it calling the Audio Manager script’s Instance property to use the PlayVoiceOver() method passing in the clipToPlay Audio Clip being used from each Voice Over Trigger.
In the VO game object’s Audio Source component, we will turn off the Play on Awake since we don’t want the audio clip to play until its been triggered. Turn the Volume down to 50% because the audio clip’s sound was too loud. Make sure the Spatial Blend is set to 2D as we want the clips to play all around and not at a 3D position activated in the game.
Since we changed the variable’s name inside of the Voice Over Trigger script, we must check to make sure the VO Triggers game objects audio clips are assigned to each of them.
When we play the game now, as the player enters each VO Trigger game object, the audio clips will play with a consistent and clear sound quality to them.
Next time we will add some ambient background music to the game.