In this lesson, you will learn how to use exposed Audio Mixer parameters to create a responsive audio environment that shifts in real-time based on gameplay states.
The Power of Parameterized Mixers
While Mixer Snapshots are great for broad changes, Parameterized Mixers offer surgical precision. By exposing a specific value—such as the Volume, Pitch, or a Lowpass Cutoff frequency—you can link game variables directly to audio processing. This turns your soundscape into an instrument that plays along with the player’s actions.
For example, imagine a game where the player’s health drops. Instead of a static "low health" sound effect, you can dynamically lower the Cutoff Frequency of a Lowpass effect on your Master Mixer group, making the game world sound "muffled" or "dizzy" as the player nears defeat.
Exposing Parameters for Scripting
To control an effect via script, you must first "expose" it to the engine:
- Open your Audio Mixer window.
- Select the desired Mixer Group (e.g., "Music").
- In the Inspector, right-click an effect property (like
Cutoffin a Lowpass effect) and select Expose '[Property Name]' to script. - Rename it in the Exposed Parameters section (top right of the Mixer window) to something readable like
"MusicFilter".
Controlling the State via Script
Once exposed, you can use the AudioMixer class to modify this value during gameplay. Here is how you might link a player’s velocity to a sound filter:
using UnityEngine;
using UnityEngine.Audio;
public class AudioStateManager : MonoBehaviour
{
public AudioMixer masterMixer;
public float playerSpeed;
void Update()
{
// Smoothly adjust the cutoff based on movement speed
float filterValue = Mathf.Clamp(playerSpeed * 500f, 200f, 22000f);
masterMixer.SetFloat("MusicFilter", filterValue);
}
}
By passing values from Update() into SetFloat(), you ensure the audio stays perfectly synced with the game state, creating a seamless, high-quality reactive soundscape without needing dozens of redundant audio clips.
Key Takeaway: Exposing parameters in the Audio Mixer allows you to bridge the gap between game logic and audio processing, enabling real-time, data-driven soundscapes.