- Home /
Jump only when Jump button is pressed - not held
Hi! Super new to Unity (and programming) here. Hoping to change that though. :) I searched all over but couldn't find an answer that specifically helped solve my issue, so... here goes:
I'm making a 2D platformer. I want my character to jump higher when the button is held down, which is achieved through the following code (in the update function/method).
 if (isGrounded) 
             {
                 jumpTimeCounter = jumpTime; 
             }
 
             if (Input.GetButtonDown ("Jump")) 
                 {
                 if (isGrounded) 
                     {
                         myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
                         stoppedJumping = false;
                     }
                 }
 
             if (Input.GetButton ("Jump") && !stoppedJumping) 
                 {
                 if (jumpTimeCounter > 0f) 
                     {
                         myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
                         jumpTimeCounter -= Time.deltaTime;
                     }
                 }
 
             if (Input.GetButtonUp ("Jump")) 
                 {
                     jumpTimeCounter = 0f;
                     stoppedJumping = true;
                 }
Now, that works great. My problem now is that the character keeps jumping when I'm holding space, which often happens accidentally, making the player jump when he/she didn't mean to.
How can I make it so that the player can only jump when space is PRESSED, not HELD when using Input.GetButton (which is used to add force to the continued jump)? I experimented with booleans on the Input.GetButtownDown and Input.GetButtonUp, but it seems the code in between those (the Input.GetButton-code) screws this up.
If anyone could help me out with this, it would be hugely appreciated.
All the best
Answer by kpf1220 · Nov 24, 2021 at 03:39 PM
do you have any update on how to fix because i am having the same problem?
Your answer
 
 
             Follow this Question
Related Questions
Long press for charged Jump 1 Answer
Raycast2D not working 1 Answer
Problem with Jump 1 Answer
Jumping effectively in a 2D sidescroller 0 Answers
How do I get the character to jump in a 2d game, which is compatible with joysticks and keyboards? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                