Using a switch to change animations

Hello! I’m fairly new to Unity and ran into a problem.
So I wanted to make the Dinosaur Game that appears in Google Chrome. One gimmick that the game has is that the speed increases over time, and the faster you go, the faster the dinosaur runs. For this, I made 10 different animations (5 for running and 5 for crouching), and since I didn’t want to have YandereDev code 2.0 I decided to use a switch statement for this. (Ignore the top part as that is the movement)

private void Movement()
    {
        rb.velocity = new Vector2(PlayerSpeed, rb.velocity.y);
        if (PlayerSpeed < 13)
        {
            PlayerSpeed += speedChangePerSecond * Time.deltaTime;
        }
      

        if(IsGrounded())
        {
            if (IsCrouching() == false)
            {
                //Animations for walking/running
              
                switch (PlayerSpeed)
                {
                    case 3:
                        ChangeAnimationState(RUN_SLOW);
                        break;
                    case 5:
                        ChangeAnimationState(RUN_SLOWMEDIUM);
                        break;
                    case 7:
                        ChangeAnimationState(RUN_MEDIUM);
                        break;
                    case 9:
                        ChangeAnimationState(RUN_MEDIUMFAST);
                        break;
                    case 11:
                        ChangeAnimationState(RUN_FAST);
                        break;
                }
            }
            else
            {
                //Animations for crouching
              
                switch (PlayerSpeed)
                {
                    case 3:
                        ChangeAnimationState(CROUCH_SLOW);
                        break;
                    case 5:
                        ChangeAnimationState(CROUCH_SLOWMEDIUM);
                        break;
                    case 7:
                        ChangeAnimationState(CROUCH_MEDIUM);
                        break;
                    case 9:
                        ChangeAnimationState(CROUCH_MEDIUMFAST);
                        break;
                    case 11:
                        ChangeAnimationState(CROUCH_FAST);
                        break;
                }
            }
        }
    
}

However, only the top animation plays, even if the speed changed to 5. The crouching animations don’t display when I press the down key, though the colliders used in there do work as intended.

Is there any way that I can fix the switch so that it works as intended?

So, one thing that immediately stood out to me, is you’re using switch with a float. Float comparisons don’t behave as you’d expect, and not only that, but the chance that the Player’s speed perfectly lines up with your cases is extremely unlikely. One thing you’re going to want to do is round it to an int to have it line up better.

You can use comparison in switch, and you probably should if you are using float.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-switch-statement
Like:

switch (PlayerSpeed)
{
    case <= 3f: ChangeAnimationState(RUN_SLOW); break;
    case <= 5f: ChangeAnimationState(RUN_SLOWMEDIUM); break;
    // ...and so on and so forth...
}

But even better if you pass the speed into the animation system as a parameter and decide there what animation to play and when. See the usage of that in the manual.

1 Like