Search Unity

Runtime linear rotation animation

Discussion in 'Scripting' started by Siccity, May 9, 2020.

  1. Siccity

    Siccity

    Joined:
    Dec 7, 2013
    Posts:
    255
    Hello,

    I am writing a 3d model importer and I've run into an issue importing animations which have their interpolation mode set to linear. For movement I am able to just set tangents and tangent weights to 0, and this will effectively create a linear interpolation for that curve, but for rotation it's different.

    This is the code i'm using for rotation. I am using the same approach to movement which works fine, but for rotation the tangent values seem to be ignored. I have found some solutions online suggesting AnimationUtility, but it's an editor-only class and I need this for runtime.

    Code (CSharp):
    1.  
    2. AnimationCurve rotX = new AnimationCurve();
    3. AnimationCurve rotY = new AnimationCurve();
    4. AnimationCurve rotZ = new AnimationCurve();
    5. AnimationCurve rotW = new AnimationCurve();
    6. for (int i = 0; i < times.Length; i++) {
    7.     rotX.AddKey(new Keyframe(times[i], values[i].x, 0, 0, 0, 0));
    8.     rotY.AddKey(new Keyframe(times[i], values[i].y, 0, 0, 0, 0));
    9.     rotZ.AddKey(new Keyframe(times[i], values[i].z, 0, 0, 0, 0));
    10.     rotW.AddKey(new Keyframe(times[i], values[i].w, 0, 0, 0, 0));
    11. }
    12. result.clip.SetCurve(relativePath, typeof(Transform), "localRotation.x", rotX);
    13. result.clip.SetCurve(relativePath, typeof(Transform), "localRotation.y", rotY);
    14. result.clip.SetCurve(relativePath, typeof(Transform), "localRotation.z", rotZ);
    15. result.clip.SetCurve(relativePath, typeof(Transform), "localRotation.w", rotW);
    16.  

    upload_2020-5-9_10-44-47.png