Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to understand and create fly/ jump cooldown rate

Discussion in 'Scripting' started by KUFgoddess, Feb 2, 2018.

  1. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    27
    In my project I have it set where you hold a to fly around and one thing i've been researching is a cooldown rate but I cannot seem to understand how to limit apply it to my code.

    Maybe I need to start from the top here is what I came up with for flying now I would like to make the player STOP flying after holding the jump button for 2 or 3 seconds.(So they cannot just fly all of the place)

    Code (CSharp):
    1. if (Time.time >= timestamp2 && (Input.GetButton("Jump") && isGrounded)) //so if im on the ground
    2.             {
    3.  
    4.                 myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    5.                 jumpSound.Play();
    6.                 secondJumpAvail = true;  //jump and then set my double jump/fly to true!
    7.                 Instantiate(burst2, gameObject.transform.position, burst.rotation); //if i am on the ground
    8.             }
    9.             if (Time.time >= timestamp2 && (Input.GetButton("Jump") && !isGrounded))// then if i am jumping and im NOT on the ground do this here
    10.             {
    11.                 StartCoroutine("CanJump");
    12.                 secondJumpAvail = true;
    13.                 if (secondJumpAvail) // if my second jump is avail then do this in the curlys
    14.                 {
    15.                     myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    16.                     if (transform.localPosition.y > 5)
    17.                         secondJumpAvail = false;
    18.                     timestamp2 = Time.time + timeBetweenShots2;
    19.                 }
    20.                 else
    21.                     secondJumpAvail = false;
    22.             }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Basically what you want to do is have a float that is the accumulated time when you are flying.

    When you land you would reset that float to zero.

    While you are holding FLY and flying, you would increment that float (each frame in an Update() function) by the contents of Time.deltaTime.

    When that float value is greater than whatever you choose as a limit on flying, then you force the FLY input to be zero, even if the player is holding a button down.

    Then when the player lands, reset that timer and they can fly again.

    If you want a cooldown that keeps them on the ground BETWEEN flights, then you need another similiar count-up-timer variable that you count up until the user has "rested" long enough on the ground to reset his flight time.
     
    KUFgoddess likes this.
  3. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    27

    Yes exactly I would like a cooldown that keeps them on the ground between flights its like they jump then hold to fly then they need start falling down because the timer should reach zero. Ive been toying at it for days but i still cannot get this to work properly. I am still new but I've been doing a lot of research even on the other posts about cooldown rates and I think im just not understanding where to trigger the cooldown rate.
    Let me try to clarify
    ok the Player is on the ground
    if the player is on the ground then they can jump once but if they are not on the ground and hold the jump button it triggers the cooldown timer while they're holding that jump button then it returns then back to the ground and restarts the timer once they hold jump while in the air again.

    2 visual mechanics to think about would be angry birds or Kirbys jumping mechanic thats basically what im trying to do :confused:
    I changed my code to appear more readable as well.




    Code (CSharp):
    1.  
    2.  
    3. void Update() {
    4.         if (timer >= 0f)
    5.         {
    6.             timer -= Time.deltaTime;
    7.         }
    8.         if (timer < 0f)
    9.         {
    10.             if (Input.GetButtonDown("Jump") && isGrounded)
    11.             {
    12.                 myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    13.             }
    14.             //JUMPING MECHANIC WITH COOLDOWN ON FLY
    15.             //if(!isGrounded)
    16.             if (Input.GetButton("Jump") && !isGrounded) //so if im NOT on the ground and i hold jump i should fly
    17.                 {
    18.                     FlyMechanic();
    19.                     timer -= Time.deltaTime;
    20.                 myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    21.                     timer = jumpCooldown;
    22.                    
    23.             }
    24.            
    25.         }
    26.  
    27.     void FlyMechanic()
    28.     {
    29.        
    30.        
    31.         timer -= Time.deltaTime;
    32.         myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed /*+ 5*/, 0f);
    33.         Debug.Log("YouAreFlying");
    34.         StartCoroutine("Disabler");
    35.     }
    36.     IEnumerator Disabler()
    37.     {
    38.         yield return new  WaitForSeconds(1);
    39.         myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, 0f, 0f);
    40.         jumpCooldown = 1f;
    41.         StopCoroutine("Disabler");
    42.     }
    43.  
     
    Last edited: Feb 6, 2018