Left Shift being cancelled by left control?

So I am just starting a 1st person platformer and I want to be able to sprint and slow down (kinda like crouch but no ducking). Both of these work individually, but in this script:

void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0) {velocity.y = -2f;}

    if (Input.GetButton("Sprint") && isGrounded) 
    {
        speed = 36f;
    } else 
    {
        speed = 12f;
    };
    
    if (Input.GetButton("left control") && isGrounded) 
    {
        speed = 6f;
    } else 
    {
        speed = 12f;
    };

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    };

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
}

Shifting doesn’t work - no speed increase occurs, but control does. It seems like the shift was being cancelled out. I did experiment with a bunch of stuff like GetKey, GetButtonDown, GetKeyDownetc. and nothing seems to be working. Any ideas?

hey there,

at a point like this i can only give the advice: read your code step by step and write on a piece of paper what actually happens:

  1. you go into the function and speed has some unknown value
  2. you check if shift is pressed and you are grounded. if this is the case speed will be set to either 36 or 12.
  3. you check if control is pressed and you are grounded. if this is the case speed will be set to either 6 or 12.

So at this point you can see, no matter which result we have for point 2: we only ever can achive a speedvalue of 6 or 12 since you overwrite everything that was before.

So best way to solve this would be to have a speed multiplier for your crouch instead of setting this to a fixed value…
something like speed *= 0.5 if the button is pressed.