This should be a trivial thing to understand, but I’m struggling here.
I’m using Vector3.Distance() and inserting the same two transforms. When using transform.localPosition for both transforms, the distance is 60 (correct with the coordinates displayed in the transform’s inspector window), but the distance is 90 when using transform.position, and does not at all match with the coordinates displayed in the inspector.
This isn’t game-breaking for me, and I can continue to work on my project just fine, but I am completely at a loss when it comes to wrapping my head around why exactly the distances vary between local and global coordinates?
If your transforms have non-1.0 scales, your distances in local space will be more units to reach across the same world-space gap. Child has to take more steps to walk the same road that Mommy does.
Vector32.Distance doesn’t care whether it is fed local or world coords. It is just dong math on the two numbers fed to it. Best to use world positions here.
transform.localPosition is the position relative to the position of the parent transform. This local position is the value shown in the inspector. The distance will be the same in both local and world coordinates only if both transforms share the same immediate parent transform (EDIT: and the accumulated scale of the parents is 1,1,1).
Otherwise, the local positions are completely not related, and the distance may differ from the world position distance even if the values shown in the inspector (local positions) are always the same.
Well, that’s not true, if the parent has a scale other than 1.0, as @halley1 said above. Also the localPosition of a child is not just relative to the position of the parent but actually lives in the parent coordinate space. That includes the position, rotation and scale. So a local position could be vastly different from the worldspace position.
A simple example. The parent is for simplicity located at (0,0,0) and has a uniform scale of (5,5,5). That parent object has two child objects A and B. “A” has the local position (0,0,0) and “B” has the local position (1,0,0). So in local space (in the parent’s coordinate system) the two objects are 1 unit apart. However in worldspace coordinates the positions are A (0,0,0) and B(5,0,0) and therefore the distance is 5 units.
[quote=“Bunny83, post:5, topic: 893233, username:Bunny83”]
Well, that’s not true, if the parent has a scale other than 1.0, as @halley1 said above. Also the localPosition of a child is not just relative to the position of the parent but actually lives in the parent coordinate space. That includes the position, rotation and scale. So a local position could be vastly different from the worldspace position.
A simple example. The parent is for simplicity located at (0,0,0) and has a uniform scale of (5,5,5). That parent object has two child objects A and B. “A” has the local position (0,0,0) and “B” has the local position (1,0,0). So in local space (in the parent’s coordinate system) the two objects are 1 unit apart. However in worldspace coordinates the positions are A (0,0,0) and B(5,0,0) and therefore the distance is 5 units.
[/quote]This corner case does not betray the premise he spoke to. Distance is between two world positions and not the local positions in the parents coordinate system.
Yes, but @Bunny83 's point is correct when applied to OP’s question. Vector3.Distance just does the vector math. So if the parent’s scale is not the identity then the vector difference between local and world coordinates will be different. I’ve edited my post to include the scale condition.
The OP said nothing about scale. That was a red herring brought in by poster #2. Relative to parent also means the scale coordinates. Not to split hairs but if that is the gist of the thread i am gonna go for it.