- Home /
How to get current state on Mecanim?
I am using this tutorial as guide http://youtu.be/4L8RWPZV-v8 but it calls AnimatorStateInfo.name which seems to have been removed from the class.
I want to call my "Atak2" if Atak_Key is pressed during "Atak1":
PseudoCode:
if(currentInfo.name == "Atak1" && Input.GetKey("Atak_Key"))
{
//Set bool on mecanim to call animation Atak2
}
How to do it now?
I'm sure there has to be a reason to store animations states and clips in hash tables, but I don't understand what's the problem having a method like GetCurrentAnimatorStateInfo(0).name. Why I have to get first the hash of the name? This push us to realize before the name of the state. Things like this makes everything complicated for an easy and fast access to the animator behavior.
Answer by jwinn · Nov 25, 2012 at 05:38 AM
Check out the latest Mecanim tutorial download, which has changed code since the beta. I was just dealing with integrating all the mecanim animator changes today.
Here's an example of how they set it up. This is assuming your base layer is named "Base":
static int atakState = Animator.StringToHash("Base.Atak1");
void Start (){
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
if (currentBaseState.nameHash == atakState){
Debug.Log("Do Stuff Here");
}
}
I ended doing something like that, but my attack combo logic never got perfect. I''ll need quite some time to adapt to $$anonymous$$ecanim. I'm having serious issues on checking the time of the animation to set if the player can call the second combo. Anyway, thanks!
I think you are not supposed to use ti$$anonymous$$gs anymore, but define events inside the animation system. (but I only read this somewhere as I am too just starting to use mecanim)
Answer by RChrispy · Jan 08, 2014 at 02:09 PM
How About using something like this:
if( anim_Animator.GetCurrentAnimatorStateInfo(0).IsName("MyAnimationName"))
{
//Do something if this particular state is palying
}
This Function returns true when the string is the same as teh StateName you play currently. Else false.
Is it possible to simply get the name of the current state as a string so I can print it out? I'd rather not have to check against each state name in the controller to find the right one.
I was thinking the same thing. I want to know if the animation playing contains "attack", because I have a lot of attack animations. Now I have a condition inside the if for every attack ¬¬ .
Edit: I just realized it can be done with tags. So I would just tag every attack animation with "Attack", so if it matches that tag, that's it. Don't know if that solves your problem. Thinking out loud just in case. gl.
Oh my god thank you brother, i have 2 days searching info about mecanim, I was using $$anonymous$$achineStates its very messy the thing, actually this thing its usefull to logic things, i will tag my anims :D
This just saved me a huge headache! Thank you for the post.
This is such a great example of a question that often leads to other developers scolding beginners like me before finally reverting with paragraphs of code. Thank you so much for this easy to understand answer, it works perfectly!
@Dev6_RC Can you please fix my code? This is the link where I attach my problem http://gamedev.stackexchange.com/questions/133689/getcurrentanimatorstateinfo0 I'm getting red line at GetCurrentAnimatorStateInfo and I don't know how to solve it.