- Home /
Question by
serialdrifter · Jan 30, 2012 at 01:50 AM ·
distancevalues
Distance measured showing negative values.
Hello,
I have a little problem, i just set up my 2d racing game so it calculates the distances between the ai players and the player. It works perfectly, and if i'm driving behind them, it shows their distance in meters.
Once i overtake them though, they will still show the distance (i guess that's normal), but i really want to show it with a minus sign in front of it, since it's negative distance now..
This is my code, is there some kinda way to make it show negative values?
function update (){
var ai : Vector3 = ai.transform.position;
var player : Vector3 = player.transform.position;
if (ai){
var dist1 = (ai-player).magnitude;
distance.text = "Distance to Green: " + Mathf.Round(dist1 / 3);
distance.material.color = Color.green;
}
}
Comment
Best Answer
Answer by Lo0NuhtiK · Jan 30, 2012 at 02:11 AM
e.g. : On the player->
var ai : Transform ;
function Update(){
var relativePos = transform.InverseTransformPoint(ai.position) ;
if(relativePos.z > 0)
print("ai in front with positive distance stuff") ;
else
print("ai in back with negative distance stuff") ;
}
Thank you very much, it worked perfectly, had to adjust it a little though, but i got the point :)
Please, can u help me with the little adjust, im in needed, please