Audio Occlusion FMOD w/ Unity not working

Hello everyone!

I have a problem with audio occlusion in Unity and FMOD. It just doesn’t work.
I am inside a hut, with a radio, and I want the radio music to be cut off on the other side of the wall. But you can hear it currently clear as day.

Here is the script I’ve been using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OcclusionScript : MonoBehaviour
{
        private Transform m_MyTrans;
        private FMODUnity.StudioEventEmitter m_Source;

        private float m_MaxDistance;
    private float m_MinDistance;

        public Transform Listener;
        public float OccludedMaxDistance = 0.0f;
    public float OccludedMinDistance = 0.0f;
        public float FadeSpeed = 10.0f;
        public LayerMask Mask;

        void Start()
        {
            m_MyTrans = transform;
            m_Source = gameObject.GetComponent<FMODUnity.StudioEventEmitter>();
            m_MaxDistance = m_Source.OverrideMaxDistance;
        }
        void Update()
        {
            float Maxtarget;
            if (Physics.Linecast(Listener.position, m_MyTrans.position, Mask.value))
        {
            
            Maxtarget = OccludedMaxDistance;
        }
                
            else
                Maxtarget = m_MaxDistance;
            m_Source.OverrideMaxDistance = Mathf.MoveTowards(m_Source.OverrideMaxDistance, Maxtarget, Time.deltaTime * FadeSpeed);



        float Mintarget;
        if (Physics.Linecast(Listener.position, m_MyTrans.position, Mask.value))
        {
            Mintarget = OccludedMinDistance;
        }

        else
            Mintarget = m_MinDistance;
        m_Source.OverrideMinDistance = Mathf.MoveTowards(m_Source.OverrideMinDistance, Mintarget, Time.deltaTime * FadeSpeed);
    }
    void OnDrawGizmosSelected()
    {
        if (Listener != null)
        {
            // Draws a blue line from this transform to the target
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(transform.position, Listener.position);
        }
    }
} 

This script is being used in conjunction with an FMOD Studio Event Emitter, not a Unity Audio Source.
All the walls (like every wall and floor and roof and everything) have the correct layer mask.

I have debugged and found a few things:

The numbers on the FMOD Studio Event Emitter override attenuation do change on runtime, so it is working in changing the attenuation.

When clicking on the object that has the Studio Event Emitter on it, visually it does show that the attenuation area has changed.

The line gizmo in the script is working, and it does pass through the objects with the mask layer, but the audio still plays.

I am not sure where to go from here. If you need any more information please let me know, I could really use some help.

Hi,

Thank you for your code, it was really helpful.

The problem is that Emitter.OverrideMaxDitance is only used when the Event is played, it isn’t used to update the attenuation distances every update. It is very confusing and I’ll make a task to make it easier to understand. What you want to use is:

m_Source.EventInstance.setProperty(FMOD.Studio.EVENT_PROPERY.MAXIMUM_DISTANCE, float)

This is the value that will update the size of the attenuation distances.

I have updated your code to use the new property:

Updated Code
public class OcclusionScript : MonoBehaviour
{
	private FMODUnity.StudioEventEmitter m_Source;

	public Transform Listener;
	public float OccludedMaxDistance = 0.0f;
	public float OccludedMinDistance = 0.0f;
	public float FadeSpeed = 10.0f;
	private float MaxTarget, MinTarget;
	private float CurrentMaxDist, CurrentMinDist; 

	void Start()
	{
		m_Source = gameObject.GetComponent<FMODUnity.StudioEventEmitter>();
	}
	void Update()
	{
		//Retrieving the property values to use in the Mathf function 
		ERRCHECK(m_Source.EventInstance.getProperty(FMOD.Studio.EVENT_PROPERTY.MINIMUM_DISTANCE, out CurrentMinDist), "Failed to retrieve min distance with result:");
		ERRCHECK(m_Source.EventInstance.getProperty(FMOD.Studio.EVENT_PROPERTY.MAXIMUM_DISTANCE, out CurrentMaxDist), "Failed to retrieve max distance with result:");

		// Using hitinfo insead of a layer mask. 
		if (Physics.Linecast(transform.position, Listener.position, out RaycastHit hitInfo))
		{
			if (hitInfo.collider.tag == "Listener")
            {
				// We can still use the these varaiables to set our max distances
				MaxTarget = m_Source.OverrideMaxDistance;
				MinTarget = m_Source.OverrideMinDistance; 
            }
			else
            {
				MaxTarget = OccludedMaxDistance;
				MinTarget = OccludedMinDistance; 
            }
		}
		else
		{
			MaxTarget = OccludedMaxDistance;
			MinTarget = OccludedMinDistance;
		}
		
		ERRCHECK(m_Source.EventInstance.setProperty(FMOD.Studio.EVENT_PROPERTY.MAXIMUM_DISTANCE, Mathf.MoveTowards(CurrentMaxDist, MaxTarget, Time.deltaTime * FadeSpeed)), "Failed to set max distance with result:");
		ERRCHECK(m_Source.EventInstance.setProperty(FMOD.Studio.EVENT_PROPERTY.MINIMUM_DISTANCE, Mathf.MoveTowards(CurrentMinDist, MinTarget, Time.deltaTime * FadeSpeed)), "Failed to set min distance with result:");
		
    }


	/// <summary>
	/// Error checking FMOD function calls and printing their results in Editor only 
	/// </summary>
	private void ERRCHECK(FMOD.RESULT result, string failMsg)
    {
#if UNITY_EDITOR
		if (result != FMOD.RESULT.OK)
			Debug.Log(failMsg + " " + result);
#endif
	}
}

Thank you for bringing this to our attention, hope this helps!