So far, everything I have looked at tells me I need to use GetCurrentAnimatorStateInfo(). However, I just get the error "An instance of type ‘UnityEngine.Animator’ is required to access non static member ‘GetCurrentAnimatorStateInfo’. The docs show Animator.GetCurrentAnimatorStateInfo, but all that does is give me the error as stated before(I’m probably using it wrong).
So if you have any information on what I should rework or what I’m doing wrong, let me know. All I’m trying to do is get the name of the clip in the Animator that is playing.
The error means you are using the GetCurrentAnimatorStateInfo with no instance of Animator class. As this is not a static method, you should use it by some Animator object but not the Animator class directlt.
in the Start() of your gameobject that has an Animator component, add this:
Two blend trees in your mechanim; I used one for walking routines, and another for running routines. Three variables are set: speedZ, speedX floats which store the values from the walking and running blend trees. Then isRunning bool which stores the value if my character is running or not. I also have an input left/right shift for running setup as “Run” which correlates with the logic you’ll see below. This is a simple example of using StringToHash.
your int cState variable looks good I would say, you can simply do this:
int cState = Animator.StringToHash("Base Layer.Idle");
if (_animator.GetCurrentAnimatorStateInfo(0).fullPathHash == cState){
//do something
}
(_animator.GetCurrentAnimatorStateInfo(0) gets the information of the animation state currently playing)
But you don’t have an instance of your animator, attach you animator controller to the gameObject that has the script in which you want to access the animator:
As global variable define:
public Animator _animator;
Then in the Start() or Awake() function you actually have to get the instance of the animator, else it remains null and you cannot invoke any function on it: