Mecanim way to take a hit

Hi

I have a simple setup so far with mecanim with an AI controlled character chasing and shooting at the player. What I’m stuck on is when the AI character gets shot how to do a “hit” animation from anywhere and then transition back to the previous state.

I have something like this at the moment

From any state I have a condition if bool “GotHit” is true.

The problem I’m having is getting out of the hit state and back to the previous state. If I set the GotHit bool to false it kind of works but the animation does not play, SetTrigger does not seem to get set back to false, even though the param is used in the transition. I’ve tried timers, etc to wait a few seconds but it is all seeming clunky.

I’m not sure if this pattern is the way to do it. Am I on the right track or is this just totally wrong.

Cheers
Matt

I should point out in the controller there is a transition from “Hit” to “Idle” this is just a test to see if I can show all of the animation clip in Hit and then transition to a particular state. Even as a test this is not working, either the animation doesn’t play and it goes straight to idle or it stays in Hit.

Well, I’ve worked around the situation in other ways. but it feels like a cludge. It would be great if there were a good set of examples for rigging mecanim for more than just walk run jump.

If you set the end transition to when your variable “GotHit” is false, it will only transition back when the variable changes, so you may need to change the behaviour in scripting. Furthermore, if you left the default “Has Exit Time” on, it will only transition out at the point specified in the transition window. As a side note, when using transitions from Any State, you’ll want to make sure the animation can’t transition to itself in the “settings” option, otherwise “GotHit” will be registered even in your hit animation.

Thanks for the response.

“Has Exit Time” is off and “Can Transition To Self” is also off.

Can you explain a little more what you mean about setting the end transition? I set GotHit which triggers the transition from AnyState to the GotHit state. I dont see a way to change the start or end transition time based on a variable, perhaps I’m missing something here.

Cheers

By End transition I meant the transition from “Hit” to “Idle” as shown in your picture. It might be a good idea to select the GameObject in question and run the game, so that the Animator window can show you which animations are played in realtime. (The blue progress bars will tell you) That way you can tell exactly what is happening and we might be able to help you further!

Also, you need to check what conditions you have for transitioning from “Hit” to “Idle”. You can change the Mecanim params in scripting by using Animator.SetBool(“GotHit”); in the Update() function.

ok, cool. Thanks for the suggestions, I’ve got a bit more of a handle on it.