Dynamic soundtracks are the ones that change according to what is happening to the player in the game, musically reflecting that place or moment of the character's adventure. In this recipe, we will implement a soundtrack that changes twice; the first time when entering a tunnel, and the second time when coming out of its end. To achieve this, we will use the new Snapshot feature of the Audio Mixer.
Snapshots are a way of saving the state of your Audio Mixer, keeping your preferences for volume levels, audio effects, and more. We can access these states through script, creating transitions between mixes, and by bringing up the desired sonic ambience for each moment of the player's journey.
For this recipe, we have prepared a basic game level, contained inside the Unity package named DynamicSoundtrack
, and two soundtrack audio clips in .ogg
format: Theme01_Percussion
and Theme01_Synths
. All these files can be found in the 1362_09_06
folder.
To make a dynamic soundtrack, follow these steps:
DynamicSoundtrack
package and both .ogg
files to your Unity Project.SnapshotTriggerTunnel
. Then, from the Inspector view, access the Box Collider component and check the Is Trigger option, as shown in the following screenshot. Also, uncheck its Mesh Renderer component. Finally, adjust its size and position to the scene tunnel's interior:SnapshotTriggerTunnel
and rename them to SnapshotTriggerStart
and SnapshotTriggerOtherSide
. Then, adjust their size and position, so that they occupy the areas before the tunnel's entrance (where the character is) and after its other end, as shown in the following screenshot:SnapshotTrigger
.using UnityEngine; using UnityEngine.Audio; using System.Collections; public class SnapshotTrigger : MonoBehaviour{ public AudioMixerSnapshot snapshot; public float crossfade; private void OnTriggerEnter(Collider other){ snapshot.TransitionTo (crossfade); } }
SnapshotTriggerTunnel
, SnapshotTriggerStart
, and SnapshotTriggerOtherSide
objects.SnapshotTriggerTunnel
. Then, from the Inspector view, access the Snapshot Trigger component, setting Snapshot as Tunnel, and Crossfade as 2
, as shown in the following screenshot:SnapshotTriggerStart
and SnapshotTriggerOtherSide
by setting their Snapshots to Start and OtherSide respectively.The Snapshot feature allows you to save Audio Mixer states (including all volume levels, every filter setting, and so on) so that you can change those mixing preferences at runtime, making the audio design more suitable for specific locations or gameplay settings. For this recipe, we have created three Snapshots for different moments in the player's journey: before entering the tunnel, inside the tunnel, and outside the tunnel. We have used the Highpass filter to make the initial Snapshot less intense. We have also turned the Synths track volume up to emphasize the open environment outside the tunnel. Hopefully, changes in the audio mix will collaborate with setting the right mood for the game.
To activate our snapshots, we have placed trigger colliders, featuring our Snapshot Trigger component in which we set the desired Snapshot and the time in seconds, that it takes to make the transition (a crossfade) between the previous Snapshot and the next. In fact, the function in our script is really this straightforward—the line of snapshot.TransitionTo (crossfade)
code simply starts a transition lasting crossfade
seconds to the desired Snapshot
.
Here is some information on how to fine-tune and customize this recipe.
You might have noticed how different the Theme01_Percussion
audio clip sounds when the Cutoff frequency of the High-pass filter is set as 10.00 Hz
. The reason for this is that the high-pass filter, as its name suggests, cuts off lower frequencies of the audio signal. In this case, it attenuated the bass drum down to inaudible levels while keeping the shakers audible. The opposite effect can be achieved through the Lowpass filter. A major benefit is the opportunity of virtually having two separate tracks into the same audio clip.
To avoid loss of audio quality, you should import your sound clips using the appropriate file format, depending upon your target platform. If you are not sure which format to use, please check out Unity's documentation on the subject at http://docs.unity3d.com/Manual/AudioFiles.html.
Although we have applied Snapshots to our music soundtrack, background noise can also benefit immensely. If your character travels across places that are significantly different, transitioning from open spaces to indoor environments, you should consider applying snapshots to your environment audio mix. Be careful, however, to create separate Audio Mixers for Music and Environment—unless you don't mind having musical and ambient sound tied to the same Snapshot.
In this recipe, we have mentioned the High-pass and Low-pass filters. However, there are many effects that can make audio clips sound radically different. Experiment! Try applying effects such as Distortion, Flange, and Chorus. In fact, we encourage you to try every effect, playing with their settings. The creative use of these effects can bring out different expressions to a single audio clip.
3.15.237.123