# how to get distance

hi how do i get the distance from one object to another, so far i get a total distance (float). but what i need is all 3 distance seperated in (x.y.z).

so x is 10 y is 1 and z is 0 instead of combined, if you imagine im makeing a tool to calculate the distance of the inside of a house to then make the outside the same size in a different scene and if i use the distance method i’ll run into problems.

Assuming you have two points … P1 = X1, Y1, Z1 and P2 = X2, Y2, Z2 then the vector subtraction of P1 - P2 is X1 - X2, Y1 - Y2 and Z1 - Z2

Applying the Mathf.Abs() function to each float will return its positive absolute value. This is I believe what you are asking for.

See:

http://unity3d.com/support/documentation/ScriptReference/Mathf.Abs.html

and

http://unity3d.com/support/documentation/ScriptReference/Vector3-operator_subtract.html

I know this has been answered already, but there is a better way…

Instead of going through all the rigamarole of doing all that math, just do this:

``````var StartPoint : Vector3;
var EndPoint : Vector3;
var Difference : Vector3;
Difference = EndPoint - StartPoint; // Yes, you subtract the START from the END
var Distance : float = Vector3.magnitude; // done
``````

Alternatively, you could just use:

``````Vector3.Distance(StartPoint, EndPoint);
``````