If we know that Unity uses the calculation `volume = 1.0 / (1.0 + rolloff * (distance 1.0));` to calculate volume based on distance, is it possible to use this in some way to manipulate the value of a variable based on the current 'perceived' volume? Such as changing the alpha value of a GUITexture's color.
I half-suspect the answer is a flat 'no', but if we know the rolloff value, can't we use this in some way? I guess an easier way is to fake it by checking the player's distance to the audiosource or using triggers, but that would surely mean a lot of tweaking the radius to match up with the sound rolloff. Just curious if there's another way.
Promise this will be my last question of the day!
Thanks.
[EDIT] I've been playing around with the formula to see what I can get from it, but i'll be honest and say i'm not 100% sure what i'm doing here. I couldn't get the right syntax to decalre a variable with this formula on a single line, so i've attacked it in two parts, then combined the result. The value returned is too large to be useful (ie, above 1.0) so I need a way to convert it to a value between 0 and 1 (I believe Mathf.Clamp is what i'm looking for but using `Mathf.Clamp((1.0 / rolloff * dist),0.0,1.0);` doesn't map it between 0.0 and 1.0). As I said, i'm fumbling around in the dark here so excuse my ignorance!
var player : GameObject;
var rolloff : float;
function Start(){
// temporary, i'll assign these from the editor later
audioSource = gameObject.Find("Sphere");
player = gameObject.Find("First Person Controller");
}
function Update(){
//the (distance - 1.0) part of the equation
var dist = Vector3.Distance(audioSource.transform.position, player.transform.position) -1.0;
// the 1.0 + rolloff part of the equation
rolloff = 1.0 + audioSource.audio.rolloffFactor;
// combining the two
currentVol = 1.0 / rolloff * dist;
guiTexture.color.a = currentVol;
print(currentVol);
}