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 whisper.
But when you add sounds to your game — even terrible ones at that — you may be shocked to discover the depth that they provide. Throw in some awesome sounds and you may discover an entirely new game.
Adding sound via code isn’t difficult, but Unity, which has an easy-to-use visual editor and works with a wide range of audio file formats, makes it even simpler. This tutorial will show you how to add sounds to the game Barn Blaster, via the Unity editor and added code. 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 properties of 3D sounds.
- 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. If you are new to Unity or need a refresher on the basic concepts, check out our Introduction to Unity tutorial.
You can download the materials for the project using the Download Materials button at the top or bottom of this tutorial. Open the project in Unity (minimum version 2018.2.10f1).
To start off with, open the Main scene in the Assets ▸ RW ▸ Scenes folder and click the Play button.
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! You’ll 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.
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.
The cow prefab can now play audio, but it needs an AudioClip to play. Currently, the AudioClip on the component you added is empty.
You may be wondering: what exactly is an AudioSource? How does it differ from an AudioClip? You can assume the 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
To play sounds in Unity, you use an AudioSource and an AudioClip.
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 AudioListener, which, in this game, is on the camera).
You can set an AudioSource to play sound in 2D space, which means 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
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 only uses 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, you will edit code that 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.
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.
- Click on the sheep prefab. In the Inspector, click the Add Component button. Select the AudioSource component in the Audio category.
- Drag the baa sound file to the AudioClip field.
- Make sure the Play On Awake box is checked.
- Click on the Pig prefab. In the Inspector, click the Add Component button. Select the AudioSource component in the Audio category.
- Drag the oink sound file to the AudioClip field.
- Make sure that the Play On Awake box is checked.
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.
Oinks and moos and baas — oh, my!