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.
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.
Something like that:
public float maxDistance = 10.0f;
AudioListener tmp = (AudioListener)FindObjectOfType(typeof(AudioListener));
listener = tmp.transform;
source = audio;
myTransform = transform;
initialVolume = source.volume;
Vector3 distVec = myTransform.position - listener.position;
distVec.z = 0; // ignore the z axis
float dist = distVec.magnitude;
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.