Search Unity

Help Wanted use animator.crossfade play animation , sometime the animation stuck at one frame.

Discussion in 'Animation' started by zhqli, Oct 11, 2021.

  1. zhqli

    zhqli

    Joined:
    Mar 12, 2018
    Posts:
    2
    Help:

    i use animator.crossfade to play animation dynamiclly, but, very occasional, the animation stuck at one frame.

    unless the object reload , or the animation re play ...

    someone has any ideas? help...
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    244
    Hey,

    If you call a transition with crossfade while already in that transition then it will freeze.

    What I do is check that we're not already in, or going to a state before starting the crossfade.

    Example:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CrossFade : MonoBehaviour
    4. {
    5.     Animator anim;
    6.  
    7.     void Awake()
    8.     {
    9.         anim = GetComponent<Animator>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if (!anim.GetNextAnimatorStateInfo(0).IsName("StateName") && !anim.GetCurrentAnimatorStateInfo(0).IsName("StateName"))
    15.         {
    16.             anim.CrossFade("StateName", 0.2f);
    17.  
    18.             // OR:
    19.  
    20.             anim.CrossFadeInFixedTime("StateName", 0.2f);
    21.         }
    22.     }
    23. }
    I don't think you need to check the current state, but I put it there anyway just to be safe. The
    !
    means
    == false
    .

    If you have any questions, let me know.
     
    Last edited: Oct 11, 2021
  3. zhqli

    zhqli

    Joined:
    Mar 12, 2018
    Posts:
    2
    thanks,I'll check my code right away!
     
    Unrighteouss likes this.
unityunity