Unity is an awesome engine for quickly iterating and building game content. The audio features have definitely improved over the years, but it’s still rather limited in many ways. Randomization of pitch, volume, lpf, or even sounds can only be done with a little bit of scripting savvy. Someday, either Unity will fix this or I’ll publish my scripts for these common practices on the asset store . One of the features we added at Free Game Games which I’m most proud of was a psuedo-occlusion scheme utilizing trigger boxes and an enum (and enumerated static variable) to attenuate and apply a low pass filter on certain sounds. This feature was used prominently throughout Free Range Games’ canceled skateboarding game, as well as Freefall Tournament, which is playable on Kongregate.com. It was one of the more advanced features we added to Unity on the audio side. I relied heavily on the scripting wizardry of Jeff Wood, a fantastic designer whom I worked with both at Free Range Games and Shaba Games before that, to handle most of the technical scripting. Our solution was not necessarily the best method to occlude sounds, but it was functional, so I’d like to outline the system so that people can ideally glean some information from it and possibly improve upon it themselves.
The core script components of our system were an AudioOcclusionTrigger and an AudioOcclusionObject. All objects the we wanted to occlude would have an AudioOcclusionObject script attached to it. To trigger the occlusion we created trigger boxes and attached an AudioOcclusionTrigger to that trigger box. Since we may want an object to be occluded in multiple boxes, we created an enum containing a list of occlusion “categories.” This list was rather arbitrary and dependent on the level design. I believe we had things like “Hallway,” “ExtAmbience”, and “IntAmbience” So for example in our skateboarding game, we had a warehouse level in which two cavernous rooms were connected by a small hallway. Each room had an ambient emitter which played a looping ambient drone and occasional one shots and a PA loudspeaker which was pumping out our licensed music soundtrack. The hallway had a trigger box around it with an AudioOcclusionTrigger script labeled as “hallway.” The ambience and music emitters in each large room where tagged with the hallway enum with their respective AudioOcclusionObject scripts, and whenever the player entered that trigger, all the sounds which contained an AudioOcclusionObject script with the category set to “hallway” would attenuate and get filtered over time. And when the player exited that box, the reverse wold happen (the objects volume and lpf would be restored to where they were prior to occlusion).
Here is a short video demonstrating the effect on the music and ambience:
We did a lot of safety checks to make sure the sounds aren’t already occluded before attempting attenuation, creating filters if they don’t exist, etc. but it’s still a far from perfect solution. It really only works well with fairly simple geometry and the more occlusion categories you add, the crazier it can get to keep track of your objects and make sure everything is set up properly.
But there you go. If you’d like to download the scripts and check it out yourself, they’re available here.