# How make 2d player only able to fly / jump for 3 seconds if not on the ground?

in my project the player can jump once and then hold jump to fly but its infinite and i do not want that. How can I make it so they only have three seconds to jump? I know you at least need a float perhaps? I am experimenting with an Ienumerator and null. I was thinking like have the ienumerator do something for a certain amount of seconds then return null?

``````public float jumpCooldownRate;
public bool secondJumpAvail = false;

if (Input.GetButtonDown("Jump") && isGrounded) //so if im on the ground
{

myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
jumpSound.Play();
secondJumpAvail = true;  //jump and then set my double jump to true!
Instantiate(burst2, gameObject.transform.position, burst.rotation);
}

if (Input.GetButton("Jump") && !isGrounded) // then if i am jumping and im NOT on the ground do this here
{
secondJumpAvail = true;

if (secondJumpAvail) // if my second jump is avail then do this in the curlys
{

myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);

Debug.Log("This Works");

}

else
secondJumpAvail = false;

Debug.Log("This Also Works");

}
``````

@KUFgoddess

Here is a 2D Double Jump Script:

``````    public float jumpForce = 100;
public KeyCode key = KeyCode.Space;
bool grounded;
bool canDoubleJump;
Rigidbody2D rb;

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

private void Update()
{
if (Input.GetKeyDown(key))
{
if (grounded)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
canDoubleJump = true;
Debug.Log("Jump1: Working!");
}

else
{
if (canDoubleJump)
{
canDoubleJump = false;
rb.velocity = new Vector2(rb.velocity.x, 0);
Debug.Log("Jump2: Working!");
}
}
}
}

private void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.tag == "Floor")
{
grounded = true;
}
}

private void OnCollisionExit2D(Collision2D other)
{
if (other.transform.tag == "Floor")
{
grounded = false;
}
}
``````

And A 3D Double Jump Script:

``````public float jumpForce = 100;
public KeyCode key = KeyCode.Space;
bool grounded;
bool canDoubleJump;
Rigidbody rb;

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

private void Update()
{
if (Input.GetKeyDown(key))
{
if (grounded)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
canDoubleJump = true;
Debug.Log("Jump1: Working!");
}

else
{
if (canDoubleJump)
{
canDoubleJump = false;
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
Debug.Log("Jump2: Working!");
}
}
}
}

private void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Floor")
{
grounded = true;
}
}

private void OnCollisionExit(Collision other)
{
if (other.transform.tag == "Floor")
{
grounded = false;
}
}
``````