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.