2D Jumping and DoubleJump Height

Hello everyone. I’m currently trying to get a jumping control setup for a character, but it’s not going quite as smooth as I was hoping.

    // FixedUpdate
    void FixedUpdate () {
        //grounded + anim
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        myAnim.SetBool ("isGrounded", grounded);

        //doublejump reset
        if (grounded)
            doubleJump = false;
            jumping = false;

        //jumping?
        if (!grounded)
            jumping = true;
    }

    //Update
    void Update(){
        //jumping
        if ((grounded || !doubleJump) && Input.GetButtonDown("Jump")){
            GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
            myAnim.SetBool ("isGrounded", grounded);

            //doublejump
            if(!doubleJump && !grounded)
                doubleJump = true;
                jumping = true;
        }               
    }
}

I can jump just fine, but my problem is when I double jump the height is different depending on where in the jump arc the second jump input is used.

If I press it quickly, I jump rather high, and if I press it right before I hit the ground it turns into more of a pause than a jump. Any tips would be appreciated… still trying to get the hang of this stuff.

You can try this, instead of AddForce:

rb.velocity = new Vector2(rb.velocity.x, jumpForce);

Just make sure to “tone down” the jumpForce variable when trying this, as velocity requires much less than force to do the same height :wink:

1 Like

Thanks again! You make this stuff seem so easy!

If you don’t mind a followup question, what would be the best way to make the first jump variable (based on length of time jump button is held) where as the double jump would always stay a static height?

No problem. As for some thoughts on how you could make the first jump a variable height, this might work for you (I noticed the very occasional quick tap would result in non-jump , though):

// whatever your jump button is instead of mouse button here*
 if (jumping && !doubleJump && !Input.GetMouseButton(1))
{
   if (rb.velocity.y > 0) rb.AddForce(Physics2D.gravity);
}

I put that at the end of the FixedUpdate method.

1 Like