Action on click instead of hold button.
I know this isn't very hard, but I'm a beginner and I can't seem to find the answer anywhere.
I'm making a simple runner game, and it's coming along nicely, but right now, when I hold the left mouse button, the jump action keeps repeating. I would like the player to be required to release the mouse button before being allowed to jump again.
I am using the code beneath, and I would expect it to already work the way I described, but it doesn't. My question is wether there is a way to tell the game to execute the code when the button is pressed, but NOT when it is held.
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
MyRigidbody.velocity = new Vector2(MyRigidbody.velocity.x, JumpForceInitial);
stoppedJumping = false;
jumpSound.Play();
}
if(!grounded && canDoubleJump)
{
MyRigidbody.velocity = new Vector2(MyRigidbody.velocity.x, JumpForceInitial);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
jumpSound.Play();
}
}
Now I know I should be able to make a bool that checks onmouseup(), but I expect there to be a much easier solution. Does anybody know if there is one?
Your answer
Follow this Question
Related Questions
Does OnMouseDown() work for one object only at a given scene? 1 Answer
Instantiate PreFab on trigger location 0 Answers
OnMouseDrag() stopped working after few clicks 0 Answers
Click detection suddenly stopped working 1 Answer
How to allow clicks to go through Circle Collider 2D on sprite prefab? 0 Answers