Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Need help getting the time of animation from the Animator

Discussion in 'Animation' started by PaperMouseGames, May 28, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there, I'm working on my first game and I just started finally adding a little more polish to the UI. I'm making UI animations like windows moving and fading, but I've run itno an issue:

    I have a window that plays an animation when it closes, but while it's open, the game is paused, and I want the game to unpause after the animation is done. The issue is I can't seem to get the exact length of the animation clip that plays when the Animator goes into its close state.

    Here is the coroutine that handles this:

    Code (CSharp):
    1. public IEnumerator EndDialogueCoroutine()
    2.     {
    3.         animator.SetBool("IsOpen", false);
    4.        
    5.        
    6.         yield return new WaitForSeconds(closeSpeed);
    7.         uiManager.CloseAndResume();
    8.     }
    animator is the attached Animator component that has the animation states:

    UI animation 1.png

    So right now in the code I am setting my own
    closeSpeed
    to a rough estimate, but what I want is to find out the length of the animation that plays when I change the bool to IsOpen = false.

    Is there any way to do this ?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    If you yield return null after setting the bool, you can use animator.GetNextAnimatorStateInfo(0).length (or GetCurrent... if the transition is instant).

    Or if you would prefer to use a more sensible system you should check out Animancer (link in my signature) which would let you write something along the lines of
    animancer.Play(dialogueBoxOpen).Events.OnEnd = uiManager.CloseAndResume;
    (where
    dialogueBoxOpen
    is an
    AnimationClip
    ). That way you don't need to waste time messing around with Animator Controllers or even bother making a coroutine just to wait for the animation to end.
     
    PaperMouseGames likes this.
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    @Kybernetik

    I had been experimenting with those methods and you're right that they are working! Thanks a lot, I'm new to the animating and I was getting confused by the layerIndex parameters going in those methods.