Faster/Better/Cleaner way to activate animations

Hey there!

I’m using some booleans for the activation of animations of two characters. This is my code so far, it works but I think I will have spaguetti code after a bit.

Is there any better way on how to do this? Maybe a cleaner and more manageable way?

if (idText == randomString.Length)
        {
            if (score > (idText / 2))
            {
                stateMatch.SetActive(true);
                displayWin.text = "You Win";
                timerCount = 0;
                chanAnimator.SetBool("winGame", true);
                zombieAnimator.SetBool("dead", true);
                zombieAnimator.SetBool("timerUp", false);
                chanAnimator.SetBool("matchText", false);
                zombieAnimator.SetBool("damaged", false);
            }
            else
            {
                stateMatch.SetActive(true);
                displayWin.text = "You Lose";
                timerCount = 0;
                chanAnimator.SetBool("dead", true);
                zombieAnimator.SetBool("win", true);
                zombieAnimator.SetBool("timerUp", false);
                chanAnimator.SetBool("matchText", false);
                zombieAnimator.SetBool("damaged", false);
            }
        }

Thank you

You could combine each pair into a single call outside of the if statements, and only the ones specific to each condition are called within the if statements. That is, “damaged”, “matchText”, “timerUp”, and “dead” are all set no matter what. Also, “win” and “winGame” are both very similarly named; is that a typo or just a poor naming scheme?

(Aside from the animator calls, setting timerCount and stateMatch.SetActive are common to both, and could be moved outside the if statements)

displayWin.text = (score > idText / 2) ? "You Win" : "You Lose";
timerCount = 0;
chanAnimator.SetBool("dead", true);
zombieAnimator.SetBool("win", true);
zombieAnimator.SetBool("timerUp", false);
chanAnimator.SetBool("matchText", false);
zombieAnimator.SetBool("damaged", false);

Yes, that’s a poor naming scheme, not a typo. Just removed the timerCount and stateMatch.SetActive from the if statements.

Well, this does not do the same, some animations have to be cancelled and others played, thanks for the input though.

if (idText == randomString.Length)
        {

            bool winRar = score > idText/2;

            stateMatch.SetActive(true);
            displayWin.text = winRar? "You Win" : "You Lose";

            timerCount = 0;

            chanAnimator.SetBool("winGame", winRar);
            chanAnimator.SetBool("dead", true);
            chanAnimator.SetBool("matchText", false);

            zombieAnimator.SetBool("timerUp", false);
            zombieAnimator.SetBool("damaged", false);
            zombieAnimator.SetBool("win", winRar);

        }
1 Like

The real problem is that you have a “god class”.

You should have a Zombie class that references an animator. It should listen to an Event (ScoreChanged) from a Score class. Then change itself to dead/damaged/etc.

Ideally, the Zombie class would also have a state machine of some kind, so you can set the animator to false automatically when it exits the state. Like this simple one:

IEnumerator DeadState()
{
    // On Enter
    animator.SetBool("IsDead", true);

    // On Update
    while (state == State.Dead)
    {
        yield return null;
    }

    // On Exit
    animator.SetBool("IsDead", false);
}
1 Like