Interrupt an animation (Animator state)

Is there a way to abort the current animation as if it was over? That is, I do not want to make conditions and transitions in any stages, it will greatly complicate everything. I need the animation to stop at the right moment at a certain event.

I will explain the situation. Humanoid beats his opponent’s foot. At this point, the blow can go through the whole body. I need a leg to stop at the touch. I think this can be done by interrupting the animation. Who knows how to solve this problem?

You can stop the current animation by setting the animation.speed to zero, this stops the entire animator. You’ll want to set the animator speed back to 1 based on the behavior you want for the combat.

Animator _tor;

void Start ()
{
[INDENT]_tor = GetComponent <Animator>();[/INDENT]
}

void DoTheCollisionDetection ( ... some params ... )
{
[INDENT]_tor.speed = 0;[/INDENT]
}

Or, you can transition to another animation e.g. “Kick_Hit” by setting up a transition in the animator and moving to the new state by updating some state value.

public enum AttackStates : int { Attacking, Missing, Hitting }

Animator _tor;

void Start ()
{
[INDENT]_tor = GetComponent <Animator>();[/INDENT]
}

void DoTheCollisionDetection ( ... some params ... )
{
[INDENT]// IF hit logic
_tor.SetInteger ("AttackState", (int) AttackStates.Hitting);

// ELSE
_tor.SetInteger ("AttackState", (int) AttackStates.Missing);[/INDENT]
}

Hope that’s of some help.