Unity Basics: Working with Audio

tutorialgame-audio-unity

A first pass through Unity’s built-in audio system on a small 3D project: setting up the project, looping music, footsteps triggered from animation events, audio mixers and snapshots, and box triggers that change the sound of a space. It is the groundwork for the FMOD guides that follow — you learn what Unity provides before you replace it with middleware.

📝 Note

Written September 2021 for MUSC 601 and last run in Fall 2022 — the project-naming step still says so. Unity’s Starter Assets package has been updated since; if a menu path differs, the concept is the same.

Project Setup

  1. Create a new 3D Unity project and name it FA22_MUSC-601_Week-03_FirstInitialLastName (e.g. FA22_MUSC-601_Week-03_DDehaan).

    screen shot 2022 09 25 at 7.09.19 pm

  2. Import the Starter Assets - Third Person Character Controller from the Unity Asset Store.

    1. After clicking the link above, click “Add to My Assets”.

      screen shot 2021 09 19 at 5.39.31 pm

    2. Click “Open in Unity”.

      screen shot 2022 09 25 at 7.11.42 pm

    3. Make sure the Starter Assets - Third Person Character Controller package is selected and click “Download” and/or “Import”.

      screen shot 2021 09 19 at 5.45.13 pm

    4. If a second import window appears make sure all the assets are selected and click “Import”.

      screen shot 2021 09 19 at 6.08.13 pm

  3. If you get a pop-up warning about this project using a new input system, click “Yes.”

    screen shot 2022 09 25 at 7.13.53 pm

  4. Once the import completes and the Unity Editor has restarted, open the demo scene found in the Project window **by navigating to Assets > StarterAssets > ThirdPersonController > Scenes and double-click “Playground”.

    screen shot 2021 09 19 at 5.48.36 pm

  5. Save the current scene as “Audio-Playground” in the Assets > Scenes folder by right-clicking on the top-level Playground Scene within the Project Hierarchy window and choosing Save Scene As…

    screen shot 2021 09 19 at 5.59.01 pm

  6. Test the scene by clicking the play button near the top of the Unity Editor window.

    screen shot 2021 09 19 at 6.48.55 pm

Adding Music Loop

  1. Check that the scene’s MainCamera game object has an Audio Listener component.

    screen shot 2021 09 19 at 6.33.39 pm

  2. Right-click in the empty space of the Hierarchy window and create an Empty game object called “MusicLoop”.

    screen shot 2021 09 19 at 6.34.56 pm

  3. In the new game object’s inspector, add an Audio Source component.

    screen shot 2021 09 19 at 6.39.39 pm

  4. Using the Project window, create a new “Music” folder inside the Assets folder.

    screen shot 2021 09 19 at 6.41.02 pm

  5. Import any music .WAV file of your choosing, by dragging and dropping into the new Music folder.

    Music Elevator by Jay_You Id-460432.wav.zip

  6. After the .WAV file has finished importing, drag it from the Project window into the Audio Clip parameter of the MusicLoop game object’s Audio Source component.

    screen shot 2021 09 19 at 6.46.05 pm

  7. Check that both the Play On Awake and Loop options are enabled.

    screen shot 2021 09 19 at 6.47.24 pm

  8. Test by clicking the Play button.

Adding Footsteps

  1. Navigate to Assets > Standard Assets > ThirdPersonController > Character > Animations > Locomotion—Walk_N.anim

    adding footstesp   step 1

  2. Expand the Events section.

    footsteps   step 2

  3. Move the model into a good position to see the feet.

    • Option/Alt + click-drag turns the camera
    • Click-drag slides the camera
    • Option/Alt + mouse wheel zooms in and out
  4. Drag on Play Ruler (above model view) to find the first footfall and add a “Step” event by clicking the Add Event button.

  5. Repeat the previous step for all footsteps.

  6. When a “Step” event has been added to all animated footsteps, click “Apply”.

    click apply

  7. Select the PlayerArmature game object in the Hierarchy window, and, if one does not already exist, add an Audio Source component.

  8. Add a new Script Component named “Audio_Trigger_Animation_Event_Step” to the PlayerArmature game object.

  9. In the Project window, create a new Scripts folder inside the main Assets folder and move your new Audio_Trigger_Animation_Event_Step inside of the new folder.

  10. Open the new script in Visual Studio.

  11. In Visual Studio edit the script so it matches the one shown below:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Audio_Trigger_Animation_Event_Step : MonoBehaviour
    {
        public AudioClip[] footsteps;
    
        private AudioSource audioSource;
    
        private void Awake()
        {
            audioSource = GetComponent<AudioSource>();
        }
    
        private void Step()
        {
            AudioClip clip = GetRandomClip();
            audioSource.PlayOneShot(clip);
        }
    
        private AudioClip GetRandomClip()
        {
            return footsteps[UnityEngine.Random.Range(0, footsteps.Length)];
        }
    }
  12. Save and close the script.

  13. Inside the main Assets folder, create a new folder called SFXs, and import these footstep audio files:

    Footstep Audio Files.zip

    create sfx folder

  14. Add several footstep audio clips to the Footsteps array within the Audio_Trigger_Animation_Event_Step component.

  15. Test by running the game.

adjust music level

Audio Mixers

By default, all new Audio Source components output their signal directly to the scene’s Audio Listener component (usually found on the MainCamera game object). Unity’s Audio Mixers can be used to control and effect the flow of sound from the scene’s Audio Sources to the Audio Listener.

