- Home /
Time Slowdown
Hi. Now I have a problem with time slowdown. When I change Time.timeScale
to 0.0f
or to 1.0f
, I have not problems. But, if I want to decrease it gradually, it doesn't work. I have tried several approaches. For example:
//--------------some code--------------------
if (pause)
{
while (Time.timeScale > 0.0f)
{
Time.timeScale = Time.timeScale - 0.1f;
StartCoroutine(Slowdown());
}
}
else {Time.timeScale = 1.0f;}
//--------------some code--------------------
IEnumerator Slowdown()
{
yield return new WaitForSeconds(0.1f);
}
or this:
//--------------some code--------------------
if (pause)
{
for (int i = 1;i<=10;i++)
{
Invoke("Slowdown",0.1f);
}
}
else {Time.timeScale = 1.0f;}
//--------------some code--------------------
void Slowdown()
{
Time.timeScale -= 0.1f; //42nd line
}
But they didn't bring a result. Last approach causes this error:
Time.timeScale is out of range. Needs to be between 0 and 100. UnityEngine.Time:set_timeScale(Single) Pause:Slowdown() (at Assets/Standard Assets/Scripts/Pause.cs:42)
I think timeScale drop below zero. But why? Help me, please.
do you realize when you make time.scale 0 ot literally means Update () function is called 0 times / s
try getting code inside the OnGUI the event call
as OnGUI can be calles more than 10 times per frame or 1 per second.
so each time you change something for OnGUI it'll run through the code.
why it was giving you that error, ... because:
you called
Invoke("Slowdown",0.1f);
several times
propably more than 10 times in 11 time it became -0.1f witch cannot be for time scale
@sdgd - Changing Time.timeScale does not change the rate at which Update() is called. If your program is running at 60fps, then you will still get 60 Update() calls per second even when Time.timeScale is 0. What changes is Time.deltaTime.
@robertbu but afaik Time.deltaTime is how much time passed between 1 update and next update
Answer by robertbu · Oct 24, 2013 at 03:52 PM
There are a number of issues here. When Time.timeScale is set to 0.0, you do continue to get Update() calls, but Time.deltaTime will be 0.0. You will not get FixedUpdate() calls, and any WaitForSeconds() calls are scaled as well, so use of WaitForSeconds() in an IEnumerator will not work correctly. Time.time also stops. There is one value you can still use: Time.realtimeSinceStartup. Here is a coroutine that will scale time. If you want to go from 1.0 down to 0.0 in 3.0 seconds, you would call it:
StartCoroutine(ScaleTime(1.0f, 0.0f, 3.0f);
You call this once, not repeatedly in Update().
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);
timer += (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
yield return null;
}
Time.timeScale = end;
}
Answer by mattssonon · Oct 24, 2013 at 03:01 PM
The while loop does not wait for the WaitForSeconds()
function just because you call StartCoroutine(Slowdown())
inside of it. Put the while loop inside of the IEnumerator instead, i.e. replace this:
if (pause)
{
while (Time.timeScale > 0.0f)
{
Time.timeScale = Time.timeScale - 0.1f;
StartCoroutine(Slowdown());
}
}
with
if (pause)
{
StartCoroutine(Slowdown());
}
IEnumerator Slowdown() {
while (Time.timeScale > 0.0f)
{
Time.timeScale = Time.timeScale - 0.1f;
yield return new WaitForSeconds(0.1f);
}
}
Also, change WaitForSeconds(0.1f);
to something higher, waiting only 0.1 seconds is probably not going to look like a gradual change.
I've tried your approach. But it gives the same error again. First time slows, but than game (not time) stops. I really hope for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Breathing sound effect using timers 1 Answer
Timer running down too quickly 1 Answer
How to create an Idle counter ? 2 Answers
Rewind? *confused* 4 Answers