- Home /
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);) 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!
Answer by andrew-lukasik · Aug 21, 2014 at 12:57 PM
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);
}
Well if I replace the code I'm not applying the force the down. I want that the ground "attracts" the player when I release the button (applying a force in the direction of the ground). It is a little difficult to axplain, so here's a video of gameplay of a game and that's the jump I need:
https://www.youtube.com/watch?v=RioD52pr$$anonymous$$Sw
In other words I don't want that I tap and he jumps, but I want that when I keep my finger pressed on the screen it does a normal jump, and if I release my finger while the character is in the air the character falls on the ground quickly (that's why I'm adding a force down)
I hope this time you'll understand. By the way thanks again for your answer!
Ah sure. Excuse me for forgetting about this. I think this should work then:
onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
if ( Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown(0) ) buttonDown = true;
else buttonDown = false;
if ( Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonUp(0) ) buttonUp = true;
else buttonUp = false;
if ( buttonDown && onTheGround ) {
rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
}
else if (buttonUp) {
rigidbody2D.AddForce (new Vector2 (0, -300));
}
Thank you so much! It works. Actually it's a little tricky, sometimes he jumps, sometimes he doesn't. Am I doing something wrong? I'm putting
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown(0)) {
buttonDown = true;
}
else {
buttonDown = false;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButton (0)) {
button = true;
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonUp(0)) {
buttonUp = true;
}
else {
buttonUp = false;
}
In Update, and
onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
rigidbody2D.velocity = new Vector2 (ForwardVelocity, rigidbody2D.velocity.y);
if (buttonDown && onTheGround) {
rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
buttonDown = false;
}
else if (buttonUp) {
rigidbody2D.AddForce (new Vector2 (0, getButtonUpForceDown));
}
if (button == true) {
}
In FixedUpdate. Do I have to put:
if (buttonDown && onTheGround) {
rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
buttonDown = false;
}
else if (buttonUp) {
rigidbody2D.AddForce (new Vector2 (0, getButtonUpForceDown));
}
if (button == true) {
}
In update? Or is there something out of place? Please help the beginner!
Ok I put the controls stuff of FixedUpdate in Update and now it seems to work.
Thats right - FixedUpdate() is a physics-dedicated callback (usually it runs x times more often that Update()) so always put there all AddForce etc. And Update() runs once/frame so it is a good enough place to check input states.