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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question Rotation order issues

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

  1. ThisIsAUser


    Oct 12, 2018
    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);
    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;
    9. curvePosX.AddKey(time, position.x);
    10. curvePosY.AddKey(time, position.y);
    11. curvePosZ.AddKey(time, position.z);
    13. curveRotX.AddKey(time, eulerAngles.x);
    14. curveRotY.AddKey(time, eulerAngles.y);
    15. curveRotZ.AddKey(time, eulerAngles.z);