Megabite #14 - Adding Music and Sound Effects into a Game

Unity makes everything in the world of sound fairly easy to understand and manipulate to your liking.  Even so, there are a lot of settings that should be understood before diving in to the audio components to help with performance and overall game size.

Import

First, you’ll want to import sounds into your project.  While you can do that with any type of sound file, you’ll want to examine the settings before attaching it to a prefab or GameObject.  An Audio Importer component will be automatically attached, and from there you’ll be able to set your settings to maximize performance.

First, take a look under Audio Format.   Whether or not you want your sound compressed will be dependent on what you want to use the sound for.  While uncompressed sounds will have a higher level of quality, this setting is best used for very short sound effects as opposed to something like in-game music.  In conjunction with this selection, you’ll also want to pay close attention to the “Load Type” section, where you can control how the audio data is pulled from disk and into memory.

Using “Decompress on Load” will be best for those very short sounds – this will keep your game from locking for a frame or two when a new sound happens within a scene.  Basically, you are opting to have the sound readied when the scene is loaded.  For larger sounds, you can choose to keep the file “Compressed in memory”, which will decompress the file as it is played.  If your file is highly compressed, this will cause a bit of load on your target machine, but it will keep initial load times down and keep your available memory more free.  Lastly, you can opt to simply stream the file from the hard drive by selecting “Stream from Disk” – this will save you on memory and loading, but because you are accessing the disk you wouldn’t want to use this setting for more than a few tracks at the same time.  Ideally, just use this setting for music since you will likely only have a1 song playing at once.

The level of compression (if you are choosing to compress your file) will be selectable via this window as well.  You want to aim to compress as much as possible while keeping your file usable for your own purpose.  This may be a setting you’ll want to experiment with – audio files can quickly drive up the file size of your game.

Listen

In each scene, you’ll want to always have just one audio listener.  This will be attached to a main camera by default, which effectively places your player’s head wherever the camera is.  However, you may want to opt into having your listener attached to your game character or some other game object.  If you add the audio listener component to any other object, you’ll want to make sure there is always one in existence in the event your character dies, as an example.  Without a listener, all sound will disappear.

In the cases where I’ve done this myself, often times the best result was achieved by disabling the listener from the player and immediately enabling one attached to the main camera (in the same function that would kill the player).  When the new player spawns, you simply reverse the process in the spawning function.

Effects

Sound effects are versatile, and you can basically opt to use them however best fits your game.  For instance, you can attach a sound to a GameObject and check the box labeled “Play on Awake” – if this is done, the sound will play as soon as the object exists.  While this may seem to be a good object for something like a bullet, you should understand that if the object is destroyed, the sound will disappear with it.

For our purposes, when you examine from a common FPS type perspective, picture we are firing a rifle.  The bullet itself may have a whizzing sound as it flies through the air, but it’s highly likely we’re going to want to attach the gunfire sound to the rifle itself.  This will do two things: keep the firing sound at a constant volume (keeping it from fading as it gets further away), and also keep the sound playing in the event the bullet is destroyed immediately.

Sounds can be added into the inspector like other variables by declaring them as AudioClip.  Once that is done, you can really use basic scripting to customize to your liking.  For example:

var myFireSound : AudioClip;
var myReloadSound : AudioClip;
var reFire : Float;

function Update () {
if (Input.GetKey(“space”) && Time.time > reFire) {
reFire = Time.time + 1;
FireGun();
}

if (Input.GetKey(“r”) && Time.time > refire) {
reFire = Time.time + 1;
ReLoad();
}
}

function FireGun () {
audio.clip = myFireSound;
audio.Play();
}

function Reload () {
audio.clip = myFireSound;
audio.Play();
}

Music

I don’t doubt that one of the most common audio questions relates to adding in-game music.  Unity doesn’t have a specific component for it, but it’s fairly easy to design your own.  In the same vein as the example above, the easiest way will be to attach your music files as inspector variables (perhaps even as an array), and play them accordingly.

An example of this might be something like this:

var musicArray : AudioClip[];  //attach as many tracks as you want in the inspector

function Start() {
ChangeTrack();
}

function ChangeTrack() {
var trackNum = Random.Range(0,musicArray.Length);
audio.clip = musicArray[trackNum];
audio.Play();
WaitTrack();
}

function WaitTrack() {
yield WaitForSeconds(audio.clip.length);
ChangeTrack();
}

Something like this would work for a quick music solution, and extra customization could be added for different areas or situations, etc.  Of course, the GameObject this script is attached to will need to have an audio component – simply don’t attach a specific clip and instead load up the array we’ve just created.

Rolloff

The way sound reacts to its distance from an audio listener component is known as rolloff.  This can be customized like everything else of course, but the easiest to understand will be a simple linear rolloff.  You can find this option available as a visual graph on any audio source component, and depending on what you are doing within your game, you will simply want to be sure that your listener falls within the ranges that the sound will make available.

The easiest way to acclimate yourself with how rolloff affects your game is to play the title within the editor.  As with all of these scripts and components, learning by doing will be invaluable.

Thanks for reading – if anyone gets hung up or has audio issues, feel free to drop me a line in the comments here.  :)