Animators, Animation Events, and Interrupting/Restarting

okay, here’s the situation.
My character can attack, by swinging his weapon. And once he starts an attack, i set the animator bool isAttacking to true. Thusly preventing the attack from being interrupted

Near the end of the attack animation, is an animation event. It calls a function which sets isAttacking to false, allowing farthere attacks to be done. I deliberately have this NEAR the end, not AT the end. so that there’s a short period of relaxing back to normal stance where the animation is technically still playing, but as far as i’m concerned the attacking part of it is “done” and i want to allow more attacks to happen.

Now the problem is, if i spam click the attack button, it breaks. If i attack again during that winding-down period, isAttacking is set to true, but the attack animation is already playing, already past the ending point, and it does not restart. This causes me to get locked in a situation where i’m no longer attacking, but isAttacking is still set true and i can’t start a new swing.

What i would like to happen is, if isAttacking is set to false, the attack animation starts anew, whether or not it’s currently finished. How might i accomplish that?
picture:

alt text

This is generally why you use triggers to fire off events in the animator instead of bools - triggers can be set before there’s a state that’s going to consume them that’s active.

The setup with triggers is something like:

  • the animator goes from Default to Swing on the “Attack” trigger.
  • the animator goes from Swing to Default on exit time
  • the Swing animation has the animation event “AttackOver” close to the end.

Then your script could look like this:

bool canAttack = true;

void Update() {
    if(canAttack && Input.GetButtonDown("Attack")) {
        canAttack = false;
        animator.SetTrigger("Attack");
    }
}

void AttackOver() {
    canAttack = true;
}