Sound It Out

This entry is part 13 of 15 in the series Game Development

Previously On IT Ninja . . .

Last time I was busy completely redoing my interactions. I got rid of the idea of a QuestGiver and rolled that into a more robust solution. As I said at the end of that post, it’s time to implement another major system within my game’s framework. And I’ve already started working on it . . . Sound! Up until now my game has been silent, that’s no good. So it’s time to add some noise.

And Now On IT Ninja . . .

Sound Manager

First thing I did was look for a tutorial I had seen before. I found this, although I’m not sure it’s what I had looked at previously. Basically I just copied the code into my game and followed the tiny amount of instructions. Pretty simple, but maybe a little too simple. The tutorial does have a couple of small issues, maybe it’s a typo, or something was changed in one place and not another, but I was able to work through it.

One of the first changes I made to it was add the ability to stop playing sound. It’s important when you’re triggering a sound effect that can last a while. Don’t want that thing playing when it shouldn’t be! Then I modified my Interactable script so I can assign audio clips. Now, when you interact with something it will play a random audio clip. It’s so much fun! I have a computer that makes different error noises when you use it.

Overcoming Limitations

There are definitely a couple things I want to change. The first is the way music works. I need to change that to handle a queue or randomize based on something. Right now we just send it an audio clip to play and it does. I want it to hold onto a list of possible music and shuffle through it. The next issue is crossfading or some kind of transition between background music clips. And lastly, I need to implement some volume controls in my UI.

Slide My Sound

This one should hopefully be easy, I got a nice tutorial to work from. Unfortunately, that tutorial includes some stuff I haven’t messed with, specifically the Audio Mixer. I’ve already got sound playing, seems to work okay, but I want people to be able to adjust the volume easily. Because of that I sought out another tutorial on the Audio Mixer. That helped me a lot in working through it.

I think I have it setup right, just have to test it, then I get to dive into PlayerPrefs so I can save this setting for the player between sessions! Turns out, after testing, I made a small mistake with some variable names, but once that was corrected it all worked beautifully! Each slider controls something different. I have the master volume, which adjusts everything accordingly, then a slider for music and one for effects.

Saving Sounds

Next step is jumping into PlayerPrefs. It really isn’t all that hard, as long as you’re not an idiot like me! I modified my volume slider script to save the value every time it changes. Then I modified my UIManager script to load those values and set the sliders appropriately when it starts. I had a hell of time figuring out why they weren’t saving. Every time I started the game to test they’d all be set to max again. I was googling and googling and googling and just couldn’t find the answer

That is, until I happened to notice some weird behavior in the editor’s log. For just a moment it would display a the max value for the sliders right as I was starting the game. Turns out, I had set the OnChange (which I’ve commonly done) to “Editor and Runtime” instead of the default “Runtime Only” which made it save the max value while the editor was running and therefor set all my volumes to maximum!

Audio Transitions

Just gotta use AudioSource.isPlaying to test. If it’s true, we do the fade out, if it’s false we PlayMusic. Then we modify PlayMusic to do a fade in somehow. . . Yeah, it’s the fade part I gotta figure out. I might have to add the Update method to the SoundManager to check if music is playing and if not, play whatever is next in queue. Man, this is just getting more complicated as I go along.

So I found myself another tutorial about fading audio. The main issue I see is that all the ideas adjust volumes, which I want the player to control, so I don’t want those values to be fucked with. I do think that the first option in the tutorial is what I’m going to use though. It will adjust the volume of the audio source and not the mixer which is what the player controls with the sliders. Of course, to actually make it work the way I want, I’ll have to do some modifications.

No, you know what? I’m doing it again. I don’t need this. Keep things simple! I can always go back and add transitions later but for now the audio system works just fine without it. Yes, there are definitely ways to improve what’s been done here, but no, I must not worry about it now.

Do The Shuffle

This part isn’t so hard although I may complicate it. I just add a new list or array to hold the audio clips and then a function to randomize them and add them to a queue. I need to create a queue, either a list or array. Again, dunno which yet. I need to be able to put something as the first thing in that queue. Also need to be able to take the first thing in the queue, play it, and remove it from the queue.

So, I modified my SoundManager by adding a new list of audio clips. Then I added the Update method and dropped in a conditional statement to play a random item from the playlist if there’s nothing playing currently. Then I went and populated the playlist with some random royalty free songs I had on hand. Well, I thought it would be simple, but for some reason even when there’s music playing, Unity’s AudioSource.IsPlaying returns false.

Sound Solutions

Then it turns out I’m just an idiot and I was invoking the wrong method. I was playing the music as a sound effect accidentally while checking if music source was playing. Duh! Of course, in testing I found another issue I hadn’t thought of which I will have to address. You see, the volume is controlled through the UI, which only exists during gameplay and not in the main menu. So, when you’re on the main menu you get all the sounds at max volume.

I took what I wrote previously for loading the sliders from the PlayerPrefs and dropped that into the Start function of the SoundManager singleton and now it’s working like a charm. Music carries over from scene to scene and randomizes through the playlist I have set. It’s not the most elegant solution but it works. Of course, there’s no volume control on the main menu either . . .

Next Time On IT Ninja . . .

Wow, that was actually a lot easier than I expected. It feels good to have my game make some noise. I can’t wait to keep going on this, but what do I work on next . . . maybe items and inventory? if I do that I’ll have to work on another huge system in my game. I guess I’ll need to do it eventually, so why not now?

Series Navigation<< Interactables and YouItems and Inventory >>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.