2
\$\begingroup\$

I want to understand how I can convert world position of an object into local position. Especially how to deal with a rotation of objects.

I'm testing transform.InverseTransform(...) methods. When parent and child has 0 rotation, this methods easily convert world into local position. I don't know how to deal with a situation, when one of the objects has not zero rotation.

In the example below my parent object has (0,0,10) position and (0,0,0) rotation. Child position (0,0,0) and rotation (90,0,0).

Debug.Log (string.Format ("World position: {0}\nLocalPosition: {1}\nInverseTransformPoint: {2}\nInverseTransformVector: {3}\nInverseTransformDirection: {4}\nTransformPoint: {5}\nTransformVector: {6}\nTransformDirection: {7}",
transform.position.ToString (),
transform.localPosition.ToString (),
transform.InverseTransformPoint (transform.position).ToString (),
transform.InverseTransformVector (transform.position).ToString (),
transform.InverseTransformDirection (transform.position).ToString (),
transform.TransformPoint (transform.localPosition).ToString (),
transform.TransformVector (transform.localPosition).ToString (),
transform.TransformDirection (transform.localPosition).ToString ()));

The example debug result:

World position: (-1.0, -0.7, 10.0)
LocalPosition: (-1.0, -0.7, 0.0)
InverseTransformPoint: (0.0, 0.0, 0.0)
InverseTransformVector: (-1.0, 10.0, 0.7)
InverseTransformDirection: (-1.0, 10.0, 0.7)
TransformPoint: (-2.1, -0.7, 9.3)
TransformVector: (-1.0, 0.0, -0.7)
TransformDirection: (-1.0, 0.0, -0.7)

What should I add to transform.InverseTransformPoint (transform.position) to get transform.localPosition?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

What should I add to transform.InverseTransformPoint (transform.position) to get transform.localPosition?

Add parent:

transform.parent.InverseTransformPoint(transform.position) == transform.localPosition

transform.localPosition is the position of this object in its parent's coordinate system.

transform.InverseTransformPoint() transforms a position from world space into this object's coordinate system. So naturally when you take the worldspace position of this object's origin and transform it into its own coordinate system, you get zero - that's what "origin" means.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .