- Home /
Problem with Jump
I have a problem with jump in my current project. The good: Character jumps when button is pressed. Character has a controllable height up to a certain limit with a minimum for input.
The bad: If the player holds down the button and approaches a ledge, they will jump automatically. Which might be neat for debugging and making sure platforms are a certain distance away but is not ideal from a game standpoint.
Included is the code and a video. The first part of the code is just to play the jump animation if the player isn't climbing or grounded. So jumping or falling. The rest is the actual code and I know the issue must lie within the GetButton area, I just have no clue how to make it work from this point.
void FixedUpdate() { if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Climbing", "Ground"))) { myAnimator.SetBool("isJumping", true); } else { myAnimator.SetBool("isJumping", false); } if (CrossPlatformInputManager.GetButtonDown("Jump")) { if (grounded && (stoppedJumping=true)) { Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed); myRigidBody.velocity += jumpVelocityToAdd; stoppedJumping = false; } } if(CrossPlatformInputManager.GetButton("Jump") && !grounded) { if (jumpTimeCounter > 0) { myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpSpeed); jumpTimeCounter -= Time.deltaTime; } } if (CrossPlatformInputManager.GetButtonUp("Jump")) { jumpTimeCounter = 0; stoppedJumping = true; } }
Answer by unity_ek98vnTRplGj8Q · Jan 29, 2020 at 09:42 PM
Look at your jumpTimeCounter -- it is resetting to 0.35 as soon as you hit the ground even when you don't let go of the jump button. This is your issue, wherever you are setting that jump timer counter should probably be moved to when you actually press down the jump button. That way it actually stays at zero until you perform a jump.