- Home /
2D game: When going from left input to right input fast the player stops moving
For awhile I was trying to get the player to stop moving when I don't press any keys I did get that to work but the if for stopping the movement is running when I quickly go in the other direction. I tried to use an and in the if statement but that just never ran. I know something is wrong with my if argument or when isMoving should be equal to true but I just can't figure it out on my own.
void Update() { //Player inputs if (Input.GetKeyDown("a") && isMoving == false) { playerRigidbody.velocity = new Vector2(-speed, 0.0f); isMoving = (true); }
if (Input.GetKeyDown("d") && isMoving == false)
{
playerRigidbody.velocity = new Vector2(speed, 0.0f);
isMoving = (true);
}
if (Input.GetKeyDown("space"))
{
if(isGrounded)
{
playerRigidbody.AddForce(transform.up * jumpForce);
}
}
if (isMoving == true && Input.GetKeyUp("a") || Input.GetKeyUp("d"))
{
isMoving = (false);
playerRigidbody.velocity = new Vector2(0f, playerRigidbody.velocity.y);
Debug.Log("Left and right keys are up");
}
}
Big thanks if you make a comment.
Your answer
Follow this Question
Related Questions
help with boss death (bool) 1 Answer
(C#) If statements based on variables in another script 1 Answer
Some Boolean "Action" Trouble 2 Answers
How to change boolean state based on User Input? 1 Answer
I'm having trouble setting a bool. 2 Answers