- Home /
 
 
               Question by 
               KUFgoddess · Jan 14, 2018 at 01:46 PM · 
                2d-platformerflyingtimer-script  
              
 
              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");
 
 
 
               
 
 
             }
 
              
               Comment
              
 
               
              Answer by KittenSnipes · Jan 15, 2018 at 01:37 AM
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);
                 rb.AddForce(new Vector2(0, jumpForce));
                 canDoubleJump = true;
                 Debug.Log("Jump1: Working!");
             }
 
             else
             {
                 if (canDoubleJump)
                 {
                     canDoubleJump = false;
                     rb.velocity = new Vector2(rb.velocity.x, 0);
                     rb.AddForce(new Vector2(0, jumpForce));
                     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);
             rb.AddForce(new Vector3(0, jumpForce, 0));
             canDoubleJump = true;
             Debug.Log("Jump1: Working!");
         }
         else
         {
             if (canDoubleJump)
             {
                 canDoubleJump = false;
                 rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
                 rb.AddForce(new Vector2(0, jumpForce));
                 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;
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Timer jitters? SOLVED 0 Answers
Object moves to -infinite... help! 2 Answers
How to I get the timer counter to restart when the player hits the ground and respawns? 2 Answers
Make An Enemy Rotate Around A Player 2 Answers
No response from raycast 0 Answers