I was scared of audio.
I didn’t quite know how to handle the many sounds I would want in HÜVER.
So I started with simple events. I discovered in Unity I could put an AudioSource on an object and script that object to PlayOneShot(sound, volume) and that worked well.
I set up an AudioSource, then made some AudioClip variables, and went searching for sounds. I found a lot of sounds by poking through the directories on my computer. I listened to a lot of standard Windows event sounds, and some bizarrely that showed up in the OpenOffice directory structure.
I used a few of those.
But I also found a site called freesound.org. I signed up and found some collision sounds, and some other interesting sounds to begin with.
I started with the Taxi Landing Pads. I knew I wanted those to hum, so you knew they were active, and with stereo sound, you could determine where they were, at least which direction and how far away, from the sounds you hear in stereo.
So I attached a looping hum to an AudioSource on the light beam itself. That way, when the beam is activated by the game, the sound starts up automatically, and goes away when you land on it, since that removes the beam.
Then I tackled the UI panel. A sound when you begin a shift. Simple enough. Worked fine.
Then I wanted a sound for every selection in the Purchase UI. Every time you pulled the joystick to change shopping options, a sound would fire. That also worked well enough, except the hacky way I did my UI meant it was on an Update() loop and I had to check for it firing once, and then not allow it to fire again until you change options. Again, no big deal.
And a sound of warning if you tried to select an unaffordable upgrade option.
And a sound for a successful purchase.
Then I wanted the fuel gauge to alert you when it was getting low on fuel.
I knew what I really wanted was a warning as you pass from one color threshold to another, with a dire warning once you were in the red, with perhaps a constant warning in that dangerous range.
So I got my daughter to record some sound samples:
“Fuel Yellow”, “Fuel Orange”, “Warning! Fuel Low!” and “Fuel Full”.
I put some reverb on those and put them right in the game, placing firing code whenever the gas gauge ranged within those color changes, and also having to make sure they didn’t fire off multiple times within those slim ranges, since this was also on an Update() loop.
Once you enter into the red, you get a constant pinging of warning, which gets faster and faster the lower your fuel level drops, emphasizing the urgency of the situation.
And suddenly I had a game with sound!
There are a few others, almost all of which are event-fired.
I wanted to have some great impact sounds for when the taxi hit buildings and other things. First, the game really only considers two kinds of collision for the taxi: Airship and Other. If it hits an airship, I want the Tip amount to go down, but I didn’t think it fair to damage the car since you can collide with an airship without being able to see it, if it’s coming from behind, or in an area not in your viewport.
So no damage. But now it makes a very satisfying object-hitting-rubber-balloon sound. And the intensity of the collision directly drives volume, so the harder you hit it, the louder the sound.
But for buildings, I wanted to have graded sounds for the intensity of impact. Volume wasn’t going to be enough.
So I created an array of 10 sounds ranging from a tiny bump. to metal hit, to glass breaking, to a collossal crash. And it fires perfeclty!
Then I added Powerups. Floating things in the city that give you bonus items if you collide with them.
I wanted those to make an arcade-like bling sound when you hit them. That was also very easy.
So what was left?
Man, I was intimidated.
I didn’t know how to drive sound on the taxi, when the engines are controlled by four joystick axes, and a button for Turbo.
I didn’t want a lot of complex code, firing looping sounds at given volumes and pitches (yes, you can alter pitch!). I didn’t want to be checking to see if one sound was playing in order to play another.
But then it hit me.
I can put a unique AudioSource on the taxi for each joystick axis!
So I found a nice jet engine sound and looped it (freesound.org to the rescue) and then I remembered last summer cleaning off our plastic deck Adriondack chairs with a hose on tight beam, and the sound was awesome! I vowed to record it and use it for my engine noises. So I did.
Finding smooth loops was hard, but I managed.
But first, the main power. That’s the UP/DOWN thrust. I wanted a jet engine sound for that so I used the freesound.org jet engine sound I found.
But how to trigger it?
So it turns out the AudioSource can be told to play on start, and to loop. Perfect.
I determined I didn’t have to have any complex code to start and stop the audio whenever I change joystick intensity. Instead, I simply alter the volume and pitch based on the joystick’s position, making sure there was a constant low volume in the cases where I wanted the engines running when there was no joystick input at all (freefall) but when the stick was engaged, it would ramp up the volume and pitch to make it sound like the engines were working harder.
And it worked! Like a charm!
So then it was an easy step to add an AudioSource for FORWARD/BACKWARD sound, and for that one I used one of the loops I made by cleaning my deck chairs. Pitch and volume is controlled by forward/backward joystick amount. I easily adjust the pitch ranges by code values until I got something I liked
Same for TURN. I was able to easily do the same for Turning.
Then I added STRAFE, which uses the same sound as Turn (since they technically use the same engines) but at a different pitch, so you can hear the difference.
TURBO simply multiplies the pitch and volume a bit so it seems more intense when Turbo is engaged. Turbo is boolean, so it’s a simple yes or no to that pitch alteration.
And now, dammit, I have a complete sounding game!
But oh… what about when you crash? Or run out of fuel?
One simple addition to the LoseControl() method, and I called PlayOneShot(losingControl, volume) and it plays a nice sound of a jet engine winding down. At the same time, I reduce the volume of the other four AudioSources to 0.
And it sounds amazing! You run out of fuel and you get the engine winding down to 0 as you fall through the sky.
NOW it’s done.
Sure, I want to tweak the sounds themselves, or even replace some, and adjust the pitches and volumes, but that’s all data.