- Home /
Hold to jump higher
I have the following code that I have written in order to make a character jump higher if the jump button is held down.
The character performs the initial jump fine but the statement around the button holding never gets triggered since Time.time
is not greater than lastJumpTIme
. Its obvious that I have gone wrong in my calculations somewhere, I just dont know where.
private void HandleJumping() {
if (grounded && ! crouching) {
if (Input.GetButtonDown ("Jump")) {
force = jumpForce;
lastJumpTime = Time.time + 0.5f;
}
}
if (lastJumpTime != 0f) {
Debug.Log (Time.time.ToString () + " - " + lastJumpTime.ToString ());
}
if (Input.GetButton ("Jump") && force != 0f && Time.time > lastJumpTime) {
//force += 100f;
Debug.Log ("HIGH");
}
if (grounded) {
lastJumpTime = 0f;
}
if (force != 0f) {
rb2d.AddForce (new Vector2 (0f, force));
force = 0f;
}
}
you are checking for input.getbutton"Jump" twice, maybe it would be better to have a Boolean for jumping or not.
im at work at the moment, but this seems like a very drawn out solution to a simple function :) ill try to take a look when I get home (if you haven't sorted it by then )
Your answer
![](https://koobas.hobune.stream/wayback/20220612033621im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mobile platformer jump not working 2D 1 Answer
Jump implementation in multi level platforms game 1 Answer
3D Jump Colission Detection 1 Answer
my ball sometimes jumps higher or sometimes not 2 Answers
Rigidbody2D character movement problem 0 Answers