Adam Mclellan Writes: Welcome to the second part in my two part series about audio scripting in Unity. In my first post I introduced Unity terminology and walked through the process of importing and triggering audio clips. Today I'll be covering more advanced topics, including Reverb Zones, advanced triggering scenarios and the additional audio effects available in Unity Pro. If you haven't already, be sure to check out the previous post as I'll be referencing some of the things I covered there as well as expanding upon the example project.
By default when you play an Audio Clip in a scene it's played completely dry--no processing is applied apart from pitch, panning, and volume (and doppler effects for 3D sounds, but this is handled by the engine).
But what if you wanted to simulate the sound of different spaces? A cave? A small room? While you could add reverb to the original audio files this would be impractical--in a world with multiple environments you would need to prepare multiple versions of every audio asset and dynamically trigger them based on the environment.
The solution to this problem is Reverb Zones. Reverb Zones work in conjunction with the Audio Listener: when an Audio Listener is within a Reverb Zone any Audio Sources within audible range are affected based on the Reverb Zone's reverb settings.
You can think of a Reverb Zone as two concentric spheres: the inner sphere applies full reverb while the outer sphere applies partial or "gradient" reverb. The latter is useful for creating transitions between different environments, say, an outdoor space into a cave, or a small room into a larger room.
Important to note is that a Reverb Zone has no awareness of which Audio Sources are contained within, therefore Audio Sources outside of the current Reverb Zone will be affected if they are within audible range of the Audio Listener.
Let's continue the example from last week by adding a Reverb Zone to our "Sphere" GameObject:
Add Component -> Audio -> Audio Reverb Zone
Here you can see the "Full Reverb" (inner) and "gradient" reverb (outer) spheres:
Play the Scene and you'll notice that the "Sphere" GameObject's Audio Source has a generic reverb applied to it. But so does the music. Let's fix that. Bring up the Inspector for the "music" GameObject and check "Bypass Reverb Zone" on the Audio Source:
In most cases you'll want to bypass Reverb Zones for things like music, UI sounds, etc.
So far we've been triggering Audio Clips via Audio Sources attached to GameObjects. While this is fine in many cases, in other cases we may want to trigger an Audio Clip on the fly. This is especially true for short-lived GameObjects such as projectiles.
For example, think of a gun firing a bullet and that bullet ricocheting off of a metal surface. While we'd have a GameObject for both the gun and the bullet and could therefore attach Audio Sources, we wouldn't necessarily have a corresponding GameObject for the ricochet sound. In cases like this we can use scripting to dynamically trigger an Audio Clip.
I guess I've been trying to recreate a Korg Electribe all this time..