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.

4.7 (3) · 1 Review

Download materials
Save for later
Share
Update note: Anthony Uccello updated this tutorial for Unity 2019. He also wrote the original.

Creating the visual elements of a game is only half of the work. Once you have your monsters in place, and you have primed all your catapults to fire, a colossal battle may appear underwhelming when the greatest of explosions fail to produce even the tiniest whimper.

When you add sounds to your game — even terrible ones — you may be shocked to discover the added depth they provide to the player experience. Throw in some awesome sounds and you may discover an entirely new game.

Adding sound via code isn’t difficult, but the Unity Editor has an easy-to-use visual editor and works with a wide range of audio file formats, making it even simpler. If you’re looking to add sounds to your next game, this tutorial is a great starting point!

In this tutorial, you’ll:

  • Learn the difference between AudioClips and AudioSources.
  • Discover how to configure various 3D sounds properties.
  • Programmatically play sounds.
  • Learn to maybe, just maybe, throw a pig into a barn.

This is a beginner-level tutorial, but you should be familiar with Unity’s interface before getting started. If you’re new to Unity or need a refresher on the basic concepts check out our Introduction to Unity tutorial.

Note: The models for this tutorial have already been imported into the starter and final projects, so Blender is not required.

Getting Started

You can download the materials for the project using the Download Materials button at the top or bottom of this tutorial. Once downloaded, open the project in Unity (minimum version 2019.1.6f1).

To start off, load up the Main scene in the Assets/RW/Scenes folder and click the Play button.

Introduction to Unity Sound

When the game runs you’ll see a bunch of tractors driving back and forth in front of a red barn. When you press the Spacebar key, a farm animal launches into the air. The goal of the game is to land the animal in the barn without it getting hit by a tractor. If the animal gets hit by a tractor, it dies; if it makes it into the barn, it’s a goal!

Right now, of course, it’s totally silent — not a moo to be heard! It’s your job to bring the game to life with sound.

Adding Your First Sound Effects

The game is set up to launch a random animal: a cow, pig or sheep. Each one will play a different sound, so your first task is add the sound effect of the animal being launched.

The project has already been set up with prefabs for each animal. In the Project window, open the Assets/RW/Prefabs window and select the cow prefab.

Note: Never heard of a prefab? Check out our Introduction to Unity tutorial to get up to speed.

Introduction to Unity Sound

The first thing this prefab needs is an AudioSource component to play sound. In the Inspector, click the Add Component button and type in Audio Source. Then, select the Audio Source option that appears.

Introduction to Unity Sound

The cow prefab can now play audio, but it needs an AudioClip to play. Currently, the AudioClip on the component you added is empty.

Introduction to Unity Sound

You may be wondering: what exactly is an AudioSource? How does it differ from an AudioClip? You could assume the component functions by their names, but assuming can be a dangerous thing — especially with flying livestock on the line — so you’ll need to cover some basics.

How Sound Effects Work in Unity

Playing sounds in Unity requires an AudioSource and AudioClip at a minimum.

An AudioSource is what will actually play the sound in 2D or 3D space. In 3D space, the sound’s volume can vary based on how far the AudioSource is from the object listening to it. The actual listening takes place from whatever object the AudioListener component is attached to, which in this case is the Main Camera.

Note: If an AudioSource is set to play sound in 2D space it will play at a consistent volume regardless of the distance from the AudioListener.

An AudioClip is the actual audio file that the AudioSource will play.

One important thing to note is that the AudioSource is a component. That means it’s an object that inherits from Unity’s MonoBehaviour class and can be attached directly to any Unity GameObject.

The AudioClip is a variable that can be set on the AudioSource (i.e., each AudioSource will have one AudioClip). You can attach components through the Unity editor and through code, though this tutorial is only going to use the editor.

A key part of playing sounds is setting up triggers for AudioSources to play. A trigger is just that: A condition that causes the AudioSource to play.

In this tutorial, the code has been pre-configured to contain only what you need for the audio-playing portions; behind the scenes is the code that uses Unity’s physics engine.

Note: If you are interested in learning how these physics triggers work, check out Unity’s Physics System.

Adding AudioClips

Open the Assets/RW/Sounds folder in the Project window and find the moo sound file. With the cow prefab still highlighted in the Inspector, drag the moo sound file onto the AudioClip.

Make sure that Play On Awake is enabled.

This means that the sound file will play as soon as the object is created, which is exactly what you want. When the animal is launched toward the barn, it will let out an animal roar — or moo, in this case!

As a challenge, do the same thing for the Sheep and Pig prefabs.

Need help? Just open the spoiler below to find out how.

[spoiler]

  1. Click on the sheep prefab. In the Inspector, click the Add Component button. Select the AudioSource component in the Audio category.
  2. Drag the baa sound file to the AudioClip field.
  3. Make sure the Play On Awake box is checked.
  4. Click on the Pig prefab. In the Inspector, click the Add Component button. Select the AudioSource component in the Audio category.
  5. Drag the oink sound file to the AudioClip field.
  6. Make sure that the Play On Awake box is checked.

[/spoiler]

At this point, all three animal prefabs have their voices added. To test it out, enter Play mode and press Spacebar to launch an animal. You should hear each animal playing its respective sound as it flies towards the barn.

moo

Oinks and moos and baas — oh, my!