Jump delay/ Jump Maximum,How to add jump delay/jump maximum

Hi,

I am very new to unity and I am currently making a 3D game that requires jumping. So far I have got the jump button working but once you hold down the jump button you can basically fly. Please help me fix this.

Thanks

my code:
if ( Input.GetKey(“space”) )
{
rb.AddForce(0, 1, 0, ForceMode.VelocityChange);
},Hi,

I’m very new to Unity and I am currently making a game that requires jumping. I got jumping to work but once you hold down the jump button you can basically fly. Please help me fix this thanks.

my currently code:

if ( Input.GetKey(“space”) )
{
rb.AddForce(0, 1, 0, ForceMode.VelocityChange);
}

Hi!

First of all Input.GetKey will return you true while you are holding Key down, but Input.GetKeyDown will return it once when you pressed the Key down.

The code above implement delay between jumping and also when you are holding Space you will jump higher, if you don’t want it then you can remove that part out of script.

/* These 2 variables only for jump smoothness when holding Space key, remove if no need */
float f_FallGravity = 3f;           // Additional gravity multiplier when vertical rigibody velocity < 0
float f_AdditionalGravity = 2f;     // Additional gravity multiplier when not holding jump button

float f_JumpSpeed = 5f;             // The overall jump speed
Rigidbody rb;                       // Rigidbody reference
bool is_Grounded = true;            // Boolean for grounding check

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

private void Update()
{
    /* We call linecast to check if we have any collider between our transform.position and transform.position + Vector3.down * 0.6f
     * Keep in mind the second position will depend on your character size and pivot point */
    is_Grounded = Physics.Linecast(transform.position, transform.position + Vector3.down * 0.6f);

    /* If we press ONCE Space button and we Grounded (what means that our Linecast hitted collider previosly
     * We add vertical velocity to rigidbody and set is_Grounded boolean to false */
    if (Input.GetKeyDown(KeyCode.Space) && is_Grounded)
    {
        rb.velocity = Vector3.up * f_JumpSpeed;
        is_Grounded = false;
    }

    /* This is only for more smoothness in jump - remove it if you don't want it
     * When rigidbody vertical velocity < 0 we don't care if we holding Space we apply standard additional gravity;
     * When rigidbody vertical velocity > 0 and we do not hold space we apply additional vertical velocity
     * The logic is that if you hold space you will jump higher, if no - you will fall faster */
    if(rb.velocity.y < 0)
        rb.velocity += Vector3.up * Physics.gravity.y * f_FallGravity * Time.deltaTime;
    else if(rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
        rb.velocity += Vector3.up * Physics.gravity.y * f_AdditionalGravity * Time.deltaTime;

}

Hope that helps.