I use booleans to trigger attack/death animations in Mecanim, setting the “die” boolean to true, and then in the next frame, in the update, setting it back to false.
In most cases, it runs just fine.
However, if the object dies too fast (or something – usually when they’re hit multiple times quickly), the animation never actually starts. The boolean is set to true, then to false, but somehow the animation doesn’t start before it’s set back to false, even though that happens on the next update cycle.
Is there a way to ensure that the animation boolean isn’t set to false until after it’s started playing the proper animation?
Ok, I think I found the solution. So far it’s working 100%. I noticed during more testing that some of the attack animations weren’t playing while the attack code (ray casts etc) was. They seem to all be working now too.
First, I added these variables:
var stateIdDie : int; // The Hash of the State ID for this Animation
var stateIdAttack1 : int; // The Hash of the State ID for this Animation
var stateIdAttack2 : int; // The Hash of the State ID for this Animation
var stateIdAttack3 : int; // The Hash of the State ID for this Animation
var stateIdMagic1 : int; // The Hash of the State ID for this Animation
var stateIdMagic2 : int; // The Hash of the State ID for this Animation
var stateIdMagic3 : int; // The Hash of the State ID for this Animation
private var lastAnimation : String; // To keep it from looping in the Animator
private var lastHash : int; // To keep it from looping in the Animator
It seems that the state – basically which animation is playing – is held in a numerical Hash. Once you know that hash, you can query to see if the hash for the next state is the same as the one you just set. If it is, set the bool to false. This way, if the animation hasn’t started playing yet, the bool will not be set to false.