Adding Audio Files in Unity

In the previous lessons, we've created a basic driving game in Unity and displayed lap times on screen. But it's all done in silence. In this lesson, you'll learn how to add audio to your game.

 

Adding Sounds

Expand your Car item in the Hierarchy and then select the Main Camera. Now look at the Inspector on the right. You'll notice that it has an Audio Listener:

Main Camera showing a Audio Listener attached

Every Unity scene has one and only one Audio Listener. If you try to add more, you'll see errors appear at the bottom of your screen. Think of the Audio Listener as a pair of ears, listening out for sounds. The idea is that you then add Audio Sources to your scene and the Listener will hear them. You can have as many Audio Sources as you like. For example, if you had a scene with a player sitting in a chair, the player's ears would be the equivalent of an Audio Listener. The player's TV would be an Audio Source, as too would be a dog barking outside, a ceiling fan, a radio, and any other sound that you want your player to hear. You can move the Audio Sources closer or further away from the Listener. The closer they move to the Listener the louder they will get, the further away, the quieter they will get.

Let's add an Audio Source to our car. First, download this audio file to your computer (Right-click > Save As):

engine.wav

Create a new folder in Unity, inside the Assets folder. Call it Audio.

Now drag and drop the file you've just download into your new Audio folder. (If dragging and dropping doesn't work, double click your new Audio folder to move inside of it. Right click and select Import New Asset from the menu that appears.)

Whichever way you choose, you should see an audio file the project area of Unity:

The Project folder in Unity showing an audio file

The audio file is pretty small in size. And, in actual fact, it's just an electric shaver recorded for 2 seconds in Audacity!

But click the file to select it. You'll see the Inspector changes:

The Inspector showing properties of an audio file

Here, you'll see lots of information about your audio file. If you press the play button indicated by a red square in the image above, you'll be able to hear the sound.

Let's add this audio file to the car, though, as it's just sitting there doing nothing, at the moment. To add an audio source to a game object, you add it as a component.

With the car selected in the Hierarchy, move over to the Inspector. Click the Add Component button at the bottom. When the menu appears, you can either select Audio > Audio Source, or type Audio in the search box. Then select the Audio Source option:

The Add Component list showing the Audio Source item highlighted

When you do, you'll see a new component appear in the Inspector:

An Audio Component attached to a game object

Notice all the settings you can have for the Audio Source component. (We're going to be playing around with Pitch, soon.) By default, Play on Awake is checked. This means the engine sound will play when the game loads. For other sounds, like gunshots, for example, you wouldn't want this checked. But it's OK for our little engine.

Put a check in the box for Loop, though, as in the Image above.

Right at the top of the Audio Source component, there is an Audio Clip item. You can simply drag your audio clip onto the box. So do that now, as in the image below:

Dragging and dropping an audio file onto the Audio Clip item in the Inspector

Your Inspector will then show which clip it's supposed to play:

The Audio Source options showing in the Inspector

Now play your game. You should hear the engine sound as soon as the game starts.

OK, it's a little on the whiney side, for now. Let's change the pitch with some code.

With the Car selected in the Hierarchy, click the Add Component button in the Inspector again. Add a New Script. Call it AudioEngine. When you create the new script, you should now have three scripts in your Scripts folder:

Three C# scripts in a folder in the Project area of Unity

With the Car selected, you should see that you now have two scripts attached to it. (You can have as many scripts as you want attached to a game object):

Two C# scripts attached to the same game object in the Inspector

Double click your new script to open it up in your coding editor. (Strictly speaking, we didn't need a new script. We could have just added the new code to the CarMove script. But it's nice to keep code separate.)

We don't need an Update method in this script, so just delete that, as well as any comments you don't want.

At the top of the code, but inside of the class, enter this line:

private AudioSource source;

This is a variable of type AudioSource. We're giving it the name source. But you could call it something else, if you wanted to.

In the Start method, add this line:

source = GetComponent<AudioSource>();

The script is attached to the car, and the car has an Audio Source attached to it (the engine noise). So we get the Audio Source component for the car. This ends up in source.

Because the source variable now refers to the Audio Source Component attached to the car, we can play around with the Audio Source settings. One of these is pitch:

The Pitch item highlighted in the Inspector

The pitch slider goes from -3 to positive 3. You can set this with code by doing this:

source.pitch = 1.25;

Let's try a random value, though. Add this to your start method

source.pitch = Random.Range(-1.2f, 1.2f);

Your coding window would then look like this:

C# code in Unity to set the Pitch of a sound

Start your game with the car selected in the Hierarchy and the Inspector showing on the right. The pitch value in the Inspector will have a random value from -1.2 to positive 1.2 (we chose these values because the pitch sounds awful at lower or higher values.) With the game still playing, move the pitch slider in the Inspector. You'll be able to judge which pitch sounds better for the car. A value of -0.2285 sounds better. In your coding window, you could hard-code this, instead of having the random value:

source.pitch = -0.2285f;

Or you could use Lerp.

 

How to use Lerp in Unity

Lerp is linear interpolation. You take two values, a minimum and a maximum, then add a third value. The third value is a value between 0 and 1 and acts to smooth out your min and max value. So, if your minimum value was 1 and your maximum value was 10, the third value could 0.5. This would give you a nice transition from the min and max. So instead of a harsh line like this:

A graph of a diagonal line

You'd get a nice curve, like this:

A graph of a curved line to indicate linear interpolation

Lerp is used in gaming to do things like smooth animation, so it doesn't look too jerky. You can also use it on audio. Try this. Comment out your previous pitch line. Add this instead:

source.pitch = Mathf.Lerp(-1.2f, 1.2f, Random.Range(-1.2f, 1.2f));

The first two values inside the round brackets of Lerp are for the minimum and maximum values. The third one is for your position between the two values. We're using a random number here.

Play your game and note the sound the car makes. Is it better? Worse, or just the same? You might need to keep restarting your game to get a decent random value. Once you're happy with it, replace the random part:

source.pitch = Mathf.Lerp(-1.2f, 1.2f, 0.22f);

You can change other values for your Audio Source with code. For example:

source.volume = 0.8f;

(The volume values go from 0 to 1.)

source.mute = true;
source.loop = false;

And lots more.

But let's leave audio there. We'll come back to it in later lessons. To wrap up this first game tutorial, let's see how to import stuff from the Unity Asset store. First, we'll get a better car. This comes with proper steering and proper car sounds. The scripts for these can be very complicated, as you'll see.

<--Back to the Unity 3D Course Contents Page