- Home /
Distance between 2 objects without their Z axis.
I need to find the distance of two objects but ignoring the Z axis in the calculation. Normaly I would just do:
Vector3 aV = a.transform.position;
Vector3 bV = b.transform.position;
aV.z = 0f;
bV.z = 0f;
float dist = Vector3.Distance(aV,bV);
But that only works when I want Z to be the WorldSpace Z. My Z should be the local Z of the 2 objects (I make sure in advance that they face the same direction).
Is there maybe already something in Unity that I can use to easily figure that distance out?
Imagine 2 gears on parallel axises. To check whether they are close enough to interact but not too close that they would intersect (beyond their teeth) I need the distance between them but only on a plane (thir common Z axis). (that they touch in Z direction I make sure via collision (in case you wonder)).
In my example above I just killed the Z value from their transforms position vectors and then measured the distance. But this works only when I asume that they are always aligned to the worldspace grid. Once I turn them this will get wrong results.
I came up with the following so far, but that might not be the perfect way to do it (especially if there maybe is a special Unity way to do this):
Vector3 dist3 = otherCollider.transform.position - transform.position;
float rDot = Vector3.Dot(transform.right, dist3);
float uDot = Vector3.Dot(transform.up, dist3);
float dist = (float) $$anonymous$$ath.Sqrt((uDot * uDot) + (rDot * rDot));
Debug.Log ("Distance: " + dist);
Answer by Loius · Feb 15, 2013 at 05:29 PM
Move b into a's local space:
Vector3 bInASpace = a.transform.InverseTransformPoint(b.transform.position);
bInASpace.z = 0
Now bInASpace should be your flattened-to-A's-local-XY-plane direction + distance.
Okay, this looks interesting. But I don´t know how I then get the distance out of that. I tried this:
Vector3 bInASpace = otherCollider.transform.InverseTransformPoint(transform.position);
bInASpace.z = 0;
float dist2 = Vector3.Distance(bInASpace, transform.position);
Debug.Log ("Distance B: " + dist2);
I set my scene up that the objects are at 0,0.5,0 and 0.5,0,0. So they are
squareroot(( 0.5^2 ) + ( 0.5^2 )) =
squareroot( 0.25 + 0.25 ) =
squareroot( 0.5 ) =
0,70710678 apart.
But with the code above I get 0.5 as result. I guess Vector3.Distance(bInASpace, transform.position) is not right here. I think it is still using world coordinates?
The vector itself is the distance - that is, dist2 = bInASpace.magnitude
Your answer
![](https://koobas.hobune.stream/wayback/20220613092744im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting the distance in a known direction 2 Answers
Distance shader returns strage distance when not at 0,0,0 2 Answers
Rotate object so a specific axis faces another object, while also relative to the parent direction 1 Answer
Shader distance between WorldSpaceCameraPos and Vertex 1 Answer
Getting a position behind a target object (targeted attack follow through) 2 Answers