[SOLVED] Efficient Way to Change Animations?

I have a series of animator parameters that I’d like only one to be true at a time (for the most part), so one way of doing it is this:

anim.SetBool ("isIdle", false);
anim.SetBool ("isWalking", true);
anim.SetBool ("isJumping", false);
... etc.

But that is very messy and inefficient. I know there would be a way to loop through each parameter with a for loop and I’ve looked up ways of doing this but it’s not working. I’ve tried this:

for (int i = 0; i < anim.paramaters.Length; i++)
{
     anim.ResetTrigger (i);
}

And although the above code does loop through each parameter, it does not reset the triggers nor the booleans using the i variable.

Any help would be greatly appreciated!

ResetTrigger(int) takes the hash of the parameter name. Sadly, there’s no way to get the states of the animator controller at runtime.

The easiest thing is to build an array of the hashes at startup:

private int[] boolHashes;

void Awake() {
    boolHashes = new int[] {
       Animator.StringToHash("isIdle");
       Animator.StringToHash("isWalking");
       Animator.StringToHash("isJumping");
       ...
    };
}

...

// when you want only isWalking true
foreach(var hash in boolHashes) {
    anim.SetBool(hash, false);
}
anim.SetBool("isWalking", true);

Alternatives is to design your animator so you don’t have to do this, or use a different way of animating. SimpleAnimation is a good alternative. Somebody linked the lite version of Animancer the other day, it looked pretty decent as well.

1 Like

Thank you! Just as a correction, commas after each element in the array, not semicolons :wink:

1 Like