101 Games

 

Unity 101 Audio Tips: by Catherine Arthur

 

unity-audio

When I started working in Unity 3D a few months ago I found the Unity Manual a great resource, but it’s written for game designers and programmers who need to have a large working knowledge of everything in Unity. I had to work through a lot of information on mesh renderers, particle effects, things I didn’t really need in order to get to the few bits about audio. Even then it took me a while to see how it all fit together. If you’re just getting into audio in Unity 3D I hope this article can save you some time. When you first import your .wav, .ogg, .mod, or what have you into Unity and click on it, you will see Unity calls your file an Audio Clip. All sound in Unity starts with an Audio clip. Audio clips are basically just your audio file with a few additional properties. From the inspector tab you can adjust the compression settings and preview the file size and compressed sound quality. Unity can also force stereo files to mono, to further reduce the size of the audio file in the built game.

audio-clip

audio clip

Once you have an Audio clip, you need something to play it. That’s what Audio Sources are for. Audio Sources hold your audio clip and give it form in the game world. They are attached as components to game objects. If your audio clip has 3D sound enabled, the sound will be positioned in 3D space, you’ll be able to hear the sound pan and attenuate as the game object moves around.

audio-source

So you have an audio source to play your sound, but how do you tell it to play? If your sound is just an ambient looping sound or background music you can just check the play on awake and looping boxes in the audio source and your sound will just happily play away until it’s parent is destroyed. Most of the time though you’ll want your sound to be triggered by some in game event and usually this means you’ll have to do some scripting. Scripting in Unity is a huge topic that I can’t go into to deeply here, but I can give a short example.

This script plays a sound when ever the object and the player collide. It’s attached to a game object along with a collision box and an audio source that has an impact sound effect. Then, whenever the object’s collision box is invaded the script checks to see if it’s the player doing the invading and if it is, the sound is played. The variable ‘audio’ is a default variable that automatically refers to the audio source attached to the object.

audio-code

Another common way to trigger a sound is through an animation event. An animation event calls a function on a specific frame of the animation. Here, the PlayFireworks() function will be called when the animation timeline reaches the blue mark. Then within the PlayFireworks() function you could call audio.Play() to play an explosion sound to go along with the pretty fireworks.

So if audio.Play() is called and no one is around to hear it, does it make a sound? No. It doesn’t. Not to the player anyway. The player needs some sort of proxy in the game world to pick up all those playing sounds. Kind of like a virtual set of ears or a microphone. This is what the Audio Listener does. All 3D sounds are positioned relative to the audio listener. It’s usually a component on the main camera object, but can be placed elsewhere for interesting effects. Just remember that each scene can have 1 and only 1 listener. Not 2, not 0... just 1. So if you ever want to do tricky things with multiple audio listeners you will have to enable and disable them via script.

Other Unity Audio Pro Tips: This part of the Unity manual is a good introduction to some basic Unity terms

When you force a stereo audio clip to mono, it sums the channels. It doesn’t just pick the left or right one. Unity has a set limit of 32 audio sources that can be playing simultaneously. When the game exceeds this number, audio sources with lower priority will still play on in virtual channels. This way, when the sound comes back in it will start at the point in the audio clip it would be at as if it were never muted. If you are using Unity pro, you can use the profiler. It has an audio tab that will show you how close you are to the 32 source limit and will show you how much memory/cpu audio is using.

---------------

Catherine Arthur is graduate of Recording Arts/Music Technology at California State University, Chico and the Sound Designer/Composer for Techtonic Games.

 

 

Related:

The 3 Levels of Game Audio Programming