- Home /
 
How to create a framerate independent time delay in Update() without coroutines
I'm creating a turn based board game and trying to create a small pause between the cpu and human player. All my logic is in the Update() function so I can't use coroutines to simulate the pause so I created a method to try delay the time.
 void miniPause ()
     {
         maxPauseTime = Time.realtimeSinceStartup + 2.5f;
         while (Time.realtimeSinceStartup < maxPauseTime) {
             /*
              *Empty loop to create a time delay
              *between the Player && CPU turns
              */
             print ("Time  = " + Time.realtimeSinceStartup);
         }//end loop
     }
 void Update(){
    if(cpu.turn){
    
     miniPause();
     
     //instantiate cpu's piece on the board
      humanPlayer.turn = true;
      cpu.turn = false
    }
    if(humanPlayer.turn){   
       if(Input.Input.GetMouseButtonDown (0)){
         
         //Instantiate human player's piece on the board here
            humanPlayer.turn = false;
            cpu.turn = true
         }
    }
 }
 
               The delay is created alright, problem is everything else pauses as well, like the the board piece stays suspended in mid-air until delay elapses then falls to the board with the cpu's piece.
Is there a way to halt the cpu player without stopping everything else like navmesh motion, particle effects.
You have a loop in the update, that blocks the whole update loop until 2.5s. Thats why your program freeze.
You could make a something like this:
 bool wait = false;
 
 
 void Update() {
 
    if(!pause) {
 
       /*GA$$anonymous$$E LOGIC*/
 
       if([PLAYER ENDTURN]) {
          pause = true;
          StartCoroutine(Count());
 }
 
 Ienumerator Count() {
   yield return new WaitforSeconds(2.5f);
   pause = false;
 }
                 I've been trying the subroutine way but it didn't work because of my approach. I wasn't forcing the cpu to stop I was just running the subroutine. That "pause" variable did the trick! Thank you kind sir! ...or maam, it works like a charm now.
Oops, I messed up the variable name.. Glad it works!
Answer by Larry-Dietz · Dec 13, 2017 at 04:47 PM
try something like this.
In the declarations section at the top`...
 public float CPUMoveTimer;
 
               then in your update ...
  if(cpu.turn){
         
         if (CPUMoveTimer>0){
               CPUMoveTimer-=Time.deltaTime;
         } else {
           //instantiate cpu's piece on the board
             humanPlayer.turn = true;
             cpu.turn = false
         }         
     }
 if(humanPlayer.turn){   
        if(Input.Input.GetMouseButtonDown (0)){
          
          //Instantiate human player's piece on the board here
             humanPlayer.turn = false;
             cpu.turn = true
             CPUMoveTimer=2.5f;
          }
     }
  }
 
               Might be a better way of doing it, but this is what I normally do in this situation, and it works.
Hope it helps, -Larry
Your answer
 
             Follow this Question
Related Questions
Heavy Processing Messes With Time.time 1 Answer
Low FPS on Simple geometry? 4 Answers
Unity 2017 frame rate capped? 2 Answers
Limit framerate on just a camera, not whole application? 2 Answers