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:
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.
var Sound : AudioClip;
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.
yield WaitForSeconds (300);
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):
yield WaitForSeconds (audio.clip.length);
This will work even if you had selected loop on the Inspector tab, it takes priority.
9 thoughts on “Unity – Quick Tips for Implementing Audio”
Reblogged this on Create 3D Games and commented:
Very useful tips for making your audio sound good in your game.
Thank you. There will be updates coming up soon on this one. Cheers.
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!
Hi! I’m not sure what you asked for. Do you want an audio file to start at some point in time after starting the game?
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??
Hi, Jeffrey. Thanks. How and / or what is causing this object to appear? Is it with disabling and enabling the renderer component?