Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Rotation order issues

Discussion in 'Animation' started by ThisIsAUser, Mar 17, 2023.

  1. ThisIsAUser

    ThisIsAUser

    Joined:
    Oct 12, 2018
    Posts:
    16
    Hi there!

    I am currently working on an importer for animations from CAD software that stores positions and rotations (Euler) in world space. But unfortunately that's not enough: The rotations are also arranged differently ZYX instead of Unity's ZXY.

    Now I have a problem regarding the animations: Sometimes single objects "flip" in rotation for one or two frames. This is probably due to my conversion to quaternions, which is why I would like to keep the euler angles. But I have no idea how, since I only get quaternions from the localToWorld matrix which I need to convert from world to local positions (and rotations). Unfortunately, changing the RotationOrder with Transform.rotationOrder is internal and you can only animate local rotations.
    Can someone help me to convert the rotations correctly? Here is my code so far:
    Code (CSharp):
    1. var position = localToWorld.inverse.MultiplyPoint3x4(rawPosition);
    2.  
    3. var rotation = localToWorld.inverse.rotation
    4. * (Quaternion.AngleAxis(rawRotation.z * Mathf.Rad2Deg, Vector3.forward)
    5. * Quaternion.AngleAxis(rawRotation.y * Mathf.Rad2Deg, Vector3.up)
    6. * Quaternion.AngleAxis(rawRotation.x * Mathf.Rad2Deg, Vector3.right);
    7. var eulerAngles = rotation.eulerAngles;
    8.  
    9. curvePosX.AddKey(time, position.x);
    10. curvePosY.AddKey(time, position.y);
    11. curvePosZ.AddKey(time, position.z);
    12.  
    13. curveRotX.AddKey(time, eulerAngles.x);
    14. curveRotY.AddKey(time, eulerAngles.y);
    15. curveRotZ.AddKey(time, eulerAngles.z);
    16.