- Home /
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:
Answer by Baste · Mar 16, 2015 at 01:01 PM
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;
}
huh? what is a trigger?
i'm looking at that in the animator now, it just looks like a bool with a different appearance What's the point of that?
okay more importantly, how does this affect my problem at all? The issue is that the animation won't restart when the conditions are right. I don't see how changing those conditions is going to help.
It's really bad interface design that triggers and bools looks the same. Bad Unity, bad!
A trigger is like a single-use bool. You set it by using SetTrigger. Once it has activated, it stays true until it has fired a single transition. Then it resets back to false until you call SetTrigger again.
Check out the docs for an explanation.
Okay thank you, i've implemented this. With this i was able to implement the transition as from anystate. seems to work as desired now
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Animatior Glitch when interrupted 0 Answers
How to interrupt ALL but one animation state? 0 Answers
Cancel current animation automatically and start it again when same animation triggered 2 Answers
Does anyone have an example of an animator with diagnol locomotion? 0 Answers