Jumping problems (how to stop a force)

Hi I have another question. This is an “auto-runner” game for ios and android, where the caracter runs to the right (rigidbody2D.velocity = new Vector2 (ForwardVelocity, rigidbody2D.velocity.y):wink: and you have to jump to avoid obstacles and jump on enemies.

My character jumps if I hold the button, and if I hold the button and release when the character is on the air he goes fast to the ground (like in Kid Tripp, a game for ios)

To do this, I said:

onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)) {

			buttonDown = true;

		}

		if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton(0)) {

			button = true;

		}

		if (Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {

			buttonUp = true;

		}


if (buttonDown == true && onTheGround == true) {

			rigidbody2D.AddForce (new Vector2 (0, jumpForce), ForceMode2D.Impulse);

			buttonDown = false;

		}

		if (button == true) {


		}

		if (buttonUp == true) {

			rigidbody2D.AddForce (new Vector2 (0, -300));


}

But when I release the button, the force stays applied forever, and that is the problem because the force stays applied when I’m on the ground after the jump and slows my character down, and if I jump again the jump is less powerful than the first one because there’s the “-300” force is still applied.

I tried adding the same force but positive when I’m on the ground, and that solves the first problem (the character doesn’t slow down on the ground anymore) but when I jump again the second jump is still less powerful than the first one.

I also tried adding the positive force on the air but when I jump he goes up and never return.

Does anyone know the answer to this?

The character has two colliders: a circle collider over the feet and a box collider over the body (if this helps).

Thanks in advance!

Try replacing all this code with this instead:

onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
if ( Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0) ) buttonDown = true;
else buttonDown = false;
if ( buttonDown && onTheGround ) {
	rigidbody2D.AddForce (new Vector2 (0, jumpForce), ForceMode2D.Impulse);
}