Audio

Since this is the first post about audio (I think?), I guess I can afford not being creative in naming the post. So, Unity already has audio facilities, which while nice, still leave some bits to be desired, especially if you’re going Wild West without using gameobjects much, like I do.

AudioListener and AudioSource

The basics that I bothered to research, require 2 things: an audio listener and audio sources, both components. Nice and simple. Normally you hook the audio listener to the player/camera gameobject, and audio source components are hooked to the gameobjects in your world. Since I’m not using gameobjects much, I’ve got an AudioListener to the camera gameobject, and a number of AudioSource components in my 2nd gameobject (called Scripts). The audio sources represent audio types really, and are 7:

  • Background music x2 : self-explanatory
  • Ambient sounds primary x2 : biome sounds for overworld, e.g. forest, swamp, open sea
  • Ambient sounds secondary x2: secondary ambience for overworld, e.g. river or shore
  • Positional sounds : misc sounds like doors opening/closing, secrets found, etc

The positional sounds source always uses the .PlayOneShot() function, which as the name suggests, plays immediately a sound, and is capable of mixing sounds, so I can reuse it to play many sounds. Simple!

The others (which are all looping channels) got a bit more complicated. I wanted crossfade, you see, and the solutions I found out there were not up to snuff. Snapshots something something, mixers, more and more components and gameobjects, or coroutines that you fire and forget, but if you want more control, tough luck.

So, I’m using a ping-pong pattern for the channels, so I need 2 of each. Thanks computer graphics for teaching me tricks! When you’re in the forest and you go to the desert, the “active” forest ambience channel starts fading out, and the 2nd buffer become active and fades in the desert audio. That’s it really! The fade in/out happens at the update function. In code, when we move around tiles in the overworld, I just set desired volume level, like “for ping-pong buffer 0 of ambient sounds primary, set volume to 1 and set audio clip to forest-ambience”. then the update function will dutifully interpolate whatever volume we currently have towards the desired volume. That’s it really! I’m quite happy with the result, and especially that it took a few hours overall to set up. I might have used more time searching for biome sounds really.

Here’s a video that demonstrates the above, plus some new biome-specific tilesets. So, you enter a dungeon in the tundra, you get tundra maps, etc. The video also uses some music that I made ages ago, as I’m eager to use this game as an outlet for all my procedural, algorithmic and creative needs.

(Towards the end of the video, I’m trying hard to find the dungeon entry, but it’s a large map so I started teleporting around, gave up and turned the FoV off, bad cheater I know 🙂 )

Leave a Reply

Your email address will not be published. Required fields are marked *

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