Trying to add Variable Jump Height Physics & Input

Hey guys,

I’m working on my jump physics for my 3rd Person Adventure game and so far the jumping works fine. I hit space the player jumps, but what I am looking for is to make him jump like Mario, with variable jump heights.

I’ve followed some tutorials and had it working for a bit but after I’ve re-organized my inputs into Update and took them out of FixedUpdate, I ran into problems.

Looking for a solution to this and get my player jumping based on how long the button is being held down. So far I’m using the JumpTimeCounter technique that sets a timer to see how long the button is held. Also, I’m trying to get him to only jump once, despite the button being held down.

All help appreciated, thank you :slight_smile:

private void GetInput()
    {
        //Jumping Inputs
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            isJumping = true;
        }
        else
        {
            isJumping = false;

        }

    }



private void Jump()
    {
        //jumping
        if (isJumping)
        {
            jumpTimeCounter = jumpTime;
            jumpTimeCounter -= Time.deltaTime;
            Vector3 jumpVelocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
            rb.velocity = jumpVelocity;
        }
        else { isJumping = false; }

    }

Here is a quick example, the default values work for a 1kg rigidbody:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleMove : MonoBehaviour
{
    [SerializeField] private float jumpForce = 1500;
    [SerializeField] private float maxJumpForce = 500;

    private Rigidbody rb;
    private float jumpTimer = 0;
    private bool isGrounded = true;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        GetInput();
    }

    private void GetInput()
    {
        if (isGrounded)
        {
            if (Input.GetButton("Jump")) jumpTimer += Time.deltaTime;
            if (Input.GetButtonUp("Jump")) Jump();
        }
    }

    private void Jump()
    {
        float force = jumpForce * jumpTimer;
        if (force > maxJumpForce) force = maxJumpForce;
        rb.AddForce(new Vector3(0, force, 0));
        jumpTimer = 0;
    }

}

You need to code your own ground check in, as it is, it will always be true. Update isGrounded just before you check for input.

1 Like

I should say that if you are going to add this to a more complex physics scene it would be a good idea to flag a jump as being required from the GetButtonUp and then check for it and apply the force in FixedUpdate. Whilst the previous code will work, it is good practice to apply forces in FixedUpdate, not Update.

1 Like

Alright I’ll figure out how to apply this. Thanks :slight_smile:

What I ended up doing instead was putting all of the JumpTimeCounter stuff in with the GetInput Function and it works perfectly. As you hold space, it adds +1 to the Counter and when it gets equal to the set JumpTime, it returns the jump false and resets the counter to 0.

        //Jumping Inputs
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            isJumping = true;
        }
        if (Input.GetButton("Jump") && jumpTimeCounter < jumpTime)
        {
            jumpTimeCounter += 1f;
        }
        else if (Input.GetButtonUp("Jump") || jumpTimeCounter >= jumpTime)
        {
            isJumping = false;
            jumpTimeCounter = 0;
        }