0
\$\begingroup\$

I have a GameObject for which I know its world position and rotation.

From another app, I receive a new rotation and position, which should represent the local position and rotation of my GameObject.

How can I compute the position and rotation of the parent object knowing the (world) position, the (world) rotation, the localPosition and the localRotation of its child? How can I find the necessary transform to obtain the second pair of coordinates from the first pair of coordinates?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

If you want a parent world rotation\$P_W\$ that takes the child local rotation \$C_L\$ to the provided child world rotation \$C_W\$, we can express that as an equation:

$$C_W = P_W \cdot C_L$$

Then you can simply solve for the parent rotation algebraically:

$$C_W \cdot C_L^{-1}= P_W \cdot C_L \cdot C_L^{-1} = P_W \cdot I = P_W$$

So, parent.rotation = childTargetWorldRotation * Quaternion.Inverse(child.localRotation);

(Note that the order of the multiplication here is important - putting the inverse on the left side of the multiplication does not produce the same rotation, in general)

Now that we know the rotation, we can work out how that transforms the parent-to-child vector:

Vector3 worldOffset = parent.TransformVector(child.localPosition);

And then reverse that vector to find where to place the parent:

parent.position = childTargetWorldPosition - worldOffset;

\$\endgroup\$
1
  • \$\begingroup\$ It worked. Thank you! \$\endgroup\$
    – NoName123
    Jul 21, 2020 at 8:56

You must log in to answer this question.

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