Search Unity

Question Help with quaternions: pose Parent->Child hierarchy to a 'transform pose' based on child offset

Discussion in 'Physics' started by Gullie667, Jun 8, 2022.

  1. Gullie667

    Gullie667

    Joined:
    Apr 21, 2021
    Posts:
    17
    Setup:
    Red Sphere is a child of Cyan Box

    Goal:
    Move / Rotate Cyan box such that Red sphere matches position / rotation of yellow sphere.

    Here is what I have so far. The Update function works since the rotation is calculated before the position is set.
    However, the fixed update function is not working. I believe this is due to the transforms being updated only after both the position and rotation is set.

    How can I fix this? Any help is appreciated!

    Thanks!

    Code (CSharp):
    1. private void FixedUpdate() {
    2.     Quaternion localRotation = Quaternion.Inverse(red.rotation) * cyan.rotation;
    3.     cyan.Rigidbody.rotation = (yellow.rotation * localRotation);
    4.     cyan.Rigidbody.position = (cyan.position + (yellow.position - red.position));
    5. }
    6. private void Update() {
    7.     Quaternion localRotation = Quaternion.Inverse(red.rotation) * cyan.rotation;
    8.     cyan.transform.rotation = (yellow.rotation * localRotation);
    9.     cyan.transform.position = (cyan.position + (yellow.position - red.position));
    10. }
    Quaternion.jpg