So I’m trying to implement power-ups in my game that last for a certain period of time.
One of them is a slow-mo, which I want to last for 20 seconds, not including time while paused.
I had a coroutine working yesterday but all of a sudden it’s not working. Here’s the code for the coroutine itself:
IEnumerator slow_mo_powerup(){
Debug.Log ("start"); //coroutine
Time.timeScale = slowMoScale; //currently 0.5f
float start = Time.realtimeSinceStartup; //this is timeScale independent
float timer = start;
while (timer < start + 20.0f) { //count 20 seconds
if (Time.timeScale != 0){ //don't count if paused
timer = Time.realtimeSinceStartup;
}
Debug.Log (timer - start);
yield return 0;
}
Time.timeScale = 1.0f;
Debug.Log ("end");
}
I call StartCoroutine in a different part of the script. Now, I have tried running this many times and each time, it starts and slows it down, logs “start”. But then it only goes through the loop once, logs a very small decimal for (timer - start), and then somehow exits without going through the loop again OR completing the last two lines. It never logs “end” nor speeds back up.
Any ideas for why this isn’t working??
Thanks