Using Unity’s Audio System

Frank Warman
5 min readMay 10, 2021

--

As an Audio Engineer, anything that involves sound intrigues me. So learning Unity’s built-in audio system was a pleasure. I am aware of other Middleware such as FMod or Wwise, but for this small project, Unity’s Built-In system will work just fine!

It’s also kind of funny to write this Medium article with just text and GIFS, because you won’t be able to hear any implementation, but trust me, the sounds are being played!

Like this background music, for example, haha.

Creating and Audio Manager

Most games will have an Audio Manager to… mange… the audio. That is, it will handle background music or ambience that fills the whole game. Other game objects can be responsible for their own Audio as you’ll see soon.

But, let’s create an Audio Manager, and connect our Background Music:

Empty Game Objects are your friend

Within the Background Music game object, we need to add an Audio Source component to be able to play our background music Audio Clip.

Make sure to enable Loop and Play on Awake — this starts the music as soon as the Game Object is enabled (when the game starts). I always tend to turn down the volume at first too so you don’t kill your eardrums if the Audio you imported is too loud.

You can Preview your Audio sounds in your Project Window by pressing the little Play button in the Inspector:

Previewing Sounds

This is especially helpful when importing lots of sounds or choices of background music.

First Method for Audio — Audio Source Component

For certain Game Objects, mainly VFX, or other objects that will always have a specific sound tied to them, you can simply attach an Audio Source Component and attach your Audio Clip. Make sure to turn off Loop , but be sure to have Play On Awake enabled so that when the VFX is instantiated, the Audio is triggered with the object.

Using this method for our Explosion_VFX:

It’s important to note that this works because our Audio Clip of the explosion is shorter in length than our explosion VFX. If our Audio Clip was longer, then we would have to modify our code to Destroy(this.gameObject) after a longer interval to allow the Audio Clip to be played all the way through.

This is why I’m not a HUGE fan of using this method. While it does work, there is a lot of room for error.

Second Method for Audio — Instantiating through code

I like using code to instantiate my Audio because I feel it give me more control. It’s fairly easy to do so as well. Most of the concepts you will be familiar with if you’ve been following my Medium Articles.

We’ll be adding Audio for our Laser first.

First, we’ll go into our Player script, and create a [SerializeField] AudioClip reference to our _laserSFX.

Simple reference in our Global Variables

This allows us to link our chosen sound in the Inspector:

laser_shot Audio Clip linked

Now we can go into our FireLaser() function and add our code:

AudioSource.PlayClipAtPoint is our favourite new function

AudioSource.PlayClipAtPoint() instantiates a new Audio Source in 3D space.

The first parameter is the Audio Clip to be instantiated (_laserSFX). The second is where to instantiate the sound. Since we’re only in 2D we don’t need to worry about depth or spatial awareness, so we can instantiate on the Main Camera — keeping the sound centered and upfront. The last parameter is the volume, which ranges from 0–1. 1 being max.

Now, when our Player presses Space to fire a laser, our Laser SFX is also played!

The same method can be applied to our Enemy for when the are destroyed and play their Explosion Animation:

and our Powerups

Here’s a little trick to applying multiple Audio Clips to Objects with like-scripts. You can select multiple Objects or Prefabs by CTRL Clicking each one, then dragging the Audio Clip you want to apply to all into the appropriate component slot!

More Performant Code

Since my time of recording these GIFs it’s come to my attention that there is a MUCH more performant way to Instantiate your audio clips.

By using AudioSource.PlayOneShot() instead of AudioSource.PlayClipAtPoint(), it is about twice as Performant!

PlayOneShot() plays the Audio Clip but does not create a new Audio Source then destroy it afterwards.

It also plays directly on through your Audio Listener which is by default the Main Camera!

So save yourself some memory and be sure to use AudioSource.PlayOneShot() when necessary!

I’m not above pointing out when I was wrong, or when I learn something new! Even though I’m the ‘Audio Guy’, I’m still learning the beauty of Unity, and I’m glad I can share that experience with you!

Keep on learning whenever you can and never be afraid to admit you’re wrong. You’ve learned something new, so that’s definitely not a bad thing!

Next up will be a super short article on a function that is surprisingly not defaulted into Unity!

--

--

Frank Warman
Frank Warman

Written by Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles