How can I get the name of the current state in a layer on my Animator component? I realize that I can compare the name with GetCurrentAnimatorStateInfo(0).IsName("statename")
but I don't want to have to run that for each state in my layer. Is it possible to simply get the name of the current state?
I don't think this was possible. The only good solution I can think about is to use switch
statement and nameHash
like this:
Animator animator = GetComponent<Animator>();
// Get the id of all state for this object
int runId = Animator.StringToHash("Run");
int jumpId = Animator.StringToHash("Jump");
AnimatorStateInfo animStateInfo = animator.GetCurrentAnimatorStateInfo(0);
switch (animStateInfo.nameHash)
{
case runId:
Debug.Log("Current state is Run");
break;
case jumpId:
Debug.Log("Current state is Jump");
break;
default:
Debug.Log("Current state is not in this list");
break;
}
We can get the current playing clip name in the layer using following code
private Animator animator = GetComponent<Animator>();
private AnimatorClipInfo[] clipInfo;
public string GetCurrentClipName(){
clipInfo = animator.GetCurrentAnimatorClipInfo(0);
return clipInfo[0].clip.name;
}
Note : This code is tested in unity version 2018.3 so I am not sure if this works on previous version or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With