Creating an Audio Mixer

  1. Begin by creating a new Audio Mixer folder inside the main Assets folder.

  2. Inside the new folder, right-click to create a new Audio Mixer and call it “Audio Mixer”.

  3. Then double-click on the new Audio Mixer to open the Audio Mixer window.

    create an audio mixer

Along the left side of the Audio Mixer window you’ll see four sections:

  • Mixers contain groups (similar to a track inside a DAW). They are called “groups” because they can have any number of Audio Sources or other Audio Mixers routed through them.
  • Snapshots save recallable presets for a Mixer.
  • Groups are the “tracks” of a Mixer.
  • Views store and recall visibility settings while editing, making it easier to work with lots of groups.

Simple Audio Mixer Example

Previously we used the Volume slider of our MusicLoop’s Audio Source component to balance the levels between the background music and the sound of the character’s footsteps. Let’s try using an Audio Mixer to accomplish the same thing.

  1. Begin by returning the background music’s volume to 1. We’ll now use the Audio Mixer to control the volume of the music.

    background music return to 1

  2. Next, add a new group called “Background Music” by clicking the ’+’ button near the top right of the Groups section.

    create background music group

  3. Finally select the Background Music Group as the Output destination for the Background Music’s Audio Source component.

    select output destination

  4. Repeat this same process for the footsteps and any other SFX you may have added.

    select sfxs output destination

screen shot 2021 09 19 at 9.44.13 pm

In this case, both the Background Music and Player SFXs groups output their audio signals to the Master group.

A mixer’s signal flow can be configured by dragging the groups around in this section.

Snapshots

  1. Enter play mode, then, after pressing the ‘esc’ key to free your mouse from the game, enable the Audio Mixer’s Edit in Play Mode feature.

  2. Right click the default snapshot and rename it to “Music Up”

  3. Then click the ’+’ button to create a new snapshot called “Music Down”

  4. With the Music Down snapshot selected, lower the volume of the Background Music group.

  5. Then listen and watch as you click back and forth between the Music Up and Music Down snapshots.

Box Triggers

Box Triggers are invisible game objects within the scene that are used to trigger events whenever a player moves through them.

Let’s create a box trigger so that whenever our player walks through the short tunnel the background music fades out, and an owl “hoots” randomly for as long as the player remains inside the tunnel.

  1. Begin by bringing the short Tunnel_Prefab game object into focus:

    1. Select it in the Hierarchy window under Environment > Greybox > Tunnel_Prefab.
    2. Hover the cursor over the Scene window and press the F key.

  2. Right click on the Tunnel_Prefab object in the Hierarchy window and create a new 3D Object > Cube game object named “Box_Trigger”.

    add cube to tunnel

  3. With the new Box_Trigger object selected in the Hierarchy window, navigate to the Inspector window and set the following:

    • Transform > Position x = 0, y = 0, z = 1.25

    • Transform > Scale x = 2.5, y = 6, z = 2.5

      screen shot 2021 09 19 at 10.41.27 pm

    • Disable its Mesh Renderer

      screen shot 2021 09 19 at 10.41.43 pm

    • Enable the Box Collider’s Is Trigger option.

      screen shot 2021 09 19 at 10.42.53 pm

  4. With the Box_Trigger game object still selected in the Hierarchy window, add a new Script component and title it “Tunnel_Box_Trigger”

    add trigger script

  5. Open the script in Visual Studio and edit it so that it matches the one shown below. Then save and return to Unity.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;
    
    public class Tunnel_Box_Trigger : MonoBehaviour
    {
        public AudioMixerSnapshot inSnapshot;
        public AudioMixerSnapshot outSnapshot;
        public float fadeInTime;
        public float fadeOutTime;
    
        public AudioClip[] tunnelSounds;
        private AudioSource audioSource;
    
        private bool playerIsInTunnel = false;
    
        private void Awake()
        {
            audioSource = GetComponent<AudioSource>();
        }
    
        private void Update()
        {
            if (playerIsInTunnel == true && audioSource.isPlaying != true)
            {
                AudioClip clip = GetRandomClip();
                audioSource.PlayOneShot(clip);
            }
        }
    
        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                playerIsInTunnel = true;
                if (inSnapshot != null) inSnapshot.TransitionTo(fadeInTime);
            }
        }
    
        private void OnTriggerExit(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                playerIsInTunnel = false;
                if (outSnapshot != null) outSnapshot.TransitionTo(fadeInTime);
            }
        }
    
        private AudioClip GetRandomClip()
        {
            return tunnelSounds[UnityEngine.Random.Range(0, tunnelSounds.Length)];
        }
    
    }
  6. Back in Unity, specify which snapshot should be recalled when the player is either inside or outside the tunnel.

  7. Next, set the time it takes to transition between the two snapshots.

    set snapshots

  8. Now we need some tunnel sounds! Download the sounds below and import them into a new “Tunnel Sounds” folder inside of the existing SFXs folder.

    Tunnel_Sounds.zip

  9. Add a couple of these audio clips as items in the Tunnel Sounds array (list).

    add tunnel sounds

  10. Finally, add an Audio Source component to the Box_Trigger game object.

    add tunnel audio source

  11. Now test to see that everything works by playing the game and moving the player in and out of the tunnel.

Challenge

Once you’ve completed all the previous steps, see if you can apply the knowledge you have gained to fill out the rest of your audio world by adding additional sounds that are…

  • 2D looping audio sources
  • 3D looping audio sources
  • triggered by animation events
  • triggered by a box trigger
  • And finally, give the terrain some textural shifts by creating several other Audio Mixer Snapshots that are triggered as the player moves around the scene.