i created a object and attached this code . it works fine but when i click a point it gives me the distance but its does not update the distance constantly when it reaches near the destination .
can some one help me how to fix this ? please and thank you. :lol:
var followMouse = 1;
// Select GameObjecs in Inspector
var cube2 : GameObject;
var aGui : GameObject;
function Update () {
if(followMouse){
if (!Input.GetMouseButton (0)){return;}
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 200)) {
Debug.DrawLine (ray.origin, hit.point);
transform.position.x = hit.point.x;
transform.position.z = hit.point.z;
}
}
var distance = Vector3.Distance (this.transform.position, cube2.transform.position);
aGui.guiText.text = "Distance to destination " + distance.ToString();
}
If you’re only changing this object’s position when the mouse button is down, and the other object doesn’t move at all, the value of distance will only change while the mouse button is down and the mouse position isn’t equivalent to the object’s position. If you already know that, I think you’ll have to tell us more about what you expect it to do versus what it’s actually doing.
my project [ first project : ]
i have 2 object , 1 target , 1 car .
1.i attach script to target .
2.add some controller to my car , from withing unity3d.
3.when i click screen to target the car to get to a particular place it gives me a distance with in two objects .
4.but , this is fixed , not updating as its moving to the location .
5.for ex:if distance = 200m , it is fixed , even the object reached its destination the distance is 200m.
6.how to update distance constantly ?? i dunno.
sorry for much noobness , my first time with unity .
You’re only moving the object this script is attached to when the mouse button is down. That’s probably why the value of distance only changes when the mouse button is down, if that is indeed the case.