# “Hold to jump higher” suddenly outputs a much higher jump

I'm programming my first game in Unity and I'd like to make a very controllable jump.

The idea is to apply an upward force while the player is still pressing the jump button, and then after some time ignore jump input. Kinda like how Mario and Mega Man jump.

Problem is, somehow, my character occasionaly jumps much higher than it normally does.

I set three variables on the inspector:

• jumpImpulse: the initial speed after pressing the jump button;
• riseAcc: the acceleration applied to the character while holding the jump button;
• maxJumpHoldTime: the longest possible time one might hold the button while still rising.

if ((onGround || onPlatform) && Input.GetButtonDown("Jump")){ //GetButtonDown prevents holding jump and keeping jumping

rb.AddForce(Vector2(0,(jumpImpulse/rb.mass)), ForceMode2D.Impulse); //force that starts the jump itself.
//  /rb.mass makes F=ma into F=ma/m ==> F = a, so "Force" is actually acceleration.
//  This is to better control mass and jump mechanics.
jumping = true;

//DEBUG
debugJumped = true;
}

if (jumping && Input.GetButton("Jump")){

jumpHoldTime += Time.deltaTime;

if (jumpHoldTime >= maxJumpHoldTime) {
jumping = false;
}

} else {

jumping = false;
jumpHoldTime = 0;

}

//Made this so I know how high the character jumps.
if (debugJumped && (!onGround) && rb.velocity.y <= 0){
Debug.Log ("Jump height is: " + transform.position.y);
debugJumped = false;
}


In the console, the jump height is listed normally as 2.376213, but sometimes it appears as 5.008272. It appeared once as 2.377432. Also, this variation seems to appear more frequently when I'm also moving the character left or right.

And I'm not sure if this is related, but there are many times when I press the jump button and the character simply doesn't jump.

Also, I put everything here under the FixedUpdate function.

In my experience handling input in fixed update is a bad idea.

All your input handling should be in Update() and physics work in FixedUpdate().

I'm not quite sure if this will completely fix your issue but it sounds like the cause of:

And I'm not sure if this is related, but there are many times when I press the jump button and the character simply doesn't jump.

Once you apply this fix let me know if you are still getting moments of increased jump height.

• Well, apparently everything is fine now, but I still wish I could understand it better. :P Thank you. – Genú May 27 '15 at 22:19

This problem I believe comes down to controlling the player velocity. Check what the object's velocity is before and after applying force and apply other forces accordingly. This way other forces that may or may not be acting on your character wont stop it from completing consistent jump arcs

It seems like you want to apply a steady upward force to the character while the button is held down. This force should be equal to whatever downward forces (like gravity) are acting on it so the character's velocity will remain unchanged.

If the upward force you apply every time Update is called is greater in magnitude than the downward forces are, your character will accelerate upward. This means each time Update is called, your speed will be greater and greater. This could get out of hand quickly, and may be the source of your problem.

Also, it seems like you are using transform.position.y to measure the height of the jump. This will give you the character's altitude in the world, not how far off the ground she has jumped. For example, if the character was on a platform that was 5 units off the ground, and you can jump 2 units high, transform.position.y will be 7 at the apex of the jump.

• Thanks for helping me understand things better, but it didn't really fix my main problem :/ – Genú May 27 '15 at 22:20