Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of current animation state

Tags:

c#

unity3d

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?

like image 400
Owen D. Avatar asked Jan 18 '16 02:01

Owen D.


Video Answer


2 Answers

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;
}
like image 158
Hossein Rashno Avatar answered Oct 01 '22 08:10

Hossein Rashno


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.

like image 26
Digvijaysinh Gohil Avatar answered Oct 01 '22 06:10

Digvijaysinh Gohil