- Home /
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
, GetKeyDown
etc. and nothing seems to be working. Any ideas?
Answer by Captain_Pineapple · Nov 11, 2021 at 12:23 PM
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:
you go into the function and speed has some unknown value
you check if shift is pressed and you are grounded. if this is the case speed will be set to either 36 or 12.
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.
I tried that and it didnt work - because I'm checking it each function it just multiplied my speed by x0.5 or 2 each frame (took me a while to figure out cause i just went absolutely flying :) ) - would assigning a variable (e.g true if shift pressed) and then checking that each time i change the characters speed be better?
Can you share your updated code?
I am certain you simply misunderstood what i meant:
if (Input.GetButton("Sprint") && isGrounded)
{
speed = 36f;
} else
{
speed = 12f;
};
if (Input.GetButton("left control") && isGrounded)
{
speed *= .5f;
}
Ideally both would be modifiers and the actual base speed is defined elsewhere
speed = baseSpeed; if (Input.GetButton("Sprint") && isGrounded) { speed = 3f; } if (Input.GetButton("left control") && isGrounded) { speed = 0.5f; } // use speed here
So if baseSpeed is 12 you get 12 when nothing is pressed. You get 36 when sprinting and 6 when "crouching". When you press both at the same time you get a value of 18 since we multiply the base speed by 3 and divide the result in half.