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

Defining Audio Geometry for Spatial Occlusion

Learn to configure static game objects with Audio Occlusion components to simulate sound muffling when obstacles block the direct path to listeners.

In this lesson, you will learn how to configure static geometry to physically obstruct sound, creating a realistic muffling effect when objects stand between an audio source and the listener.

The Concept of Audio Occlusion

In real life, sound waves don't just travel through walls; they are absorbed, reflected, and muffled. In Unity, Audio Occlusion allows us to simulate this behavior. When an AudioSource is blocked by a physical object, Unity can automatically reduce the volume or modify the frequency to make it sound "behind" an obstacle, such as a concrete wall or a thick door.

To enable this, we don’t rely on the AudioSource alone; we must define the world's geometry so the engine knows what qualifies as an "occluder."

Setting Up Static Occluders

For Unity to calculate occlusion, it needs to know which objects in your scene are solid. We do this by using the Static flags on your GameObjects:

  1. Select the GameObject you want to act as an obstacle (like a wall).
  2. In the Inspector, look at the top-right corner for the Static dropdown.
  3. Ensure the object is marked as Navigation Static or Occluder Static. This tells the engine's internal spatial systems that this object is a physical barrier that sound cannot simply pass through unimpeded.

Configuring the AudioSource

Once your walls are marked as static, you must tell your AudioSource to respect these boundaries. Within your AudioSource component, you will find the Spatial Blend slider set to 3D.

Under the 3D Sound Settings, look for the Occlusion toggle. When enabled, Unity casts a "ray" from the AudioListener to the AudioSource. If that ray hits a collider marked as static, the system triggers the occlusion effect defined in your Audio Mixer.

// Example: Checking for occlusion manually via raycast
bool IsOccluded(Vector3 sourcePos, Vector3 listenerPos) {
    RaycastHit hit;
    // Check if any static geometry blocks the direct line of sight
    if (Physics.Linecast(sourcePos, listenerPos, out hit)) {
        return hit.collider.gameObject.isStatic;
    }
    return false;
}

By combining static geometry flags with the 3D spatial settings, you allow the engine to automatically simulate the muffling of sounds that are hidden behind your environment’s architecture.

Key Takeaway: By marking objects as Static, you provide the necessary metadata for Unity to calculate physical sound obstruction, allowing your audio to feel contextually aware of the surrounding environment.