- Home /
Volume and rolloff - can you ignore the Z axis?
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!
a simple solution is just always $$anonymous$$OVE the sound source so that it is in the plane (or whatever) you want !!
So, add a one-line script to your sound source that keeps the Z value where you want. un plaer!
i truly hope it helps !!!
note that in some case depending on your physics, you can even more simply just click the damn "fix Z position" checkbox in the inspector !! then it will move around but keep the Z fixed inplace.
ALso sometimes this can help: leave your original sound thing where it was (this may cause less confusion for some other reason). call that one the "virtual sound thing". then just make a "real sound thing" which indeed simply follows it every fixedupdate but with your fixed Z value.
i like having such "virtual" game objects or "tracker" or "measuring" objects and my scenes are packed with 'em.
Answer by Bunny83 · Jun 11, 2012 at 11:04 AM
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.