Curriculum
Course: How to develop 3D Games with Unity
Login
Video lesson

Audio

Summary

Now you’re ready to add the finishing touches to your game with engaging audio.

In this tutorial, you’ll:

  • Explore the different kinds of audio that can be used in a game
  • Add a soundtrack and range of audio effects
  • Write a script to control the use of audio in your game
  • Once you’ve completed this tutorial, your game will be finished and you’ll be ready to create a build! 

Implementing Non-Diegetic and Diegetic Audio in Unity

Non-Diegetic Audio Setup

  1. Create Ambient Audio Source:
    • Create an empty GameObject named “Ambient”.
    • Set its position to (0, 0, 0).
    • Add an Audio Source component by dragging the SFXHouseAmbience Audio Clip from Assets > Audio onto the Inspector.
    • Ensure Spatial Blend is set to 2D.
    • Enable ‘Play On Awake’ and ‘Loop’.
    • Adjust Volume to 0.5.
  2. Duplicate for Game Ending Sounds:
    • Duplicate the Ambient GameObject twice, renaming them to “Escape” and “Caught”.
    • Disable ‘Play On Awake’ and ‘Loop’ for both.
    • Set Volume to 1.
  3. Assign Audio Clips:
    • For ‘Escape’, assign SFXWin as the AudioClip.
    • For ‘Caught’, assign SFXGameOver as the AudioClip.
  4. Organize Audio Sources:
    • Create an empty GameObject named “Audio”.
    • Make ‘Ambient’, ‘Escape’, and ‘Caught’ child objects of ‘Audio’.

Updating GameEnding Script

  1. Modify GameEnding Script:
    • Add AudioSource references for exitAudio and caughtAudio.
    • Implement a boolean m_HasAudioPlayed to ensure audio plays only once.
    • Modify EndLevel method to include AudioSource parameter and logic for playing audio.
    • Update calls to EndLevel in Update method with corresponding AudioSource arguments.

Implementing Footsteps Audio

  1. Set Up Footsteps Audio for JohnLemon:
    • Select JohnLemon GameObject and add an AudioSource component.
    • Set AudioClip to SFXFootstepsLooping.
    • Keep Spatial Blend fully 2D.
    • Disable ‘Play On Awake’ and enable ‘Loop’.
  2. Update PlayerMovement Script:
    • Add an AudioSource reference.
    • Assign the AudioSource in the Start method.
    • Modify FixedUpdate to play footsteps audio when JohnLemon is walking and stop when stationary.

Final Audio Adjustments

  1. Adjust Audio Listener:
    • Remove Audio Listener from Main Camera.
    • Add Audio Listener to JohnLemon GameObject.
  2. Update JohnLemon Prefab:
    • Apply changes of AudioSource and Audio Listener to the JohnLemon Prefab.
  3. Add Audio Source to Ghosts:
  • Select a Ghost GameObject and open the Prefab for editing.
  • Add an Audio Source with SFXGhostMove AudioClip.
  • Set Loop to enabled, Volume to 0.4, and Spatial Blend to 1 (fully 3D).
  1. Tweak 3D Sound Settings:
  • Adjust Max Distance to 10.
  • Change Volume Rolloff to Custom Rolloff.
  1. Correct Ghost Sound Direction:
  • Set the Spread property to 180 in the Audio Source for Ghost Prefab to create a directionless sound.
  1. Test the Game:
  • Save all changes and test the game to experience the new audio effects.

Final Code for GameEnding and PlayerMovement Scripts

 

 

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class PlayerMovement : MonoBehaviour

{

    public float turnSpeed = 20f;

 

    Animator m_Animator;

    Rigidbody m_Rigidbody;

    AudioSource m_AudioSource;

    Vector3 m_Movement;

    Quaternion m_Rotation = Quaternion.identity;

 

    void Start ()

    {

        m_Animator = GetComponent<Animator> ();

        m_Rigidbody = GetComponent<Rigidbody> ();

        m_AudioSource = GetComponent<AudioSource> ();

    }

 

    void FixedUpdate ()

    {

        float horizontal = Input.GetAxis (“Horizontal”);

        float vertical = Input.GetAxis (“Vertical”);

       

        m_Movement.Set(horizontal, 0f, vertical);

        m_Movement.Normalize ();

 

        bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);

        bool hasVerticalInput = !Mathf.Approximately (vertical, 0f);

        bool isWalking = hasHorizontalInput || hasVerticalInput;

        m_Animator.SetBool (“IsWalking”, isWalking);

       

        if (isWalking)

        {

            if (!m_AudioSource.isPlaying)

            {

                m_AudioSource.Play();

            }

        }

        else

        {

            m_AudioSource.Stop ();

        }

 

        Vector3 desiredForward = Vector3.RotateTowards (transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);

        m_Rotation = Quaternion.LookRotation (desiredForward);

    }

 

    void OnAnimatorMove ()

    {

        m_Rigidbody.MovePosition (m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);

        m_Rigidbody.MoveRotation (m_Rotation);

    }

}