- Home /
The question is answered, right answer was accepted
(Solved) Double jumping
Hi, I'm simply trying to make my player able to double jump. So far I've been able to make my player able to jump once until it's grounded again; then I tried to add a jump counter. However Unity gives me an error message on jumpCount +1; Does anyone know what I've done wrong and how to fix this?
public float moveSpeed = 10f;
public float jumpForce = 50f;
int jumpCount = 1;
bool grounded = false;
void Update ()
{
//Jumping
if(jumpCount > 3 || grounded && Input.GetKeyDown(KeyCode.UpArrow))
{
rigidbody2D.AddForce(new Vector2(0,jumpForce));
grounded = false;
jumpCount + 1;
}
}
if((count <3 || grounded ) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)) jumpCount less the 3 then don't forget about operators precedence, without parentheses the && will be evaluated before the ||
Here is my simple solution : int jumpCount = 1;
if(Input.GetButtonDown("Jump") && jumpCount < 2)
{
rb.AddForce(Vector2.up *150f);
jumpCount++;
}
if(rb.velocity.y ==0){
jumpCount = 1;
}
so when the player collision with the ground (rigid body velocity y = 0 ) the jump count will reset to 1, and you can change the jumpCount in if condition to measure the jump count like jumpCount <3 for 3 jump and more and more..
Answer by Komak57 · Feb 18, 2014 at 07:45 PM
Usually, when jumping, you would want to zero out vertical velocity before adding force to counter any gravity. Next, you have 2 scenarios you want to watch for. Grounded & jumping, Airborn & jumping & candoublejump. So you want something like
if (jumpkeydown) {
if (grounded) {
rigidbody2D.velocity.y = 0;
rigidbody2D.AddForce(new Vector2(0, jumpForce));
candobulejump = true;
} else {
if (candoublejump) {
candoublejump = false;
rigidbody2D.velocity.y = 0;
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
values and descriptions may vary.
Thank you very much. This is very simple and works great -Cheers :D
Thank you so very much man! :D I was having a problem with my character jumping WAY too high when you double tap the jump button and setting the y-axis velocity to 0 solved that.
For Unity 5.1 users, ins$$anonymous$$d of:
rigidbody2d.velocity.y = 0;
Do this:
rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, 0);
Thanks!
Answer by Josh707 · Feb 18, 2014 at 07:42 PM
jumpCount > 3 || grounded
I think you mean less than, every time you jump the counter goes up, once it's over 3 you can jump continuously. The error is also because you aren't assigning that calculation to a variable, unlike Python you can't just write '1+1' and it will print the result. It will have to be 'jumpCount = jumpCount + 1;', or just += 1 or better yet jumpCount++.
Ok, you were right about my > mistake and the +=1 seems to work. Now my problem is to somehow reset the jumping counter, do you know how?
to reset jump counter just put
jumpCount = 1;
can you give me a code to limit it two jump rate in a time span ,i have been trying but no success
Answer by JayOhhh · Feb 18, 2014 at 07:43 PM
You should try assigning jump count the +1 value. As you have it right now your variable is not being assigned your +1 value.
jumpCount = jumpCount + 1;
Top comment didnt work for me he just would jump extremely high ins$$anonymous$$d of jumping twice so in the else statement i just wrote a different condition adding the space bar being pressed again... if this helps ive only been program$$anonymous$$g now for a week so if my code is stupid, my bad
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && OnGround && Jumpcount < 2) {
jumpForce = 360f;
rigid.AddRelativeForce (new Vector2 (0, jumpForce));
candoublejump = true;
}
else
{
if (candoublejump == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
jumpForce = 300f;
rigid.AddRelativeForce (new Vector2 (0, jumpForce));
candoublejump = false;
}
}
That code work for me ins$$anonymous$$d of the other one above. Thanks for sharing!
Follow this Question
Related Questions
Character doesn't jump repeatedly 1 Answer
2d Beat Em Up Jump Functionality 0 Answers
Jumping from a floor to another floor (2D) 2 Answers
Accelerating speed 2 Answers