hello!
I am wondering if there is a way to ignore the Z axis when unity is computing audio roll-off?
We are creating a top down zelda type game that has a script to move objects in front of/ behind the player, and this is sometimes causing unwanted rolloff, even when the player appears to be right beside the object.
Muchos Gracias!
No, the roll-off process is completely internal and you can’t influence how it works.
You could compute the distance manually and apply your own roll-off formular to the volume.
edit
Something like that:
//C#
public float maxDistance = 10.0f;
Transform listener;
Transform myTransform;
AudioSource source;
float initialVolume;
void Start()
{
AudioListener tmp = (AudioListener)FindObjectOfType(typeof(AudioListener));
listener = tmp.transform;
source = audio;
myTransform = transform;
initialVolume = source.volume;
}
void Update()
{
Vector3 distVec = myTransform.position - listener.position;
distVec.z = 0; // ignore the z axis
float dist = distVec.magnitude;
// linear
float falloff = Mathf.Clamp01(1.0f - (dist / maxDistance));
source.volume = initialVolume * falloff;
}
Keep in mind that the listener have to exist when the script’s start is executed. If you instantiate your player at a later point in time, you have to trigger this initialization manually.