currentSpeed doesnt change when pressing Shift (C#)

Hello.
I’m fairly new to programming and game making so I’m setting up my first character controller.

What I have is a Player who can walk along the x and z axis. I have 3 floats in my script named “walkSpeed”, “sprintSpeed” and “currentSpeed” and I would set them up in the Inspector.
ie: walkSpeed = 5 and sprintSpeed = 10.

I want to use the walkSpeed as the normal Speed and the sprintSpeed only if the left Shift key is pressed.

What I got for now:

    public float walkSpeed;
    public float sprintSpeed;
    public float currentSpeed;

	void Update () {

        if (Input.GetKeyDown("left shift"))
            currentSpeed = sprintSpeed;
        else
            currentSpeed = walkSpeed;

        float verMov = Input.GetAxis("Vertical") * currentSpeed;
        float horMov = Input.GetAxis("Horizontal") * currentSpeed;

        verMov *= Time.deltaTime;
        horMov *= Time.deltaTime;

        transform.Translate(horMov, 0, verMov);

	}

In my understanding the currentSpeed should change to 10 IF the left shift key is pressed and ELSE the currentSpeed should stay 5.
but when i start the game the currentSpeed stays 5 even when im pressing left shift.

I would love to hear your tips regarding this code. And best you can do is finding a way to let this script work and not posting your script and saying “replace this with yours it works 100%”.

Thank you :slight_smile:

EDIT:
when I revert
currentSpeed = sprintSpeed and currentSpeed = walkSpeedto sprintSpeed = currentSpeed and walkSpeed = currentSpeed
the walkSpeed goes instantly to 0 and if I press the shift key the sprintSpeed changes from 10 to 0.

That shows that the code recognizes that the key is pressed.

Did you check what GetKeyDown returns and when ?

Returns true during the frame the user starts pressing down the key identified by name.

So (if all else is OK) the speed will be sprintSpeed for the duration of 1 frame and go back to walkSpeed the next (unless you managed to release and press the button again in about a 60th of a second). Use lnput.GetKey() instead. It returns true every frame the key is being pressed.

And yes, you should always use KeyCode.LeftShift instead of a hardcoded string because

A) you can’t typo a variable name without getting a compiler error, but it’s easy to accidentally write "left shift " and not notice the space at the end

B) if a Unity update should change the value of that keycode, your code would break, but code using the variable would not.