- Home /
Objects in the same place with different coordinates
Hi,
Since I am not very good at 2d Drawing, I figured I might try to do a "2D" game using 3D. All action happens on the same z coordinate. So I place my player ship on z =-4.9.I then manually move another object to a position where it collides with the player ship. But when I check its z value, it is not the same as the player ship. How can this be? My guess is that the transform.position property is a relative value of some sort.Or am I just seeing things?
Answer by Rafes · Sep 27, 2011 at 11:10 PM
You are correct in your assumption. This result assumes at least one of your objects has a parent and that the parent is also moved away from the world origin/center.
This goes by different names in different software: Center, origin, pivot... The point from which children and vertices are calculated for an object. An empty game object contains ONLY a transform center, no geometry. This is similar to a locator in Maya or null in Houdini and XSI. When you add geometry, you are basically just adding a bunch of points in this coordinate space with information on how to "connect the dots". This is why geometry moves through 3D space. Because the points are relative to the geometry's transform.
Unity displays this local space in the inspector, which is relative to the parent transform. If one is the child of the scene directly (no GameObject parent) it is in World space. If the other object has a parent, it is relative to that object. If you use the object's transform.position, that is world space, regardless of the parent. transform.localPosition is the same as what you see in the inspector.
This the THE fundamental of 3D work...coordinate spaces....World, local object, even space relative to other objects for calculations. While you rarely need to know the actual math, a basic understanding will allow you to pick the right methods in scripts. Know them, learn them, pretend to love them =)
Answer by Piflik · Sep 27, 2011 at 06:58 PM
The coordinates are in relation to the object's pivot, so if the pivot is far off the center, the coordinates are different, even if the objects sit on top of each other.
How can I control the location of an object's pivot, then?
If the objects are created in Unity, the pivots should be centered by default, with other objects you set the pivot in the program in which you created the object. Sometimes the Pivot is set to (0, 0, 0) on export, so you should always place the model on the origin before exporting.
This is a little misleading as you don't want to change the pivot relative to its geometry, which is the method described in the last comment. You want are dealing in transform positions. You just need to change the position to change the position in world space, otherwise, use transform.localPosition, which is relative to the parent.
[I added an answer with more information.]
You can't modify the pivot, but you can create an empty object and child your model to it. You can then adjust the Position of your model in the Inspector (it's the position relative to the parent object). Once your model is childed to an object, move this object ins$$anonymous$$d of the model.
But I suspect your problem is of another nature: when moving things in the Scene view, the objects are moved parallel to the screen. If the Scene camera isn't looking directly to the Z direction, any movement will affect the Z coordinate as well. To avoid this, click the Z axis at the Scene Gizmo:
This will ensure the Scene camera is looking to the Z direction, so any movement will only affect the other coordinates.