Search Unity

Loop a portion of an animation clip?

Discussion in 'Animation' started by Deeeds, Sep 3, 2018.

  1. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    Is there any way to loop only a portion of an animation clip?

    eg
    The clip is 10 frames long, but I only want to loop the first 5 frames.

    I'm using legacy Animation Component.
     
  2. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    On the source file (e.g. fbx) in the animation tab.
    Check both loop time & loop pose. Set end of clip to 5th frame.
     
  3. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    This is an animation created in Unity.

    And I want to do this in code, the loop to the 5th frame, until a condition changes, then loop through the entire 10 frames.

    Can this be done?
     
  4. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    If you have access to the animator controller, should be possible.
    Part of the code I use to play animation frame by frame in the editor:

    Code (CSharp):
    1. public void SetAnimationFrame(string animationName, int layer, int frame)
    2.         {
    3.             if (animator != null)
    4.             {
    5.                 animator.speed = 0f;
    6.                 animator.Play(animationName, layer, CalculateFrameTime(frame));
    7.                 animator.Update(Time.deltaTime);
    8.             }
    9.         }
    10.  
    11.         public float CalculateFrameTime(int frame)
    12.         {
    13.             float frameTime = 1f / fps;
    14.  
    15.             return frame * frameTime;
    16.         }
    17.  
    If you want to play the animation at normal speed, set animator.speed = 1f;
    Animation fps is set to 24.

    So check what the current frame it is in, then reset back the frame to 0 (or any other point).

    p/s: I'm very drowsy atm, sorry if its not clear.
     
    s138366 likes this.
  5. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    No worries. and THANK YOU!!!

    I think I understand what you're doing, let me see if I've got this right:

    You're manually advancing the frame of the animation and (effectively) pausing on that frame for as long as you want, specified by your 'frame rate'.

    If that's about the sum of it, I think I can do something similar, with Animation Component, as it's happy to be paused, and then told what frame to be upon, at any time, which means I could, maybe, do something similar.

    I'm not a coder, so this will take me a good long while to figure out and work through and around.

    I've gotten rid of all Animator Controllers in my game, as they weren't playing well with pooling and other optimisations I'm doing.
     
    ikazrima likes this.
  6. Siliko

    Siliko

    Joined:
    Sep 7, 2020
    Posts:
    8
    You can open the animation clip on the inspector in debug mode (click on the button on the up right of the inspector) and set start and end of the clip