Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Find local rotation given global rotation

Discussion in 'Scripting' started by GreenStone, May 17, 2012.

Thread Status:
Not open for further replies.
  1. GreenStone

    GreenStone

    Joined:
    Mar 19, 2012
    Posts:
    9
    Hi, guys

    I need some help with rotations and quaternions (which are new to me...well, the quaternions). So I have a rotation given in world coordinates as quaternion, let's call it Qrot_world. And I also have an object, with its world rotation Qobj_world (which, as fas as I understand, is retrieved when transform.rotation is called).
    Now if I assign Qrot_world to transform.rotation of the object I will get the object rotated in a way that I want it to be. However, what I also need is the rotation of the object in LOCAL coordinates (preferably with localEulerAngles) so that after applying this local rotation the object will be in the same orieantation as with Qrot_world.

    I really hope you guys can help me out. It has been a while since the last time I worked with rotations.

    GreenStone
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    assign the world rotation to your transform and read the corresponding local rotation. (you can restore the original rotation after you retrieve the local rotation if you just want to know this local rotation without rotating the transform).
     
  3. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Why would you want to have a local rotation when you have access to the world rotation and just Transform.Rotate according to Space.World?

    In any case, there are some functions that you might like, such as this and this
     
    Holo_Leto likes this.
  4. GreenStone

    GreenStone

    Joined:
    Mar 19, 2012
    Posts:
    9
    I really would like to skip this assignment. Otherwise it will lead to object jumping around and interacting with other game elements.
     
  5. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    Quaternion.Inverse will give you the inverse of your "from" rotation - you just multiply to get some other rotation relative to that.
     
  6. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    it won't jump if you restore it right away
    Code (csharp):
    1.  
    2. originalRotation = transform.rotation;
    3. transform.rotation = newWorldRotation ;
    4. newLocalRotation = transform.localRotation;
    5. transform.rotation = originalRotation;
    6.  
    otherwise you would have to do some quaternion operations.
    I think it would be:
    Code (csharp):
    1.  
    2. newLocalRotation = newWorldRotation * Quaternion.Inverse(transform.parent.rotation);
    3. or
    4. newLocalRotation = Quaternion.Inverse(transform.parent.rotation) * newWorldRotation;
    5.  
    if parent is null, then newLocalRotation = newWorldRotation
     
  7. GreenStone

    GreenStone

    Joined:
    Mar 19, 2012
    Posts:
    9
    Oh, that actually makes sense. I can just restore the position immediately. Somehow it got stuck in my head that the rotation will be updated right away but thats not the case.
    Then the problem is solved. Thanks guys! Time to check other parts of my code for errors.
     
  8. Cliwo

    Cliwo

    Joined:
    Oct 12, 2016
    Posts:
    12
    I was able to get localEuler using
    Code (CSharp):
    1. var localEuler = (Quaternion.Inverse(target.transform.parent.rotation) * Quaternion.Euler(worldEuler))
     
Thread Status:
Not open for further replies.