- Home /
Vector3.Distance function unclear
when i display it out, i thought starting distanceTravelled should be 0 why is it 15.02341 ???? can any1 help me out thx as starting its not moving, so it should be 0, so when i make it move then it increase distance travelled..
var distanceTravelled :float = 0;
var lastPosition: Vector3;
var cube :Transform;
function Start(){
lastPosition = cube.tranform.position;
}
function Update(){
distanceTravelled += Vector3.Distance(cube.transform.position,lastPosition);
lastPosition = cube.transform.position;
}
change the start function to awake and try? well im not sure jus a suggestion! other than that everything seems perfect.
no use ,nothings happened, start still straight away get a large distance Travelled value. think my x.y.z is large also.. so anyway to solve this.
is the value of distance being changed anywhere else?? ma be tht is the problem
no, i think it should be this link causeing it distanceTravelled += Vector3.Distance(cube.transform.position,lastPosition);
o yea if i put the cube at tansform x-0,y-0,z-0, then the distantravelled = starting=0. but if starting i not put the cube at 0 i will have value, so how to solve it if i want it starting be 0 in the same time the cube is not at transform-0
Answer by Berenger · Feb 06, 2012 at 06:55 AM
Is that cube moving ? You could try to get the distance yourself, [AB] = (B - A).magnitude
no matter its moving or not, once start it will show distance with some value which is not suppose to be. as once start it show slowly from 0 and increase. any1 know how to do that??
Answer by CgShady · Feb 13, 2012 at 09:06 AM
Try replacing
lastPosition = cube.tranform.position;
with
lastPosition = cube.transform.position;
I'm actually surprised it didn't stop with an error.
yea changed,the distanced travelled show =15.+ . but starting i wanted it to be 0,if it move then start ++. starting is show value due to the oject is placing at some coordinates. if its placing at coordinate=0 then start will =0. but i need to places it far away from coordinates 0
somehow i think this calculation is not i wanted ^^as i need as in example: like if car didnt move distance =0 right, if it start moving how do i calculate its total distance travelled and if stop distance stop adding.somethings like this
Your answer
Follow this Question
Related Questions
Maintain height/Altitude/distance from ground? 2 Answers
Animation / Distance 0 Answers
Why this script is not working? 1 Answer
Distance betwen two objects.Error,bug,etc. 2 Answers
How do I set a maximum distance on an activate-able object? 1 Answer