Unity Basics Working with Audio

tutorialgame-audio-unity

Unity Basics: Working with Audio

📝 Note

Table of Contents:


Created by Daniel Dehaan, 2021-09-20

📝 Note

Project Setup


  1. Create a new 3D Unity project and name it “FA22_MUSC-601_Week-03_First initial Last name” (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

      📝 Note
2. Click "Open in Unity".
    
    ![[screen_shot_2022-09-25_at_7.11.42_pm.png]]
    
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.png]]
    
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.png]]
    

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.png]]

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.png]]

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.png]]

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.png]]

> [!note]

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.

📝 Note

Adding Footsteps


  1. Navigate to Assets > Standard Assets > ThirdPersonController > Character > Animations > Locamotion—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 = turn camera
    • Click+Drag = slide camera
    • Option/Alt + mouse wheel up/down to zoom in/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.

    📝 Note
  1. 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.

  2. Open new Script in Visual Studio.

  3. 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)];
        }
    }
  4. Save and close the script.

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

    Footstep Audio Files.zip

    create sfx folder

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

  7. Test by running the game.

📝 Note

adjust music level

📝 Note

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 and 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 the 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/recall visibility setting 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’ 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 call “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

📝 Note

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 mixers 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 the Music Down snapshot selected, lower the volume of the Background Music group.

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

📝 Note
📝 Note

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 by

    1. Selecting it in the Hierarchy window under Environment > Greybox > Tunnel_Prefab
    2. Then hover the cursor over the Scene window and pressing the ‘f’ key

    📝 Note
  1. 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

  2. With the new Box_Trigger object selected in the Hierarchy window, navigate to the Inspector widow and adjust all of the following parameters to…

    • 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

    • And enable the *Box Collider’s ‘*Is Trigger’ option.

      screen shot 2021 09 19 at 10.42.53 pm

  3. 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

  4. 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)];
        }
    
    }
  5. Back in Unity, specify which snapshot should be recalled when the player is either inside or outside the tunnel.

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

    set snapshots

  7. 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

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

    add tunnel sounds

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

    add tunnel audio source

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

📝 Note
📝 Note

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.