- Home /
Whats the best way to get the Distance between two Vector3?
I know that the best way to get the distance for a Vector3 is using Vector3.distance, etc, etc. But what I'm looking for here, is how to get the Distance between the two "local X axis" for both Vector3 and their rotations. I don't know how to explain this so well, so I made this image:
I want to know the "gap" between these two positions, for exemple. Both are facing the same way, and have the same Y position ( are on the same height ) So they differ on the X and Z axis. I want to get the distance between them using a perpendicular axis. Ahn, I really dont know how to write this down, but I guess you guys understood...
I know how to do this using global values, for exemple, the global X axis or Z, like the first image, but I need them to be rotated, how do I do this?
Answer by Owen-Reynolds · Dec 16, 2013 at 04:50 PM
Not sure what the end result you want is, but: Often converting to local space of one object can help. Standard practice for people who do this sort of math. From memory:
vector3 p2 = thing1.InverseTransformDirection(thing2.position - thing1.position);
Math notes: t1-t1 a the world direction. ITD turns a world into a local direction.
In the second picture, p2 is now something like (12,0,-2). Where 12 is the long "thing1 x" line you drew, and -2 is the distance backwards to the tip. In other words, how to find things2, starting from thing1 and using it's RGB arrows.
Haha, I'm glad you like this answer but the fact that this is what you meant just confuses me even more. Carry on.
Answer by flaviusxvii · Dec 16, 2013 at 03:52 PM
Vector projection is what you are looking for. It'll take the vector between the two positions and then you can constrain that vector to the XZ plane (or whichever plane you like).
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Project.html
Thanks, I'm taking a look at this, it seems complicated, but i'll keep trying. If you could explain me a little bit how to achieve what I want, i would be thankful ^^ If not, its good too :) Thanks anyway! (Sorry for bad english...)
One way to project onto A is: convert to A's local coords, take the local z only, covert back to global. So, if what you want is "kind of like projecting," using local-coords is like doing a slow projection, with more chances to jump in and change things.
Plus projections and basis changes can be difficult to visualize. It seems like people tend to either "get" one or the other.
Your answer
Follow this Question
Related Questions
Find gameobject furthest from player along z axis 0 Answers
Set Distance to object 1 Answer
if close to one of a kind? 2 Answers
Distance Variable Won't Change. What's Wrong With My Script? 1 Answer