[SOLVED] Inconsistent Jumping Height

Hello everyone. I’m having trouble with making my character jump. I can make him jump but the height of that jump is inconsistent. I don’t want him to jump very high, but sometimes he just rockets up into the air, and I don’t know what is causing it. Here’s my code. I took out everything except what related to jumping.

public int jumpSpeed; //Jump speed.
private bool canJump; //Whether the player can jump or not.
private Rigidbody rb; //Gives access to the rigidbody.
void Start(){ //Specifies the Rigidody.
        rb = GetComponent<Rigidbody>();
    }
void FixedUpdate () {
    PlayerJump();
}
void PlayerJump(){ //Controls jumping.
    if((canJump == true) && (Input.GetKeyDown(KeyCode.Space))){
        Vector3 jump = new Vector3(0, jumpSpeed, 0);
        rb.AddForce(0f, 0f, 0f);
        rb.AddForce(jump, ForceMode.Impulse);
    }
}
//Checks if the player is on ground.
void OnCollisionEnter(Collision other){
    if(other.gameObject.tag == "Ground"){
        canJump = true;
    }
}

void OnCollisionExit(Collision other){
    canJump = false;
}
//==================================

Any help would be greatly appreciated.

A few things that could help might be:

a) Check for the jump input in Update(). You can safely add a 1 time force in Update(), also, unless there is more in FixedUpdate that was removed for your post. If that’s the case, and you’d rather have it there, then you can set a bool value to true, for the jump input, and once it’s “consumed” in FixedUpdate() set it back to false. This could prevent possibly having 2 fixed updates run in a row - maybe that was your issue, I’m not sure.

b) You might want to check that you left the proper tag when setting ‘canJump’ to false in the exit collision.

c) It might be better still to check if you’re grounded with a small raycast; I find that’s a lot easier, but to each their own.

3 Likes

Thanks for the answer methos5k! I tried what you said and changed it to this:

    void PlayerJump(){ //Controls jumping.
        if((canJump == true) && (Input.GetKeyDown(KeyCode.Space))){
            Vector3 jump = new Vector3(0, jumpSpeed, 0);
            rb.AddForce(0f, 0f, 0f);
            rb.AddForce(jump, ForceMode.Impulse);
            canJump = false;
        }
    }

After putting canJump to false after adding force it hasn’t had the inconsistent jump at all! I really appreciate the help! As for using a raycast, I saw a lot of places suggesting that but I’m just kinda tinkering right now. Anyway thanks again for the help!

2 Likes

No problem :slight_smile:

I once had built a character with a similar design to yours and eventually went back to a raycast, but we all have our own journeys :slight_smile:

I’m glad that I could help, and that it’s working better for you now.

1 small thing, which is probably not hurting anything, is that I’m 90+% sure that the line “rb.AddForce(0,0,0)” does nothing :wink:

1 Like

Oops, your right. That line is pointless. Well, live and learn. Thanks again. :slight_smile:

Ya, not a big deal just thought I’d mention it. :slight_smile: Take care.

1 Like

Thanks for putting up your code fix. This helped me.

1 Like

Thank you! That was so helpful. I had the exact same problem and the boolean in Update tactic worked for me.

My Solution
Right Before you jump you have to cancel the velocity on y axis

public void Jump()
    {
        if (!isGrounded)
            return;
        rb.velocity = new Vector2(rb.velocity.x, 0f);
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    }

Also you have to cancel velocity when you set isGrounded to true.

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Platform")
        {
            isGrounded = true;
            rb.velocity = Vector3.zero;
            rb.angularVelocity = 0f;
            
            
        }
    }

Check my youtube channel

3 Likes

Thanks

Thank you so so much i was struggling as well my code was so close to this but i was using AddRelativeForce and my RigidBody was not set up correctly im kinda a noob at this so thank a ton mate

Thanks This solved my issue.

I know that his is an old thread. But just wanted to say, it did exactly solve my problem (although i am not using a RB, bot moving my character with a CC instead).
The tip with switching off the gravity that is needed to keep the isGrounded stable during take-off was exactly the right one !

-Michael

7 years later struggling with the same and finding the answer here !
Thx !

1 Like