Reset animation in animator when execution the same animation in row
Hi, I have a problem with a chained corroutine when I play the same animation in row.
If I queue two different animations it runs fine (Attack1-> Attack2) But if I run the same animation again(Attack1->Attack1) then the visualisation is only played once.
The funny thing is if I add a delay in waitBeforeExecution or waitAfterExecution it works. I would expect that WaitUntil blocks the queue long enough for the animation to finish before the next animation is started.
My best bet would be that the animator don't reset the animation till it is changed to another state.
Is there an easy solution to fix this?
I already checked the StateMachineBehavior but I haven't found a good way to integrate it. Another solution could be to add an AnimationEvent on the last frame of the animation and hold the state of this in an Monobehavior that could be checked but this feels like a dirty solution because the animator should know when he has finished playing the animation.
//Execute Queue
public IEnumerator RunActionBehavior(Queue<IEnumerator> actionQueue)
{
while (true)
{
if (actionQueue.Count > 0)
yield return StartCoroutine(actionQueue.Dequeue());
else
yield break;
}
}
// The Behavior in the Queue
public IEnumerator BehaviorAsIEnumerator()
{
yield return new WaitForSeconds(waitBeforeExecution);
yield return PlayAnimation();
yield return new WaitForSeconds(waitAfterExecution);
}
// Execute the animation and wait until animation is played
private IEnumerator PlayAnimation()
{
Animator animator = subject.GetComponent<Animator>();
animator.Play(GetAnimationName(animationType), -1, 0f);
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1f);
}