The Sound of Lasers

After getting the UI looking good and all the Game Mechanics working right, we still haven’t touched on one of the keys in Game Design, Sound.

Today we are going to look at setting up an Audio Manager and getting our Player’s Ship to fire some proper Lasers.

To start off, Let’s set out setting up a Mixer that will allow us to later build a interface for the Player to adjust the volume.

To make an Audio Mixer, open the context menu (right click) in you project View, select Create then Audio Mixer. This will generate a Mixer Controller in your Project and should open the Audio Mixer window (above). As you can see, we have three channels in our Mixer, but by default it only starts with One, the Master Channel. On the left of the window, you can find the Groups area. Here we can add additional Channels and even Child them to each other. We’ve added Music and Sound Effects, if we had dialogue we could include Voice as well. And by making these two children of the Master, we can control their volumes separately while still controlling an over all volume. But now we need something to tie these Mixers to.

In our Hierarchy, Let’s create a new Empty GameObject and call it AudioManager. And we could add multiple AudioSources to it and call it a day. But for neatness, We are going to add and empty Child could Sources and make Children under that for each Audio Channel we wait, in this case Music and SFX.

Each of the Channels will get an AudioSource component. Then we can drag the matching Mixer into the Output field of each Channel.

Now we want to get a script on the Audio Manager so that we can control everything and let other Classes call it to play what is needed. To start we are going to keep it simple and make a Singleton Class for our AudioManager.

Here we have the simple version of our AudioManager. In coming articles, we will expand it out for further functionality. Right now, we just need to get it to tell the right audio source to play the right clips. And since it is a Singleton, we don’t have to worry about the other Classes needing a reference to it or the AudioSources.

We will need references for our AudioSources and a few arrays to hold our AudioClips. We can assign these in the inspector and that will allow the Designers to add more or check the ID for the clip they need to play.

And by adding a couple of Public Methods, PlaySFX() and PlayMusic(), we give the other Classes the ability to request the sound they need played.

If we take a look at the Player’s Normal Shot, We can see it us a Method call of PlaySFX(2).

We use a separate Method instead of just make the call to the AudioManager directly for two primary reasons. Firstly for modularity as it can be used multiple times through out the whole Class. Secondly, making this call has nothing to do with the weapon firing, so we want to pull it out for neatness and debugging later if we need to. For instance, If we have issue with the sound effects later, with it being moved into it’s own Method, we are able to narrow down the problem location. If the explosion sound is happening, but the laser sound aren’t, we can know that either there is a problem with the AudioClips being used or that the call from the Weapon Method is not being made.

Next article, we will look at expanding the AudioManager to allow us to either play a specific clip or have it choose one randomly. And since randomness tends to mean more options, we will expand the AudioClip arrays to allow for some organization and separation of the clip types.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Kesler

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.