Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Transitioning Animation to a Specific Frame (StateMachineBehaviour)

Discussion in 'Animation' started by lootersmakemesick, Jun 19, 2020.

  1. lootersmakemesick

    lootersmakemesick

    Joined:
    Jun 19, 2020
    Posts:
    3
    Is there any way to set the frame that an animation should start on in a StateMachineBehaviour?

    The closest I have been able to get is this:

    Code (CSharp):
    1. override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2. {
    3.     animator.Play("NAME OF THIS ANIMATION", 0, time);
    4. }
    While this almost works, it isn't a smooth transition as you still see frame 1 of the animation before it gets set to the proper time. It also doesn't seem very elegant, especially for something that seems like a feature many would need (maybe I'm approaching this the wrong way?)

    My reason for wanting this is that I have shooting animations that are many frames long connected to different states of the character (e.g. jumping shoot, idle shoot, falling shoot). So, for example, if the player is on frame 5 of shooting while idle and jumps, I would like to transition to frame 6 of the shooting while jumping animation.

    The only way I can see how to do this currently is moving all of my transition logic to code and not using the animator transitions.
     
    Last edited: Jun 19, 2020
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,576
    anamator.CrossFade is like Play with a smooth transition.
     
  3. lootersmakemesick

    lootersmakemesick

    Joined:
    Jun 19, 2020
    Posts:
    3
    animator.CrossFade has the same issue in this case.

    The problem is that the first frame of the animation gets played before the proper time is set. So, for example, instead of going from ShootingIdle frame 6 -> ShootingJump frame 7 it goes ShootingIdle frame 6 -> ShootingJump frame 1 -> ShootingJump frame 7.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,576
    In that case there might not be anything you can do about it if you keep using Animator Controllers.

    Instead, you could check out Animancer (link in my signature) which lets you directly control the details of animations (including time). The Walk and Run example shows how you can sync the time when swapping between animations.
     
  5. lootersmakemesick

    lootersmakemesick

    Joined:
    Jun 19, 2020
    Posts:
    3
    That's what I was afraid of. I'll take a look at animancer, but it seems weird that the default animator doesn't support that type of transition. I'd imagine most games that have pixel sprites would need to mesh animations like this.
     
    Last edited: Jun 20, 2020