Holding a button and another button to Sprint/Walk - Is there a better way of doing this?

I’ve been trying to figure out how to use Input System to be able to hold down buttons for sprinting and walking and what I have here seems to be working, but I’m wondering if maybe there’s a better way of doing this? And are the if-else statements necessary here? I’m new to programming and I’ve heard stuff about too many if statements being bad.

[SerializeField]
private float baseSpeed = 10f;
[SerializeField]
private float sprintSpeed = 15f;
[SerializeField]
private float walkSpeed = 5f;

private float playerSpeed;

void Update()

{
   if (Sprint.action.activeControl != null && groundedPlayer)
   {
     playerSpeed = sprintSpeed;
   }
       else if (Walk.action.activeControl != null && groundedPlayer)
   {
     playerSpeed = walkSpeed;
   }
        else
   {
     playerSpeed = baseSpeed;
   }

    controller.Move(move * Time.deltaTime * playerSpeed);
}

I think it’s easier to do something like Sprint.action.performed.
You can also apply scaling modifiers to action bindings, and then using ReadValue on the action.

1 Like