- Home /
How do I retrieve Mecanim State Name?
Hi, what happen is that I have this repetitive code here which should return true whenever the Animation is in a State with the name "Platform" in it. And the thing is, I cannot figure out how to get Animator state name and retrieve it as a string.
Can anyone help me on this?
protected bool CompareBaseState(string stateName)
{
AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo((int)MOTIONLAYER.BASE);
if(currentState.nameHash == Animator.StringToHash(stateName)) { return true; }
return false;
}
//Mecanim States that are putted into this, will not response to move input
protected bool IsInUncontrollableState()
{
if(CompareBaseState("Base.Platform_Cross"))
{
return true;
}
if(CompareBaseState("Base.Platform_StepUp"))
{
return true;
}
if(CompareBaseState("Base.Platform_MoveUp"))
{
return true;
}
if(CompareBaseState("Base.Platform_RunUp"))
{
return true;
}
return false;
}
Answer by infinitypbr · Oct 17, 2013 at 07:59 PM
Maybe I'm reading your question wrong, but this is what I do:
in Start()
stateIdDie = charAnimator.StringToHash("Base Layer.Die"); //repeat for all states
Whenever I start a new animation, like "attack 1" or "magic 1" etc, I set my lastAnimation
variable to the state I've chosen, and then lastHash
to match the hash of that animation
function SetHash()
{
if (lastAnimation == "die")
lastHash = stateIdDie;
}
And in Update()
if(charAnimator.IsInTransition(0) && charAnimator.GetNextAnimatorStateInfo(0).nameHash == lastHash)
charAnimator.SetBool(lastAnimation, false);
Basically this says if the Animator is not in transition (and is therefor, in a single animation), and the next state is the same as the lastHash I've set (as in, when this animation is done, it will then repeat to the same animation -- like looping an attack animation etc -- then set the bool to false, to keep the animator from looping.
Answer by vikitosss · May 18, 2016 at 04:47 AM
var anim = GetComponent(Animator);
// with or without -OnAnimatorMove function -
function OnAnimatorMove(){
if( anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) {
print( "Current state is WALK" );
}
else if( anim.GetCurrentAnimatorStateInfo(0).IsName("JUMP")) {
print( "Current state is JUMP" );
}
else if( anim.GetCurrentAnimatorStateInfo(0).IsName("....")) {
print( "Current state is..." );
}
}
or if you like it : if( !anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) { anim.SetBool("WALK", true); }