How can I limit jump height in unity?

Hello all,

I’m trying to store the position.y of the character at the beginning of the jump, and compare the new position.y of each frame update with the initial value so I can stop the jump early. ( I cannot use velocity for this since I need a fast jump and I want control the minimum height as well). I don;t know how to store the initial position since the best form I got to updates my h it each frame:

 float h;
 float offset = 0.2f;

 void FixedUpdate{
 void betterJump()
    if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
        h = playerRB.position.y;
        Debug.Log("h is " + h);
        animator.SetBool("isPjumping", true);
        playerRB.velocity = Vector2.up * jumpVelocity;
        jumpCount = 1;
    //better jump
    if (playerRB.velocity.y < 0)
        playerRB.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
    else if (playerRB.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
        if (h + minJump < playerRB.position.y)
            playerRB.velocity = Vector2.up * Physics2D.gravity.y * lowJumpMulti * Time.deltaTime;            
    else if (playerRB.velocity.y == 0 && !IsGrounded())
        playerRB.velocity += Vector2.up * Physics2D.gravity.y * (2 * fallMultiplier) * Time.deltaTime;
    if (playerRB.velocity.y>0 && playerRB.position.y >= h + maxJump)
        Debug.Log("it works");
        playerRB.velocity = Vector2.down;
        playerRB.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;


You apply the velocity when the player is grounded. Then you immediately check if it’s exceeding the height, but it can’t, as you just confirmed it is still grounded, and the rigidbody will need some steps in the physics simulation to actually have that velocity cause a change in transform.position. Nothing yet got moved.

So, for starters, move your height limiting check outside the whole isGrounded scope; it should constantly get checked.

On a side note, in your code sample it reads fixedUpdate{ but I suppose your original uses the correct FixedUpdate(){

Good luck!

Check out this video: Better Jumping in Unity With Four Lines of Code - YouTube

I don’t really know what this line is supposed to accomplish:

if (h+ maxJump + offset >= playerRB.position.y && playerRB.position.y >= h + maxJump - offset)

What you’re basically saying is 20 + 5 + 0.2 >= 30 && 30 >= 20 + 5 - 0.2. That indeed is never gonna be true. If you want it to trigger while the jump is higher than the minimal offset but lower than the maximum offset you should use

if (playerRB.position.y <= h+ maxJump + offset && playerRB.position.y >= h + maxJump - offset)

This way it will trigger when the player is anywhere between “h + maxJump - offset” and h + maxJump + offset.

But if the player is never supposed to be above h + maxJump you can also just use:

if (playerRB.position.y >= h + maxJump)

Might be a lot more clear than using an offset and such. This way it’ll also trigger if the velocity is so high that the character moves more than offset*2 within one frame.