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
EDIT:
when I revert
currentSpeed = sprintSpeed
and currentSpeed = walkSpeed
to 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.