- Home /
 
Reverting coroutine effect on input change.
Basicly, what I'm trying to do is to gradually slow down time on input and when I release my input button (In this case spacebar) to gradually return timestep to its normal values (1f). No problems there. However, the problem happens if I release spacebar before while loop ended, I have no idea how would I make it stop and exit and then revert the effect in time timer added up. Lets say transition takes 0.5s to finish transitioning from 1f to 0.2f but I release spacebar after 0.3s passed. Hope you can understand what I'm triyng to say. I'm not asking for code I can copy/paste I would like just a tip on how could I achive this. I tried a lot of things and none seem to work.
Here is code for slowing down time on input I'm using: IEnumerator ScaleTime(float start, float end, float time) { float lastTime = Time.realtimeSinceStartup; float timer = 0.0f;
         while (timer < time) {
 
             Time.timeScale = Mathf.Lerp (start, end, timer / time);
             Time.fixedDeltaTime = 0.02f * Time.timeScale;
             timer += (Time.realtimeSinceStartup - lastTime);
             lastTime = Time.realtimeSinceStartup;
             yield return null;
         }
     }
 
              Your answer