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

Script-generated animation clip produces strange results...

Discussion in 'Animation' started by Aaron-Meyers, Nov 26, 2019.

  1. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    I've written a script that parses a series of .json files and converts the data into an AnimationClip. When I look at the animation curves in the Animation window, it looks like everything is correct, but when I drive the animation of a Transform with the clip, I'm seeing numbers in the inspector that don't correspond to the animation numbers at all.

    In the screen below, you can see how my scene is configured. The animation clip is on an AnimationTrack on a Timeline. The values for the current frame of the animation do not align at all with the values in the inspector. The transform is at the top of the scene hierarchy so there is no conceivable way that some kind of scaling transformation is effecting the position.

    upload_2019-11-26_11-11-24.png

    Here is the applicable code I use when setting up the animation curves on the AnimationClip before saving it to the asset database:
    Code (CSharp):
    1. clip.SetCurve( "", typeof(Transform), "localPosition.x", posXCurve );
    2. clip.SetCurve( "", typeof(Transform), "localPosition.y", posYCurve );
    3. clip.SetCurve( "", typeof(Transform), "localPosition.z", posZCurve );
    It all seems pretty straightforward to me, but this is the first time I've created AnimationClips in code so maybe I am missing some critical step/setting?

    Can anyone help?
     
  2. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    tried making an animation clip with just a linear progression in a for loop to see if I got the same strange behavior:
    Code (CSharp):
    1. for ( int i=0; i<302; i++ )
    2. {
    3.     var time = (1/30f) * (float)(i);
    4.  
    5.     posXCurve.AddKey( time, i * .1f );
    6.     posYCurve.AddKey( time, i * .1f );
    7.     posZCurve.AddKey( time, i * .1f );
    8. }
    It looked as I expected! Strange!

    After a lot of digging around, I found some settings on the Animation Playable Asset itself that I tried messing with and it turned out that unchecking the "Remove Start Offset" box lined the animation up to the right values.

    I guess it was using the first position in the sequence as the origin offset for the rest of the animation. And since the first frame of my test animation from the for loop was at 0,0,0 it wasn't necessary for that. Hmm... weird. I wonder if there is a way to fix the clip so that this isn't a necessary step in the workflow...