Hold space longer = Longer jumps?

Hey all,

I’m trying to make a game where the jump length is changed based on if the player is holding the space bar or just tapping it.

I honestly don’t have an idea of where to start with this, so all help is appreciated.


Let’s consider you are doing a 2D platform game. If not, no worry, the principle remains the same.

Her is a class that allows you to do some movement in 2D and the jump has a minimal jump and if you keep on pressing the jump is larger:

public float speed = 6.0F;
public float jumpSpeed = 20.0F;
public float gravity = 20.0F;
public float gravityForce = 3.0f; 
public float airTime = 2f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private float forceY = 0;
private float invertGrav;

void Start(){
    // invertGrav is set greater than gravity so that our guy jumps
    invertGrav = gravity * airTime;
    controller = GetComponent<CharacterController>();
void Update() {   
    moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0);
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;	
    if (controller.isGrounded) {
        // we are grounded so forceY is 0
        forceY = 0;
        // invertGrav is also reset based on the gravity
        invertGrav = gravity * airTime;
        if (Input.GetKeyDown(KeyCode.Space)){
            // we jump 
            forceY = jumpSpeed;
    // We are now jumping since forceY is not 0
    // we add invertGrav to our jumpForce and invertGrav is also
    // decreased so that we get a curvy jump
    if(Input.GetKey(KeyCode.Space) && forceY != 0 ){
        invertGrav -= Time.deltaTime;
        forceY += invertGrav*Time.deltaTime;
    // Here we apply the gravity
    forceY -= gravity*Time.deltaTime* gravityForce;
    moveDirection.y = forceY;
    controller.Move(moveDirection * Time.deltaTime);

It might not be the best and it probably needs some modifications but it works…

I recommend you to work with AddForce. The first time you press space, apply an instant force with ForceMode.VelocityChange, this will be the basic jump, and then, while Space button is pressed, continue using AddForce but with ForceMode.Acceleration as parameter so each frame Space key keep pressed, yo apply a force to your character so it will continue going up before start falling.

void Jump ()
    if (Input.GetKeyDown (KeyCode.Space)) {
        rigidbody.AddForce(Vector3.up * 10,ForceMode.VelocityChange);
    if (Input.GetKey (KeyCode.Space)) {
       if (jumpValue < maxJumpValue) {
         rigidbody.AddForce(Vector3.up * 10,ForceMode.Acceleration);