Hello, I want to be able to use axis (a, d) and getkey (left control) simultaneously, but when I walk and then press control I stop walking, but my getkey (left control) does get executed though) and when I release it, I can walk again. Help would be appreciated!
void FixedUpdate () {
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
private void Update()
{
if (speedMultiplyWhenRunningAlreadyHappend == false && Input.GetKey(KeyCode.LeftControl))
{
speed *= 2;
speedMultiplyWhenRunningAlreadyHappend = true;
}
else if (speedMultiplyWhenRunningAlreadyHappend == true && Input.GetKeyUp(KeyCode.LeftControl))
{
speed /= 2;
speedMultiplyWhenRunningAlreadyHappend = false;
}
}
}
This may not work since your code is missing some parts (your speedMultiplyWhenRunningAlreadyHappened may be used somewhere else ?), but try changing the content of you Update() function with the following one :
private void Update()
{
if ( Input.GetKeyDown(KeyCode.LeftControl))
speed *= 2;
else if( Input.GetKeyUp(KeyCode.LeftControl))
speed /= 2;
}
Note the difference; GetKeyDown return true if the key was previously up and has been pressed this frame (so you can spare the use for the boolean), while GetKey tells you whether or not the key is currently held down.
The easiest and most reliable solution is just this:
void FixedUpdate ()
{
moveInput = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.LeftControl))
moveInput *= 2f;
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
You shouldn’t multiple and divide floating point numbers all the time as they might drift away over time. This may not happen with a factor of “2” but odd numbers could result in slight errors over time. In my case we just conditionally muliply the moveInput by 2. Since moveInput is read / updated every fixed frame we get a stable result.
If you want to have a speed variable that holds the current speed (for some reason) you shouldn’t mess with your variables that you use as setting in the inspector. Instead use a seperate variable.
// set those in the inspector, don't mess with them in code
public float walkSpeed;
public float sprintMultiplier = 2;
// This will hold your actual speed:
private float speed;
void FixedUpdate ()
{
speed = walkSpeed;
if (Input.GetKey(KeyCode.LeftControl))
speed *= sprintMultiplier;
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
@Nebukam That didn’t fix my problem, but it made my code cleaner because apparently I didn’t even need a bool speedMultiplyWhenRunningAlreadyHappened. Problem is I wanna move and while moving press ctrl and become faster, release ctrl and go back to normal speed, but now when I press ctrl I just stop and get faster (but I can’t move while pressing ctrl so getting faster is useless) and when I release it I can move again (at regular speed)
Guys, I figured it out. The problem was: apparently control works only when combined with other keys. Since I was pressing only control, I guess it caused complications between pressing control and a, d (axes) simultaneously. I could have added a key to be pressed with control, but instead I just changed control to another key (Z) and now it works. I can move AND hold ctrl for when I wanna run. Thanks for help, @Nebukam @Bunny83