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

Pause WaitForSecond

Discussion in 'Scripting' started by ZeWitcher, Jul 18, 2012.

  1. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    Hello,

    I'm creating a shooting game. The player can hold down a button and it will automatically shoot so I had to put a delay between each shot. First, for information: For pausing I don't change Time.timeScale. What I do is disabling rigidbodies, particles and stuff like that and then I use something like if (paused) in my update functions. For delay between each shot, I use yield WaitForSeconds but the problem is that whenever the player shoots, he/she can pause the game and wait for the yield to finish then he/she can resume the game. How do I pause a WaitForSeconds?
     
    Last edited: Jul 18, 2012
  2. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    you could do a system like this

    Code (csharp):
    1.  
    2. var coolDown : float = 1.0;
    3. var nextShot : float = 0.0;
    4.  
    5. if(Input.GetButtonDown("Fire1")   nextShot <= 0){
    6. nextShot = coolDown ;
    7. //Handle the projectile behavior
    8. nextShot -= Time.time; //or you can use Time.deltaTime here its up to you
    9. }
    10.  
    11.  
    This should work.
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Its my understanding that once you pass the wait for seconds back to Unity you have lost all control. The simplest alternative is to avoid using WaitForSeconds(), and instead maintain your own timer which is only adjusted when not paused.

    Another simple option might be to skip/cancel "pending" shots if the game has been paused.
     
  4. lakshmi.nani

    lakshmi.nani

    Joined:
    Jul 12, 2012
    Posts:
    10
    use this..


    Code (csharp):
    1. yield WaitForSeconds(delaytime);
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Please at least read the original post.
     
  6. lakshmi.nani

    lakshmi.nani

    Joined:
    Jul 12, 2012
    Posts:
    10
    The best option what i think might be disabling the pause option during the shots....
     
  7. lakshmi.nani

    lakshmi.nani

    Joined:
    Jul 12, 2012
    Posts:
    10
    @Johnny Sorry for that post... I have not completed the post and submitted in the middle..
     
  8. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    @lakshmi.nani: Disabling the pause option during the shots might be a good idea, but think about it. What if you have a power attack that takes like 3 mins to recharge? Or maybe a mission that you can fail if you don't finish it before time

    Creating a timer like what kingcharizard and JohnnyA said is a good idea. I could use like an AddTimer() function and store them in an array, then loop thru that array and call the Step() function for each of them every frame

    That fixes my problem, thanks everyone!