How it works...

To check the Animator state our character is currently in, we first use the GetCurrentAnimatorStateInfo(int layerIndex) function to get the state the character's Animator is in. We need to call this function on the Animator component; thus, we need to have a variable storing a reference to it (anim in our example). Then we call the IsName(string name) function on the state we got from the GetCurrentAnimatorStateInfo(int layerIndex) function. The IsName(string name) function compares the Animator state name to the name we put as the parameter.

We need to specify the full name of the state. This name needs to be in this format: <Layer Name>.<State Name>. For instance, our SpiderIdle state is in the Base Layer, so we need to ...

Get Unity 5.x Animation Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.