# Why is transform.position not equal to the world position in my object?

This is driving me insane - gameObject.transform.position gives the world relative position of the object, right?

I’m trying to get the distance between two transform.position, and the distance is zero when they are not even overlapping.

In the below image, the two objects I’m comparing distance between their transform.position are “Player_Chef” and “CashStack test”. I’ve selected the Player_Chef to show you the origin point, and the component “Cash Transfer” attached is the one printing to the Console the line
`distance: 0.6426157, cashStack: (13.10, 0.00, 52.80), cashTransfer: (13.50, -0.08, 52.30)`
and the code in this script to get the distance is
`float distance = Vector3.Distance(cashStack.transform.position, gameObject.transform.position);`

Note the distance reported is just about zero, the positions are similar, and yet visually, they are nowhere near each other.

Here’s another screenshot with the “CashStack test” object selected, to prove the origin is in the center, you can see the move gizmo is centered on the green stack of cash;

And here’s a 3rd image to show what happens to the distance when I actually move the player on top of the object - the distance grows to “9”, and the positions become more different:

Now, it’s true the Player prefab is set up weird and not ideal (I’m a contractor newly placed on the team so I can’t do much about this), the player object is nested in another prefab and moves around internally relative to that prefab.

But regardless of what wacky nested prefab they have set up, the position given for transform.position for either object should be giving me the correct world position to get a distance between, right?? So what’s going on here??

It gives you the world position yes only relative to the world-origin. The value you see in the inspector is the local position relative to its parent which is Transform.localPosition.

Do the math yourself, the calculation is correct. You’re also only showing this in 2D, they are positioned apart in all three axii.

Here’s the source for the Vector3.Distance call: https://github.com/Unity-Technologies/UnityCsReference/blob/4e215c07ca8e9a32a589043202fd919bdfc0a26d/Runtime/Export/Math/Vector3.cs#L352

The distance you’re getting is 0.6 Unity units. Imagine it to be 0.6 meters. That does actually match what I’m seeing in your screenshots.

But then I move on top of it and it becomes 9.0?

No they are on the same horizontal plane (Y axis) well very close anyway. You can see that doesn’t change between each screenshot from the debug output, and yet the distance grows when I approach the object.

Look at the positions of that screenshot. You will find that the positions do equate to 9 meters of distance.

As above, do the math yourself as per the method or check that you’re referring to the correct Transforms here.

Yep the math is correct, that’s not the part I have a problem with - the problem is the position given from the transforms in the first place, why is it not centered on the object like Unity clearly shows with the Gizmo?

In the first screenshot, you can see I have the “player_Chef” selected, and the “Cash Transfer” script attached to that very same object, is the one asking for “gameObject.transform.position”, which, going by the output coordinates and the distance, appears to be a position somewhere above and to the right of the “CashStack test” object in the middle of the screen. Or lower to the left, I may have that the wrong way around, but either way…

How is that not the right transform to use? If the two objects are near each other, and their transforms are in the center of the meshes, why are the transform.positions different? Why does the player have to be above and to the right for the positions to grow close?

I mean the inspector gives you the local position. `transform.position` gives you the world position. If the positions aren’t as expected, there’s something else going on here in your setup. It’s very unlikely one of the most fundamental parts of Unity is at fault here.

Also FYI, your transform tool handle is in ‘Center’ not ‘Pivot’ mode. This may be misleading you as to the actual position of these game objects, as it will center the gizmo on the bounding box center, not it’s actual 3d origin point.

3 Likes

I’m pretty sure it’s the

3 Likes

That was it. It wasn’t even the wack-ass super nested player prefab that I suspected, it was the “Cash Stack test” thing which is 100% my own creation - my math was off for the procedurally generated cash and when I changed the gizmo mode to “pivot” the gizmo shows up in the exact position the distance reports “zero”, above and to the right of the cash stack.

Thank you every one for helping me out!

1 Like

Great you solved it.

Note that as programmers an important attitude is: The computer / compiler is always right. If it’s not right, it’s broken. While it’s possible that hardware and software can have bugs or be broken, it’s not something you should prioritize. If something doesn’t work, the first thought should be : Where have I messed up?. If you checked all your code against your understanding of the situation and you can’t find anything wrong with it, the second question should be: What have I misunderstood? We usually make many assumptions about how things work. Unity has many quirks and gotchas (the pivot mode is one of them) you may not yet know about. So breaking things down and testing individual parts usually helps to narrow down the potential causes.

1 Like