Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Why switching Animator State is taking 2 frames?

Discussion in 'Animation' started by URocks, Dec 4, 2019.

  1. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    I am creating a coroutine where I am detecting if the animator state is finished. And I Don't understand why I need to wait for 2 frames to correctly detect current AnimatorStateInfo.

    If you want to try it yourself delete both yield return null statements and run Start ScaleUp from context menu, and without them it will fail to detect correct AnimatorStateInfo
    (try to run it at least 2 times in row)

    Code (CSharp):
    1.  
    2.  
    3. public class AnimManager: MonoBehaviour
    4. {
    5.  
    6. public enum AnimTypes
    7.     {
    8.         scaleUp,
    9.         scaleDown,
    10.     }
    11.  
    12. public Animator TestingAnimator;
    13.  
    14.  
    15.     public IEnumerator PlayAnimatorWaitToFinish(Animator myAnimator, AnimTypes myAnimType)
    16.     {
    17.         myAnimator.Play(myAnimType.ToString(), 0, 0);
    18.  
    19. //HERE I NEED TO WAIT 2 FRAMES
    20.         yield return null;
    21.         yield return null;
    22.  
    23.         while (myAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)
    24.         {
    25.             yield return null;
    26.         }
    27.     }
    28.  
    29.     [ContextMenu("Start ScaleUp")]
    30.     public void StartUp()
    31.     {
    32.         StartCoroutine(StartAnimatorRoutine(TestingAnimator, AnimTypes.scaleUp));
    33.     }
    34.  
    35.     public IEnumerator StartAnimatorRoutine(Animator myAnimator, AnimTypes animType)
    36.     {
    37.         yield return PlayAnimatorWaitToFinish(myAnimator, animType);
    38.         Finish(myAnimator);
    39.     }
    40.  
    41.     public void Finish(Animator myAnimator)
    42.     {
    43.         Debug.Log("Finish Animator "  + myAnimator.gameObject.name);
    44.     }
    45. }
    my Animator controller screen capture is in attachement
     

    Attached Files:

    Last edited: Dec 4, 2019
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,533
    That's interesting. I find it really stupid that you have to wait even one frame, but I've never seen it need to wait two.

    Maybe it has something to do with when context menu functions are executed so it might work properly if you run it when a key is pressed during Update.

    You might also be interested in Animancer (link in my signature) which lets you avoid Animator Controllers and just play whatever AnimationClips you want on demand. It doesn't force you to wait a frame to check or modify the current state and it has a few inbuilt ways of waiting for animations to end.
     
  3. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    Thank you @Kybernetik i will check it, but it looks pretty cool. But still i would like to know why waiting 2 frames is needed. I will try to chceck the context menu if it is using one frame. But still it is weird to wait one or two frames for sure