Using Audio in Unity

I am the Lead Sound in my game design group, so I have done research on how to use Unity’s audio system, both through the editor and through code. I found it a bit tricky at first, so I’m hoping that this post might clear some things up and help someone else understand.

For my fellow students: if you find this helpful, make sure to show it to your own group’s Lead Sound! And if your team has any sound issues, write to me and maybe I can help you 🙂

First of all. To be able to hear sound in your game, you need to have an “AudioListener” component attached to an active GameObject. Unity will automatically attach listeners to “Camera” objects, so you project most likely already has one.
Don’t have more than one AudioListener active at the same time, because this can cause the audio to stack, which can make the volume unpleasantly high (and, because Unity says not to! (but not why…)).

The component responsible for actually playing sound is called “AudioSource”. AudioSources can be attached to any GameObject, but unlike listeners, you may have as many sources as you want. So you can attach an AudioSource to objects that you want to manage their own audio.

AudioSources look like this:

AudioSource_no3D.pngRead about all the settings here.
I will go through the most basic ones.

AudioClip is a sound/music file.
If “Play On Awake” is enabled, the AudioClip will play as soon as the AudioSource becomes active.

Output.
It’s perfectly fine to leave this as “None”.
Think of “Audio Mixer Groups” as channels. You might want to create one mixer for sound effects, and another for music. That way you can separately change/mute the volume of one channel, and still have the other one play sound.
If you don’t create any Mixers, Unity will still play all sounds. Mixers exist to give more control over the audio.

Volume is the volume for this specific AudioSource, not the master volume for the whole game.

Pitch – get to know this one, for you own sanity’s sake.
“Pitch” is very basically “brightness”. Increase the pitch, and you will increase the brightness of the sound. The best example of this is… sigh… the singing mice in Cinderella
If you change the pitch a little bit up and down every time you shoot, the sound will appear to be slightly different. This will make your shooting sound more interesting, but don’t overdo it, because if you change it too much it will sound weird and have a negative effect instead. I’ll give you a code example on how to do this further down.

These are the most important ones when you are dealing with 2D perspective games where sound doesn’t need a specific “location” in the world. In 3D games, most sound effects need to come from a location and the volume needs to be relative to the distance to the player (the “ears” of the game).

But even if your game is in 2D, if your game is an exploration game there’s one cool thing you can do to make the sound expand your world and help give the player feedback on where enemies/ETC. are located.

AudioSource_3D.png

If you expand “3D Sound Settings”, you will get access to Min and Max Distance. These are radiuses around the AudioSource that shows you how close the AudioListener has to be to the source to hear the sound. The default Max of 500 is very big, so zoom out in the scene to see it.

There are two things you MUST do to be able to use the Distance settings.

  1. Move the Spatial Blend slider from “2D” to “3D”.
  2. Remove the AudioListener component from the Camera and attach one to the Player, or whatever object you want to be your ears.

 

One thing to keep in mind is that if you disable an AudioSource that is currently playing audio, or disable/destroy the parent GameObject it’s attached to, the audio will stop. To get around this, you can have a GameObject called, for instance, “SoundManager”, attach an AudioSource to it, and play sound through it via code. Or even instantiate an AudioSource prefab!

Code examples:

Instantiating an AudioSource prefab, using it and destroying it:
http://pastebin.com/ZpCr7pCJ

Changing pitch, for example when playing shoot sounds:
http://pastebin.com/wP6N9kEg

About Robin Gerndt

2016 Programming