Player Health Bar Graphic

Create a Low-Health Audio Filter in Unity, Using the Audio Mixer & Transition to Snapshots

In Articles by JohnLeave a Comment

Unity’s Audio Mixer provides all kinds of possibilities for manipulating audio signals.

In this post I’m going to explain how I use Audio Mixer Snapshots and the Transition to Snapshots function to create an audio filtering effect that’s controlled by a linear value, in this case the player’s health.

Here’s the end result:

Why this method?

This technique is most useful when you want to control one or more the volume, effects or parameters of Audio Mixer Groups with one, or more, in-game values.

Audio effects often use values that are not easily compatible with other linear game values, e.g. 22000hz (Lowpass filter). Using Transition to Snapshots however, we’re able to use relative weightings, which are defined in an array of float values, to mix between two or more Audio Mixer Snapshot states using TransitionToSnapshots. It’s easy to connect these float values to linear values in game.

In this example I’m using it to connect a low pass filter (20hz-20,000hz) to the player’s health(0-100).

Pros & cons of using Transition to Snapshots:

  • Control any audio effect or parameter without needing to directly connect it to an in-game value.
  • Control multiple audio parameters at once (via Snapshot states).
  • Use multiple inputs to control multiple Snapshots.
  • Create states that exist between Snapshots (unlike TransitionTo which can only transition to a single Snapshot fully over time).
  • Smoothly transition between states.

When this method isn’t suitable:

Depending on the application, there may be a more suitable method that achieves the same result.

  • For example if you only want to control an Audio Mixer parameter using a UI slider control, then it’s simpler to expose it to scripting, it also doesn’t need to be as smooth.
  • If you want to fade audio volume by 3D position, then the built in 3D sound settings are great for this.
  • If you want to fully switch between two Audio Mixer Snapshots then you can simply use TransitionTo to achieve this.

How to use Transition to Snapshots to create an audio filter in Unity

Setting up the effects & Snapshots

  1. Create an Audio Mixer. and output the audio that you want to process to it.
  2. Create a second Audio Mixer Snapshot so that you have two, name them default and filtered.
  3. Add a Low Pass Filter effect to the Master Group.
  4. Select the default Snapshot and increase the low pass effect Cuttoff freq to maximum.
  5. Select the filtered Snapshot and decrease the low pass effect Cuttoff freq to around 150-300hz.
  6. Hit Play and test the snapshots. Selecting the second snapshot should significantly muffle the audio.
    If it’s not working, check your routing. Is audio passing through the mixer?

Writing the script

  1. Create a new CSharp Script called FilterControl to control the Snapshots.
    Copy the script below (or follow steps 2-10) and attach it to an object in the scene.
  2. Add the using UnityEngine.Audio; namespace.
    This is required for us to access mixer functions.
  3. Create a public float called maxThreshold, initialise it at 50.
  4. Create a public AudioMixer reference called filterMixer.
  5. Create a public AudioMixerSnapshot Array called filterSnapshots.
  6. Create a public float Array called weights.
  7. Create a new public void function called BlendSnapshots(float playerHealth);.
  8. Set the first weights float to the player’s health.
  9. Set the second weights float to match the the filter threshold – the player’s health.
  10. Call TransitionToSnapshots on the filterMixer, pass in the Snapshots array, the weights array and a transition time.
  11. Save the script and return to Unity.
  12. Connect the Mixer and Snapshots in the Inspector.
  13. Set the length of the Weights Array to 2, give the first float a value of 100.

Filter control script

using UnityEngine;
using System.Collections;
using UnityEngine.Audio;

public class FilterContol : MonoBehaviour {

    public float filterThreshold=50;
    public AudioMixer filterMixer;
    public AudioMixerSnapshot[] filterSnapshots;
    public float[] weights;
    
    public void BlendSnapshots (float playerHealth) {
            weights[0] = playerHealth;
            weights[1] = filterThreshold - playerHealth;
            filterMixer.TransitionToSnapshots(filterSnapshots, weights, 0.2f);
    }
}

How to trigger the effect

Call the BlendSnapshots function whenever the value changes. This will also work if called from Update.

Try it yourself

The project example below contains the above script, set up to work with a placeholder health value which you can test with a UI slider.

Share this Post

Leave a Comment