Unity – Quick Tips for Implementing Audio

In this post, you will learn the basics of audio on Unity, from importing files to make your sound object interact with the environment.

     Import Audio Files: simply drag your audio file into the Project tab. If you click on it you’ll see its settings:

Unity will call any audio file an Audio Clip, which will be important to remember when creating a Javascript for determining its behaviour. An Audio Clip is basically a container for the audio data. From this Inspector tab you are able to compress your Audio Clip to fit the needs of your game on memory usage.

Assign the Audio Clip to an Object: In order to have your sound playing, you’ll need to tell what object will hold that sound. And for that you need to create an Audio Source. From the Hierarchy tab, you click on which object you want to assign your sound to.

Background music: Let’s say you want to assign a music theme playing all the time; for this, you should first create an “Empty” GameObject from the drop-down menu. This an object that will never be seen on the game but you can attach sounds and scripts to it. Then, from the Component menu, select Audio > Audio Source. Unity will automatically assign the Audio Source to the selected item in the Hierarchy tab and show everything in the Inspector tab:

The first thing you need to do is, obviously, to select an audio file you had previously imported to the project. You do this by clicking on the circle on the right of the dialogue that shows which file is associated to that Audio Source. The audio files are being stored on the Assets folder:

Play On Awake means the audio will play immediately once the project is on play mode. In this case we’ll want the theme to be looped as well.

And that’s it for music. Remember that if it is a background generic song, the Audio Clip must be 2D in order to play equally throughout the walk-through.

Trigger Sounds on a given Action: Now, we want to create an Object that will trigger a sound every time the player collides with it. For this, let’s suppose we have a cube (GameObject > Create Other > Cube). Just as in the previous example, we need to assign an Audio Source to it and select an Audio Clip from the inspector. Select “is trigger” on the Box Collider properties to allow the following events to happen.

To determine how the sound it’s going to be triggered, we need to create a script. So, from the create pull-down menu in the Project panel, we select Javascript; I named it “CubeBehave”. We edit the script by clicking “open” in the Inspector tab. It should go like this, if you decide to call your variable “Sound”:

var Sound : AudioClip;

function OnTriggerEnter(){
audio.PlayOneShot(Sound);
}

The variable “Sound” automatically refers to the audio clip in the Audio Source attached to the object. The introduced function is for the trigger, and the function will be to play once the Audio Clip if the player collides with the object.

In the Inspector window, on the script tab, make sure you assign your Audio Clip to the file you want it to be played on the event, on the “Sound” entry that we determined being your variable:

This now shall work properly, you can try it with a First Person Controller, imported from the Prefabs effects.

But if the sound we’re working on has to be stopped after a while or we want our background music to stop after playing for 5 minutes, we can easily do that as well  with Javascript. So, we add a new script to our EmptyObject containing background music and simply type in the following:

function Start()
{
audio.Play();

yield WaitForSeconds (300);

audio.Stop();
}

Here, the audio in the associated Object will play, and after the number of seconds you entered, it will stop.

You can also tell Unity to stop after the audio has been played one time (if you’d like to give it another order after it, if not, you should use PlayOneShot):

function Start()
{
audio.Play();

yield WaitForSeconds (audio.clip.length);

audio.Stop();
}

This will work even if you had selected loop on the Inspector tab, it takes priority.

References:

http://www.gameaudio101.com/Unity-Audio-Tips.php

http://docs.unity3d.com/Documentation/ScriptReference/

9 thoughts on “Unity – Quick Tips for Implementing Audio

      1. thanks! could you please tell me how to make music or sound start in as many minutes or seconds.
        apology for the language not written in English.
        THANKS! I hope your answer!

  1. Hey this was helpful thank you…. am totally new to this so here’s a question…..is there a java script to control an audio clip in such a way that it will play only when a particular object is shown in the scene??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s