Add audio to your game
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(15)
Unity Technologies

In this tutorial, you’ll add ambient sound, object-specific audio, and SFX to enhance the atmosphere and player experience.
1. Overview
Now that your level is filled with enemies to challenge the player character’s escape, you might have noticed during testing that the game feels a little too quiet.
In this tutorial, you’ll add the finishing touches to your game by incorporating spooky audio to enhance the atmosphere and create a more immersive experience.
You’ll work with two different types of audio:
- Non-diegetic sound (2D): This type of audio has no identifiable source in the game world, such as background music or ambient effects.
- Diegetic sound (3D): This type of audio comes from a recognizable source within the game, such as the player character’s footsteps or the eerie noises made by enemies. Each time you've entered Play mode, you might have heard a sound similar to a light flickering. That’s because each lamp in the level has a 3D Audio Source component with this noise attached to it.
By the end of this tutorial, your game will have a richer, more unsettling soundscape that draws players deeper into the haunted environment.
2. Create an Audio Source
Before you begin adding audio to your game, it is important to quickly review how audio works in Unity. There are three main parts: Audio Clips, Audio Sources, and the Audio Listener.
Audio Clips are assets such as MP3 files that contain all the data for a particular sound. Audio Sources are components that act as the origin of a sound within the game world. The Audio Listener is a single component in the scene that works like the virtual ears of the player. By default, the Audio Listener component is attached to the Main Camera GameObject.
These elements work together in the following way: an Audio Source component plays an Audio Clip, and if the Audio Listener component is close enough to the Audio Source component, the sound will be heard.
First, you’ll create an Audio Source component for the ambient background sound in your game.
1. Create an Empty GameObject:
- Right-click In the Hierarchy window and select Create Empty.
- Rename the empty GameObject "Ambient_AudioSource".
2. Set the Position property:
- In the Inspector window, set the Ambient_AudioSource GameObject’s Transform component’s Position property to X = 0, Y = 0, and Z = 0.
Note: The position doesn’t matter for a 2D ambient sound, but it’s good practice to keep GameObjects organized.
3. Add and configure the Audio Source component:
- In the Project window, navigate to _3DStealthGame > Audio.
- Select the SFXHouseAmbience Audio Clip and press the Play button in the Inspector window to play the audio preview.
- In the Hierarchy, re-select Ambient_AudioSource GameObject and drag the SFXHouseAmbience Audio Clip from the Project window into the Inspector window.
This will automatically create an Audio Source component and assign the SFXHouseAmbience Audio Clip to it.
4. Check the Audio Source settings:
- The Spatial Blend property should already be set fully to 2D.
Note: Setting the Spatial Blend property to 2D makes the audio play at the same volume everywhere, which is great for ambient sounds. Setting the property to 3D changes the audio’s volume based on the distance of the Audio Listener component to the Audio Source component, which is useful for sounds like people talking.
- Check that the Play On Awake property is enabled, meaning that the audio will start automatically when the level loads.
- Enable the Loop property so that the ambient sound continuously plays throughout the game.
5. Test the ambient sound:
- Save the scene using Ctrl+S (macOS: Cmd+S).
- Enter Play mode and ensure the audio plays properly.
Note: You might realize the sound is too loud. Lowering the volume helps balance the ambient sound with other game sounds you’ll add later.
- Exit Play mode when you’re done testing.
6. Adjust the volume:
- In the Inspector window, set the Audio Source component’s Volume property to 0.5.
Now your haunted house has an eerie background ambiance that sets the tone for your game. In the next step, you will create another Audio Source component for the win and lose conditions.
3. Create ending Audio Sources
As already mentioned, one Audio Source component can produce a sound, and you can set a script that changes different audios through code. However, that would be a lot of work. For the scope of this project, you’ll create a different Audio Source component that plays when the player wins or loses the game.
There are two more non-diegetic sounds to implement: the VFXLose audio, which plays when the player character is caught by an enemy, and the VFXWin audio, which plays when the player character successfully exits the haunted house.
Instead of setting them up from scratch, let’s be more efficient by duplicating the existing Ambient_AudioSource GameObject and adjusting its settings for each new sound.
1. Duplicate the Ambient_AudioSource GameObject:
- In the Hierarchy window, select the Ambient_AudioSource GameObject.
- Duplicate it twice using the shortcut Ctrl+D (macOS: Cmd+D).
- Rename the first copy “Escape_AudioSource ” and the second copy “Caught_AudioSource”.
2. Adjust common settings:
- In the Hierarchy window, hold Ctrl (macOS: Cmd) and select the Escape_AudioSource and Caught_AudioSource GameObjects to select them both.
- With both GameObjects selected:
- Disable the Play On Awake property.
- Disable the Loop property.
- Set the Volume property value to 1.
Note: These sounds shouldn’t play automatically or loop, and they need to be louder than the ambient audio to stand out.
3. Assign the Audio Clips:
- Select the Escape_AudioSource GameObject, select the Audio Resource picker (⊙) and select SFXWin.
- Select the Caught_AudioSource GameObject, select the Audio Resource picker (⊙) and select SFXLose.
4. Create a parent GameObject for the Audio Sources:
- In the Hierarchy window, hold Ctrl (macOS: Cmd) and select the Ambient_AudioSource, Escape_AudioSource, and Caught_AudioSource GameObjects to select them all at once.
- Right-click and select Create Empty Parent.
- Rename the empty parent GameObject “Audio”.
Now you’ve set up your non-diegetic Audio Source components. In the next step, you’ll add the functionality to play these audio sources in your GameEnding script.
4. Update the GameEnding script
Now that you’ve tidied the audio up, it’s time to implement the game-ending sounds in the GameEnding script.
You need to add the functionality to play an Audio Source when the EndLevel method is called. There are two different Audio Sources that could play, so you will need a reference to each. Remember that EndLevel is called every frame in the Update method. You don’t want the Audio Sources to keep playing over and over, so you’ll need a way to stop them after the first time. Let’s get started by adding your Audio Source references!
1. Add the Audio Source references:
- In the Project window, locate the GameEnding script.
- Double-click the GameEnding script to open it in your IDE.
- Below the UIDocument variable declaration, add the following variables:
public AudioSource exitAudio;
public AudioSource caughtAudio;
bool m_HasAudioPlayed;Note: The m_HasAudioPlayed variable ensures the audio only plays once. A bool variable is false by default; you’ll check if it’s false before playing the audio and set it to true once the audio plays.
2. Update the EndLevel method:
- Change the EndLevel method to accept an additional parameter:
void EndLevel (CanvasGroup imageCanvasGroup, bool doRestart, AudioSource audioSource)3. Update the calls to EndLevel:
- Update the first EndLevel call to the following:
EndLevel (exitBackgroundImageCanvasGroup, false, exitAudio);- Update the second EndLevel call to the following:
EndLevel (caughtBackgroundImageCanvasGroup, true, caughtAudio);Note: The method calls are now correct again, but the EndLevel method itself needs to use the new AudioSource parameter.
4. Play the audio inside EndLevel:
- At the start of the EndLevel method, add the following if statement:
if (!m_HasAudioPlayed)
{
audioSource.Play();
m_HasAudioPlayed = true;
}Note: The exclamation mark (!) negates the condition, meaning this block only executes if the audio hasn't been played yet.
- Save the script using Ctrl+S (macOS: Cmd+D) and return to Unity.
5. Assign the Audio Sources:
- In the Hierarchy window, select the GameEnding GameObject.
- In the Inspector window, use the picker (⊙) to assign:
- Escape_AudioSource GameObject in scene to the Exit Audio slot.
- Caught_AudioSource GameObject in scene to the Caught Audio slot.
6. Test the new functionality:
- Select the Play button to enter Play mode and listen for the different audio components you added.
You’ve finished setting up the non-diegetic ending audio. In the next steps, you will add the diegetic audio for the enemy effects.
5. Review the GameEnding script
Before moving on, make sure your GameEnding script matches the one below:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
public class GameEnding : MonoBehaviour
{
public float fadeDuration = 1f;
public float displayImageDuration = 1f;
public GameObject player;
public UIDocument uiDocument;
public AudioSource exitAudio;
public AudioSource caughtAudio;
bool m_IsPlayerAtExit;
bool m_IsPlayerCaught;
float m_Timer;
bool m_HasAudioPlayed;
private VisualElement m_EndScreen;
private VisualElement m_CaughtScreen;
void Start()
{
m_EndScreen = uiDocument.rootVisualElement.Q<VisualElement>("EndScreen");
m_CaughtScreen = uiDocument.rootVisualElement.Q<VisualElement>("CaughtScreen");
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == player)
{
m_IsPlayerAtExit = true;
}
}
public void CaughtPlayer ()
{
m_IsPlayerCaught = true;
}
void Update ()
{
if (m_IsPlayerAtExit)
{
EndLevel (m_EndScreen, false, exitAudio);
}
else if (m_IsPlayerCaught)
{
EndLevel (m_CaughtScreen, true, caughtAudio);
}
}
void EndLevel (VisualElement element, bool doRestart, AudioSource audioSource)
{
if (!m_HasAudioPlayed)
{
audioSource.Play();
m_HasAudioPlayed = true;
}
m_Timer += Time.deltaTime;
element.style.opacity = m_Timer / fadeDuration;
if (m_Timer > fadeDuration + displayImageDuration)
{
if (doRestart)
{
SceneManager.LoadScene (0);
}
else
{
Application.Quit();
Time.timeScale = 0;
}
}
}
}
6. Add the Ghost Audio Source
The final piece of audio for your game is the spooky sounds that the Ghosts will make as they move. This will not only enhance the atmosphere of your game, but it will also help the player detect how close danger is.
Now that the Player prefab has an Audio Listener component, you need to set the Ghosts' audio volume to vary relative to the Player GameObject’s position.
1. Open the Ghost prefab:
- In the Hierarchy window, use the foldout (triangle) to expand the Enemies GameObject.
- Select one of the Ghost GameObjects.
- Select the arrow next to Ghost GameObject’s name in the Hierarchy window to enter prefab editing mode.
2. Add the movement sound to the Ghost:
- In the Project window, navigate to _3DStealthGame > Audio and locate the SFXGhostMove Audio Clip.
- Click and drag the SFXGhostMove Audio Clip onto the Ghost GameObject in the Inspector window.
This will automatically add an Audio Source component with the SFXGhostMove Audio Clip attached to it.
3. Adjust the Audio Source settings:
- In the Inspector window, locate the Audio Source component you just added to the Ghost GameObject.
- Enable the Loop property so the sound plays continuously.
- Set the Volume property to 0.4 to keep the sound from being too overwhelming.
- Set the Spatial Blend property to 1 to make the sound fully 3D.
4. Customize 3D Sound Settings:
- Use the foldout (triangle) to expand the 3D Sound Settings section.
- Set the Max Distance property to 10.
Note: This means the player will still hear the Ghosts up to 10 meters away.
- Change the Volume Rolloff property from Logarithmic Rolloff to Custom Rolloff.
- Save the Ghost prefab.
- Exit prefab editing mode to return to your scene.
Great! You've added a nice variety of sounds that help immerse the player in your game and give feedback when they win, lose, or get close to an enemy. In the last step, you’ll set the Audio Listener correctly on your Player GameObject.
7. Move the Audio Listener onto the Player GameObject
By default, the Audio Listener component is located on the Main Camera GameObject. This means that diegetic sounds (like the ghost’s eerie sounds) will get louder as they approach the camera, not the Player GameObject.
To make the sounds properly follow the Player GameObject’s position, you need to move the Audio Listener component onto the Player GameObject.
1. Remove the Audio Listener from the Main Camera GameObject:
- In the Hierarchy window, select the Main Camera GameObject.
- In the Inspector window, select the Audio Listener component’s More menu (⋮) and select Remove Component.
2. Add an Audio Listener to the Player GameObject:
- Select the arrow next to the Player GameObject’s name in the Hierarchy window to open prefab editing mode.
- In the Inspector window, select the Add Component button.
- Search for and add the Audio Listener component to the Player parent GameObject.
Your game audio is now finished! Make sure to test it and experience the full atmosphere you’ve created. If you’re curious to learn more, check out Unity’s Audio Documentation or the Creative Core Pathway: Audio course.
8. Next steps
Congratulations!
With these steps completed, you’ve added all the core functionality to your game, and it’s now ready to be deployed!
In the next tutorial, you’ll have the opportunity to learn how to take your game to the next level by adding extra features and personal touches.
However, this tutorial is entirely optional; it’s up to you if you’d like to explore it. If not, go ahead and mark everything as complete and move on to the final tutorial, where you’ll learn how to build your game and share it with others.