Animator m_anim = ...;
m_anim.Play("GenericSpinReveal");
m_anim.Update(Time.smoothDeltaTime);
m_anim.Update(Time.smoothDeltaTime);
// Assert test passes only if I have the two updates above.
DebugUtils.Assert(m_anim.GetCurrentAnimatorStateInfo(0).IsName("Base.GenericSpinReveal"));
Any way around this? Needless to say this breaks a lot of basic logic you might want to do. :shock:
The issue seemed to be that isName() would still match against its starting state for a few frames(?) after the animation was supposed to be triggered.
and it’s working just fine. If you need to match IsName instead of being able to check an exit condition, I would have a second while loop yielding until IsName returns true once, then another yielding until IsName no longer passes.
It’s always funny seeing the stupid hacks people have to come up with to accomplish even the simplest of tasks with Mecanim. You tell it to play an animation and you just have to hope that it will do what you want at some point in the future.
So I’m in Unity 2020.3.31f1 and I have a similar (but inverse of this) problem; the Animator window shows I’m in a completely different animation state than what I’m testing IsName against in code, but I’m getting a false positive, resulting me being stuck forever in a while loop
//wait for the animation to start
while (!animator.GetCurrentAnimatorStateInfo(0).IsName(animName))
yield return null;
//then wait for the animation to finish
while (
animator.GetCurrentAnimatorStateInfo(0).IsName(animName)
{
//stuck here forever
yield return null;
}```
Has this bug existed for 7+ years and it's just never been fixed?
Indeed. My workaround/hack is to reference the script attached to the Animator in the State’s Behaviour. The script object (AnimatorStateController) cannot be set in the Inspector, so must be referenced in code:
public class WalkBehaviour : StateMachineBehaviour
{
AnimatorStateController aniController;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (aniController == null)
aniController = animator.gameObject.GetComponentInChildren<AnimatorStateController>();
aniController.UpdateWalkAnimationState(true);
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
aniController.UpdateWalkAnimationState(false);
}
Then in the AnimatorStateController just need the public function UpdateWalkAnimationState() and a private backing bool:
public class AnimatorStateController : MonoBehaviour
{
[HideInInspector] bool walkAnimationStateOn = false;
public void UpdateWalkAnimationState(bool running)
{
walkAnimationStateOn = running;
}
}