1
votes

I'm having a big problem here. I need to know which animator state I'm currently in, in order to make something happen. I've been searching a lot on this matter but because the mecanim animation is so new there isn't much info about it. At least not for unityscript.

Hope someone can help,

Thanks, Róbert Dan

3

3 Answers

0
votes

Unfortunately I don't know JS, but the following C# lines should be easy to be translated. Animator.GetCurrentAnimatorStateInfo returns an AnimatorStateInfo struct that describes the current animator state.

int stateId = Animator.StringToHash("Base Layer.State Name"); 
Animator anim = GetComponent<Animator>();
AnimatorStateInfo currentBaseState = anim.GetCurrentAnimatorStateInfo(0);

if (currentBaseState.nameHash == stateId )
{
  //you are in stateId
}

Like you see in the example above the animator state's name are hashed (for performance reasons) so you have to hash the name of the state using Animator.StringToHash in order to retrieve the integer id associated with that particular state.

0
votes

IT'S DO NOT WORK Try this: print (currentBaseState.nameHash +" "+ stateId);

And play the animation. They are different

0
votes

NOVA, I realize your answer was 2 years ago but it matches if you use the "full path" instead of .nameHash.

obl.GetCurrentAnimatorStateInfo (0).fullPathHash == Animator.StringToHash ("Base Layer.StateName")