Cleaning up Animation Controller 'bools'

I have a series of events that trigger animations for my character.

Each one makes a boolean false/true whatever he is doing with controls the booleans in the Animation Controller, and it’s starting to get a bit bulky.

Each control boolean looks like this:

            isSneakingPressed = true;
            m_playerAnim.SetBool("isSneaking", isSneakingPressed);

            isWalkingPressed = false;
            m_playerAnim.SetBool("isWalking", isWalkingPressed);
            isJumpingPressed = false;
            m_playerAnim.SetBool("isJumping", isJumpingPressed);
            isRunningPressed = false;
            m_playerAnim.SetBool("isRunning", isRunningPressed);
            isShootingPistolIdlePressed = false;
            m_playerAnim.SetBool("isShootingPistolIdle", isShootingPistolIdlePressed);
            isShootingPistolPressed = false;
            m_playerAnim.SetBool("isShootingPistol", isShootingPistolPressed);

Does anyone have a link to some tips of how to clean the code up? If only for sanity as it’s getting painful having to set each one when I introduce new events for the character (and I miss one).

That’s because they’re strings, Animator actually uses integers to change it’s variables, when pass a string a conversion happens and an integer is generated which passed to the animator, what you can do is cache animator’s variable.

int isSneaking;
int isWalking;
int isJumping;

void Start(){
    isSneaking = Animator.StringToHash("isSneaking");
    isWalking= Animator.StringToHash("isWalking");
    isJumping= Animator.StringToHash("isJumping");
}


void Jump(){
    m_playerAnim.SetBool(isJumping, false);
}

Further more, these lines you shared, where are you using them?

Thanks cmyd.

They’re in a separate script - from memory, attached to the character, or a gameobject containing a spawned character.

are they in a loop or update?

They are in update

The thing I was about to tidy up/optimise (for my own sanity!) are the several states where a button is pressed and I am passing each SetBool etc in each if statement … ?

I’ve just used these as a staging thing and was always going to get around to doing them properly at some point. Maybe create a function passing back the variables to a single bool run through :slight_smile:

There are roughly seven or eight of these (at the moment):

        if(Input.GetAxis("xboxlb") > 0 && Input.GetButtonDown("xboxrb"))
        {
            isShootingPistolIdlePressed = true;
            m_playerAnim.SetBool("isShootingPistolIdle", isShootingPistolIdlePressed);
             isShootingPistolPressed = true;
            m_playerAnim.SetBool("isShootingPistol", isShootingPistolPressed);

            isSneakingPressed = false;
            m_playerAnim.SetBool("isSneaking", isSneakingPressed);
            isWalkingPressed = false;
            m_playerAnim.SetBool("isWalking", isWalkingPressed);
            isJumpingPressed = false;
            m_playerAnim.SetBool("isJumping", isJumpingPressed);

        }

From what I see you’ve set a bool to true and passed it on to the animator and it’s not changing to false, If that’s the case don’t create that bool just pass true, further more you don’t need to set animator’s parameters every update, change them only when you need to.