Course contentsShow
← Unity Game Development
Lesson 365 of 8285. UI and Audio SystemsFree lesson

Real-time Audio Filtering Based on Game Context

Learners will apply dynamic low-pass and high-pass filters to audio sources to simulate environmental occlusion and acoustic changes based on player positioning.

In this lesson, you will learn how to use dynamic low-pass and high-pass filters to make your game’s audio sound more realistic as players move behind obstacles or transition between different environments.

The Concept of Audio Filtering

In real life, sound changes based on the environment. If you stand behind a thick wall, high-frequency sounds (like a whistle) are absorbed, leaving only a muffled, low-frequency hum. This is an "occlusion" effect. In Unity, we simulate this by manipulating the AudioLowPassFilter or AudioHighPassFilter components attached to an AudioSource.

By adjusting the cutoffFrequency parameter in real-time based on the distance or the presence of geometry between the listener and the sound source, we create a sense of depth and physical space.

Implementing Dynamic Filtering

To achieve this, your script needs to monitor the player's position relative to the sound source. When the player moves behind a wall, you lower the cutoffFrequency of a Low Pass Filter. When they move back into the open, you raise it.

Here is a simplified example of how you might update a filter based on an occlusion state:

public class AudioOcclusion : MonoBehaviour
{
    public AudioLowPassFilter lowPassFilter;
    public float muffledFreq = 500f; // Frequency when behind a wall
    public float clearFreq = 22000f; // Full range

    public void SetOcclusion(bool isOccluded)
    {
        // Smoothly adjust the frequency based on context
        lowPassFilter.cutoffFrequency = isOccluded ? muffledFreq : clearFreq;
    }
}

Contextual Usage

  • Low-Pass Filter: Used to simulate walls, water, or being "underground." It cuts out the high frequencies to simulate sound traveling through dense materials.
  • High-Pass Filter: Useful for simulating thin, airy spaces or effects like telecommunications (radio/phone audio), where the low-end "thump" is removed to isolate clear voice frequencies.

By linking these filters to the geometry detection systems you’ve already built—such as raycasting to see if a wall is in the way—you ensure the audio is never static, but instead breathes along with the player’s journey.

Key Takeaway: Dynamic filtering uses frequency cutoffs to mimic how sound waves interact with physical barriers, turning flat audio into a spatially aware soundscape.