Search Unity

Looping an animation, should the last frame be duplicated one or two frames later?

Discussion in '2D' started by vhman, Nov 1, 2019.

  1. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    - Looping an animation, should the last frame be duplicated one or two frames later?
    I see a common answer to this question as: "yes, you should duplicate last key frame, but put it at lastFrame-1 position".
    Jaggy looping animation is commonly caused by "zero" duration of last frame, so its virtually skiped.

    I can verify it with test of images of number form 0 to 9, where 9 doesn't show (shown for 1/60 of second).
    And if I put 9 two times, I can vary how long it will shown, but it is impossible to put it precisely on last frame -1 (with default clip animation tool).

    I do not utilize this technics, as I was able to put animation FPS to vary duration of each frame precisely. But FPS field is missing in new version of Unity, so I need to enable debug view every time I do animation clip. Why it was removed?

    I'm asking community to share with "know how" how you handling looping sprite animation?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    Probably not the answer you are looking for but if you have tested this multiple times, just do what looks best for what you are trying to do. Or, if you still arent sure, post a GIF or short video of each of them and post it on here for people to vote on which looks best.
     
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Come on, Community!

    Most tutorial are useless, they don't give any depth. Give me some hint on how you solve this problem.
     
  4. zh4r0naX

    zh4r0naX

    Joined:
    Oct 30, 2016
    Posts:
    71
    Hi vhman,

    if you are talking about the samplerate, you can activate it in new unity there:

    upload_2019-11-6_10-6-18.png

    Then check "show sample rate".

    no need of debug view then.
     
  5. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Thank you @zh4r0naX
    This will solve my issue. I will relay solely on animation FPS.
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    The really top-notch programmers are sick of this forum. It is rare to get a comment or response from them. Unfortunately, I'm not an animation guru, so I cannot do anything more than you here and spitball possibilities. Your best bet here is to start playing with Animation functionality until you find something that works or something that destroys your animation altogether. That's how I handle these things. :p
     
    vhman likes this.
  7. Bunderant

    Bunderant

    Joined:
    May 29, 2013
    Posts:
    21
    Sorry for dragging this thread back from the grave, just ran into some related confusion myself and thought I'd share my solution.

    Looks like there's some custom behavior for the duration of an AnimationClip when it comes a SpriteRenderer's Sprite property, specifically. If the last frame of your animation has a Sprite key, the overall duration will be automatically extended by one sample.

    Here's an animation that only animates a Transform's position. It loops just as you'd expect, returning to zero immediately after the one second mark:
    animate-position.jpg

    The more saturated blue coloring at the top from 0 to 1 sec shows the animation's actual duration. Now look what happens as soon as you add a Sprite property:
    animate-position-and-sprite.jpg

    The duration has now been extended by the sample rate (I have mine set to 10 frames per second). This can be hard to notice and may not matter so much if your sample rate is much higher, like 60+ FPS.

    There are a couple ways to go about adjusting this. One way is to have a dummy property (other than Sprite) in the animation that never changes, and use it to control the duration explicitly, or use an animation event. To avoid keeping that junk around and maybe introducing bugs though, I took this approach: animate-sprite-only.jpg

    In the above image, each Sprite persists for exactly half a second as the animation loops*, without any dummy properties. There is just a duplicate keyframe for the second image, set one sample behind what I want the final duration of my animation to be. It's pretty straightforward to do something like this if you're generating animations programmaticallly, as well, but I'll leave that out for simplicity's sake here unless anyone wants to see it.

    Anyway, the main thing to keep in mind here is that the approach to a clean loop may change depending on what properties you're animating. That said, I'm not sure this weird timing exception exists for anything but Sprites.

    *Note: in this specific case, I'd just set the sample rate to 2 rather than have this keyframe manipulation. This solution really only comes into play when you have variation in the spacing of your keyframes.
     
    rayleigh231, BK-101 and vhman like this.
  8. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi @Bunderant,

    Thank you for reply. I choose to use sample rate for lopping consistency and precise duration
     
    Bunderant likes this.
  9. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
  10. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    I didn't upgrade to 2022, so I cannot confirm or investigate issue.