Search Unity

Animation and FPS, skipping issue

Discussion in 'Animation' started by Khena_B, Nov 16, 2017.

  1. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Hey,

    I have a simple 2D animation, it's just a 3 frame sword slash playing at 45 samples because it needs to be fast, but at lower FPS it's skipping some of the frames and it doesn't look good at all, sometime only one of the frames is visible, how do i prevent that from ever happening, is there a way to slow down the animation to match the FPS so that it never skips a frame?

    Thanks
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Forgive my lack of knowledge on this, what does the samples represent? What happens when you adjust the samples to an even number?
     
  3. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I guess they represent the framerate at which the animations are playing, an even number wouldn't make a difference, there is no issue if the animations are playing at a framerate that is lower or equal to the game's framerate.

    Let's say i have 30 FPS in game and my animation has a sample rate of 30, there will be an animation frame for each game frame, but if the game dips to 15 FPS, the animation will play at the same speed and half of the frames will be skipped. The only solution that i can think of would be to slow the animation down to match the gams's FPS if it is below the animation's sample rate. I tried it and it still skips frames, the character swings his sword and the sword isn't visible etc, it looks a bit ridiculous and i'm not sure what to do.

    I have a sword animation that is playing at 45 FPS and has only 3 frames, a quick slash, but if the game dips below 45 FPS which could happen on lower end systems it would look a bit silly, I observed other 2D games made in Unity and noticed their animations are playing slower when forced at a lower FPS, every single frames are visible, i'm surprised this hasn't been discussed, can't find anything on Google.
     
  4. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    This seems to work, but there is no way to access the current animation's "Samples" in animator so it won't work if the GameObject has multiple animations with different "Samples" values, in my case this was only needed for the sword attack to prevent it from being invisible which looked really bad.

    Samples.png

    Code (CSharp):
    1.     public Animator anim;
    2.     public float animationSamples; //This would be set to 45 for the sword
    3.  
    4.     private float fps;
    5.  
    6.     void Awake ()
    7.     {
    8.         anim.speed = 0;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         fps = 1 / Time.deltaTime;
    14.  
    15.         if (fps < animationSamples)
    16.             anim.speed = fps / animationSamples;
    17.         else
    18.             anim.speed = 1;
    19.     }
     
    Last edited: Nov 16, 2017
  5. abhishekm942

    abhishekm942

    Joined:
    Oct 18, 2019
    Posts:
    10
    As a workaround tht I tried, copying the keyframes of the final frame and pasting them in either side of the final frame helps. When ever the game running at low fps skips the final frame, it means that one of the frames near the final frame is rendered but not the final frame. If we copy and paste the keyframe in the above manner the final frame will apparently be rendered (or any of the adjacent frames where we copied the keyframes).