# Distance between transforms are different in local versus world positions?

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.

1 Like

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.

2 Likes

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.

3 Likes