1
\$\begingroup\$

I have set up a relatively simple example scene with one enemy.

This enemy is controlled with an animation controller. In this animation controller, I have set up a trigger named "Hit".

The enemy plays an idle animation, and when my hero character shoots him, I set the trigger "Hit" via script like this:

_animator.SetTrigger("Hit");

This causes the animation controller to react to it, and it transitions from "Idle Animation" to "Hit Animation":

enter image description here

The hit animation is 3 seconds long. After it has completed, the animation controller will automatically transition back to the "Idle Animation".

I would like make it so that the hero character can shoot the enemy even though he is already playing the hit animation. Let's imagine you use a machine gun to shoot him repeatedly quickly.

However, when I call

_animator.SetTrigger("Hit");

while the hit animation is already being played, nothing happens.

As I currently understand it, this makes perfect sense as the Trigger triggers the transition from animation A to animation B, and if we're already at animation B, then there can be no transition.

The transition will only kick in if the animation controller is currently playing the "Idle Animation" as it can then transition to the "Hit animation"

What could I do to "restart" the hit animation?

Thank you.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Presumably you've tried using transitions from the "Any State" with "Can Transition To Self" enabled? You may want to then treat your various hit variables as separate triggers rather than integer values. \$\endgroup\$
    – DMGregory
    Jun 5 '20 at 13:56
1
\$\begingroup\$

According to DMGregory's suggestion, I have created a transition from "Any State" to "Hit Animation", and now it works. Calling "SetTrigger("Hit")) repeatedly causes a restart of the "Hit Animation".

Also, I have saved a lot of transitions because with "Any State", I can reach all of my "hit" animations (of which I have many, I just removed them for simplicity here) without creating transitions between all animations.

This is what the animation controller now looks like for me:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.