- Home /
 
2d game needs timer inbetween jumps
My 2d game test to learn C# coding has proved fruitful, except I need a timer inbetween the 3 jumps my player has. link is right here. thanks for anyone fixing it :D
The timer has to have a second between each jump, and the reset of the var "jumptime" needs to take 3 seconds to avoid spacebar spam$$anonymous$$g and jumping to infinity. $$anonymous$$ind you, I'm very new to Unity, having started less than a week ago.
Answer by Zaeran · Dec 22, 2017 at 09:43 AM
2 ways to do this.
The first, with just an update loop:
 float jumpTimer = 0;
 void Update(){
     if(jumpTimer > 0){ //count down the timer
         jumpTimer -= Time.deltaTime;
     }
     else if(jumpTime == 3){ //if the timer is zero and you have jumps, remove them
         jumpTime -= 3;
     }
 }
 
               then you have in your fixedUpdate
 if(jumpTime == 3 && jumpTimer <= 0){ //only set the jump timer it's currently set to zero
     jumpTimer = 3 //replace this with number of seconds
 }
 
               The second way is with a coroutine.
 IEnumerator JumpTime(){
     yield return new WaitForSeconds(3);
     jumpTime -= 3;
 }
 
               which you call from your if(jumpTime == 3) with
 StartCoroutine(JumpTime);
 
               Just make sure you put in a flag that the coroutine is running, otherwise it'll start a whole bunch of them
Your answer
 
             Follow this Question
Related Questions
Instantiation Disables Objects Scripts and Colliders 1 Answer
Trying to change character model on mouse down 1 Answer
help with instantiating gameobject at random postition from a target gameobject? 1 Answer
Recording the button touch and screen touch at same time.... 0 Answers
Need a bit of help implementing something into a code 1 Answer