Unity Audio Tutorial: Getting Started

Adding sounds to your game is just as important as developing amazing shaders. In this tutorial, you’ll learn the basics of sound development in Unity by chucking animals into a barn. By Anthony Uccello.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Sound Not Playing?

The AudioSource can play a wide variety of file formats, including the common .mp3 and .wav formats. If you ever find that your sound file is not playing, first check here to see if the file format is supported.

In some cases, Unity will encode sound files into formats such as .mp3 to take advantage of hardware decoding. In cases where you need to supply an .mp3 audio file, the .mp3 files will be re-encoded and result in additional quality loss.

Note: When choosing a sound format to use, you should prefer a lossless format such as .ogg or .wav.

Since this tutorial was prepared ahead of time, the file formats should be fine. So if the sounds aren’t playing, check the following:

  1. Is the volume set too low? Check the volume level on each of AudioSources.
  2. Is the AudioSource component disabled? If the component is disabled, it won’t play the sound.
  3. Is Play On Awake toggled? You won’t always want this on, but if you expect a sound to play as soon as the object is created, check that this is toggled on. It will play in the same frame the object is created.

Polishing Sound

The game is coming along great so far — all the animals are making their sounds as they fly towards the barn. But a few things are still missing:

  • The tractors aren’t making any sound.
  • There’s no success sound when an animal makes it to the barn.
  • There is no death sound if an animal hits a tractor.

Adding these sounds will give this game this game some real polish.

Adding Tractor Sounds

To add a buzzing sound to the tractors as they move back and forth, first, hold Control (on PC) or Command (on Mac), and click on all three tractors in the Hierarchy. Batch-add an AudioSource, then add the tractor sound clip just like you did with the animal sounds. Make sure Play On Awake is checked.

With all the tractors hooked up with sound, click the Play button.

It’s great the tractors have sound, but with all three playing at the same time it’s hard to distinguish between them. Plus, it’s really loud.

As mentioned earlier, 3D sounds can play with a decrease in volume the farther away the AudioListener is from the AudioSource. Adding a falloff effect (so that the farther the tractors are from the Main Camera, the quieter they are) will make things sound a lot better.

Click on tractor in the Hierarchy; in the Inspector, look at the AudioSource component. There will be a drop-down area called 3D Sound Settings. Here, you can set the Doppler Level, the Rolloff and the Distance variables.

The Doppler Level is exactly what it sounds like: sound intensity that varies to mimic the Doppler effect. The Rolloff Level is how much the volume quiets down the farther its source is from the AudioListener.

This is the closest tractor to the player, so its sound falloff should be more dramatic than the back two. Set the Volume Rolloff to Linear Rolloff, which will make the effects more pronounced. Next, set the Min Distance to 1 and the Max Distance to 9. Set the Doppler Level to 0.68.

This will make the front tractor sound loud as it approaches the middle of the screen (as it moves closer to the listener on the camera) and quieter as it moves off screen. Click Play to see this for yourself.

For the back two tractors, their sounds should be more like background noise; only the front tractor should have the exaggerated sounds.

To turn down the intensity of the back tractors:

  1. Start by selecting tractor2 in the Hierarchy.
  2. Set the Volume Rolloff to Linear Rolloff.
  3. Then, set the Min Distance to 1 and the Max Distance to 30.
  4. Set the Doppler Level to 0.68.
  5. Next, select tractor3 in the Hierarchy and set the Volume Rolloff to Linear Rolloff.
  6. Set the Min Distance to 1 and the Max Distance to 50. Set the Doppler Level to 0.68.

Click Play and listen to the different sound levels of the tractors as they move back and forth.

The tractors are way too loud, especially with three of them playing at once. Set the Volume slider on their AudioSource to 0.4.

If you play the game for more than 40 seconds, you’ll notice that the tractor sounds cut out. That’s because their AudioSource hasn’t been set to loop. To change this, click the Loop box for each tractor’s AudioComponent.

Adding Hit Sounds

When the animals hit a tractor, unfortunately, they get clobbered by it. What’s even more unfortunate is that there is currently no sound when that happens! As mentioned earlier, the tractors are using the Unity physics engine and listening for collisions with the animals. When an animal collides with a tractor, it tells the animal to die — that’s when the sound needs to play.

First, you will need to attach the AudioClip of the death sound to the Tractor component. To do this:

  1. Hold Control (on PC) or Command (on Mac). Then click on the three tractors in the Hierarchy.
  2. Select the three tractor GameObjects and click the dot next to the Death Sound field of the Tractor component in the Inspector.
  3. Next, select the death sound file from the Select Audio Clip pop-up window shown below.

To code in the animal death sound, start by double-clicking on the Tractor C# script to open it in your editor (found in the Assets/RW/Scripts folder). Before the closing brace of the if statement in the OnCollisionEnter method, add the following code:

audioSource.PlayOneShot(deathSound);

OnCollisionEnter should look like this when you’re done:

void OnCollisionEnter(Collision col) 
{
    if (col.gameObject.GetComponent<Animal>() && !col.gameObject.GetComponent<Animal>().isDead) 
    {
        Destroy(col.gameObject);
        audioSource.PlayOneShot(deathSound);
    }
}

Press Control-S (on PC) or Command-S (on Mac) to save the script.

PlayOneShot() plays the sound once, since you’re passing in the sound that you added to the tractor script and then playing it once from the tractor. The reason the tractor (rather than the animal) should play the sound is that, when the animal is hit by the tractor, the literal GameObject that was the animal is deleted. Since it no longer exists, the animal can’t play a sound. You could turn the mesh off and play the sound from the animal that way, but that is more complicated.

The benefit of using PlayOneShot() is that the sound will play to completion even if PlayOneShot() is called again. If this were not the case, the animal sounds would cut each other off, sounding jerky to the user.

Click Play and exercise your darker side by intentionally launching animals into the tractors instead of aiming for the safety of the barn. You should hear a “squish” sound as they collide with the tractor.

squish