- Home /
How to generate Mecanim Sub-state namehash?
To figure out what animation state an animator is in I can do this:
AnimatorStateInfo currentState= animator.GetCurrentAnimatorStateInfo(0);
if (currentState.nameHash == Animator.StringToHash("Base Layer.state"))
{
// I'm in "state"
}
So if I am in a sub-state I figured this would work:
AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);
if (currentState.nameHash == Animator.StringToHash("Base Layer.parentState.subState"))
{
// I'm in "subState"
}
But it doesn't... What string is used to generate the name has of sub-states?
Answer by bushdiver · Mar 29, 2013 at 12:17 AM
This was a pain.. The sub-state becomes the layer name, I don't know if this is a bug or was intended.
In the example above to get the sub-state.state hashName:
AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);
if (currentState.nameHash == Animator.StringToHash("parentState.subState"))
{
// I'm in "subState"
}
Animator.StringToHash("parentState.subState") rather than Animator.StringToHash("Base Layer.parentState.subState")
What about nested, can you just keep going? "Base.state.substate.subsubstate"
Does not work for me... aaah unity... Neither did the modifications posted after.
Answer by DrEvil · Dec 16, 2012 at 06:07 PM
Base.State.AnimName should work. Looks like you are using "Base Layer.etc rather than just "Base.etc"
Answer by Kwaung · Feb 22, 2013 at 04:20 PM
I use Animator.StringToHash("parentState.subState") seem to work for me. (Yes no "Base Layer".) But I didn't try with state that have more then one layers yet.
Answer by stealann · Dec 22, 2013 at 04:39 PM
@bushdiver Does anyone tested Unity 4.3.1? I am using Unity4.3.1 and seems it doesn't works anymore x_x. I did the test of getting the sub-state of Locomotion in the tutorial project of Mecanim:
Start(){
static int runState = Animator.StringToHash("Base.Locomotion.Runs");
}
FixUpdate(){
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
if (currentBaseState.nameHash == runState){
Debug.Log ("I am Running!");
}
}
I also tried ("parent-State.Sub-state") and it doesn't work either...
Answer by tanoshimi · Dec 22, 2013 at 07:33 PM
@stealann - please don't ask additional questions as answers - start a new thread. The reason your code doesn't work is because Locomotion in the Mecanim tutorial is not a substate - it's a blend tree. Therefore the animator is never in the "runs" state - that's just a motion field that is blended as part of the locomotion state. This code works fine:
runState = anim.StringToHash("Base Layer.Locomotion");
var currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
if (currentBaseState.nameHash == runState){
Debug.Log ("I am Running!");
}
Thanks for answering. I will start a new thread for this. As you said, I misunderstood the name of the animation clips in BlendTrees as Sub-status. What I actually wannna access is the blending situation of the blendtree.
Your answer
Follow this Question
Related Questions
Is it possible to make empty, passing state in mecanim? 3 Answers
Not able to use a Vector as a condition in Mecanim? 2 Answers
How do I make the transition between two states in the "Animator" happen instantly? 3 Answers
How to get fileID information so i can just edit Mecanim Animator through script? 2 Answers
Can't set animator bool to True 1 Answer