Search Unity

Rotate a parent object so that a child's rotation matches a given rotation value.

Discussion in 'Scripting' started by Autonoma, Jan 7, 2014.

  1. Autonoma

    Autonoma

    Joined:
    Jul 21, 2013
    Posts:
    92
    I have a setup where I have a game object that has an empty game object as a child. The child is at a different world rotation from that parent. What I want to do is rotate the parent so that the child's rotation matches a given rotation value (I get that rotation value from a different empty game object).

    I think this can be done using the Quaternion math, but I have no idea how to do it. Any tips would be appreciated!

    Thanks,
    Zach
     
  2. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    If Q[sub]c[/sub] is the child's world Quaternion, and Q[sub]to[/sub] is the quaternion that you're trying to match the child's orientation to, and Q[sub]p[/sub] is the parent's current world quaternion, then...

    Q'[sub]p[/sub] = Q[sub]p[/sub] x (Q[sub]c[/sub][sup]-1[/sup] x Q[sub]to[/sub])

    Code (csharp):
    1.  
    2. Quaternion Qto; // Rotation to match the child to.
    3. parent.rotation = parent.rotation * (child.Inverse * Qto);
    4.  
     
  3. Autonoma

    Autonoma

    Joined:
    Jul 21, 2013
    Posts:
    92
    Edit: I got it working. I think what I'm seeing is an affect of how I'm calculating the rotation to match.

    Thanks for your help!!
     
    Last edited: Jan 7, 2014
  4. Sandy-Gifford

    Sandy-Gifford

    Joined:
    Dec 18, 2013
    Posts:
    1
    Sorry to bring this thread back from the dead, but is child.Inverse the same as Qaternion.Inverse(child.transform.rotation)?