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

Question Timings slightly off when creating AnimationClip in Code

Discussion in 'Animation' started by Epexe, Sep 5, 2023.

  1. Epexe

    Epexe

    Joined:
    Aug 4, 2023
    Posts:
    2
    Hi,
    When Creating a new AnimationClip from another one by just copying the AnimationCurves over, the timings of the Positions are slightly offset while Rotation and Scale stay the same.
    Here is the Code I use to what should be just copying an AnimationClip (The idea is to eddit the animation in this process in the end):

    Code (CSharp):
    1.     [MenuItem("Assets/ReconstituteAnimation")]
    2.     private static void ReconstituteAnimation()
    3.     {
    4.         AnimationClip clip = (AnimationClip)Selection.activeObject;
    5.         AnimationClip newClip = new AnimationClip();
    6.         foreach (var binding in AnimationUtility.GetCurveBindings(clip))
    7.         {
    8.             var curve = AnimationUtility.GetEditorCurve(clip, binding);
    9.             newClip.SetCurve(binding.path, binding.type, binding.propertyName, curve);
    10.         }
    11.         string clipPath = AssetDatabase.GetAssetPath(clip);
    12.         string newClipPath = clipPath.Substring(0, clipPath.Length - 5) + "Reconstituted.anim";
    13.         AssetDatabase.CreateAsset(newClip, newClipPath);
    14.         AssetDatabase.SaveAssets();
    15.     }
    16.  
    17.     [MenuItem("Assets/ReconstituteAnimation", true)]
    18.     private static bool ReconstituteAnimationValidation()
    19.     {
    20.         return Selection.activeObject is AnimationClip;
    21.     }
    And here are the Original and Resulting Animation viewed in the Animation Window:
    Original:
    Capture1.JPG

    Reconstituted:
    Capture2.JPG

    Anybody got an Idea whats going on here? Can anybody else reproduce this behaviour?
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    105
    From what I can see, the fps of the clips are not the same, so some of the values may fall to different value due to rounding.
     
  3. Epexe

    Epexe

    Joined:
    Aug 4, 2023
    Posts:
    2
    Thank you! Yup, that was exactly it. Just if anyone else ever runs into this:
    Including "newClip.frameRate = clip.frameRate;" fixes this.