# Audio Occlusion FMOD w/ Unity not working

**URL:** <https://qa.fmod.com/t/audio-occlusion-fmod-w-unity-not-working/19313>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [September 24, 2022, 5:45pm UTC](https://qa.fmod.com/t/audio-occlusion-fmod-w-unity-not-working/19313 "2022-09-24T17:45:28Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [September 28, 2022, 7:23am UTC](https://qa.fmod.com/t/audio-occlusion-fmod-w-unity-not-working/19313/2 "2022-09-28T07:23:25Z")

</div>

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:

```auto
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**
>
> ```auto
> 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!

---

_[View the full topic](https://qa.fmod.com/t/audio-occlusion-fmod-w-unity-not-working/19313)._
