Hello everyone, recently I saw some tutorials about how to do a pause for your game, in all the tactic was the same, set Time.timescale to zero.
This really works, all in-game for, run Update(), but other updates like FixedUpdate() stop completely, like physics simulation and animations. So, for such a purpose to pause the game, this is incredible, however, I came across the following difficulty: How to do something when you “to” the game? I mean, usually you pause the game to show a menu of settings, and if I want this menu to appear in an animated way, I do not know, changing its alpha from 0 to 1 maybe? I could not do that with animations because they are not reproduced while timescale is zero. I also could not use Coroutine, because they also stop running. So, how would I do to perform some actions even with timescale at zero?
(Sorry for bad english, is google translate
)
unscaledTime and unscaledDeltaTime:
Can’t do much about FixedUpdate.
Coroutines shouldn’t stop, unless you use WaitForFixedUpdate.
Also, the Animator component allows you to use Unscaled Time in its animations - very handy for menu animation. Unity - Manual: Animator component
Personally though I don’t like how there really is only 2 times, and that they have no object identity.
This is why I wrote my own TimeSupplier:
With this interface:
That way I can have various times for various situations… as well as stacking time scales.
Guys, you’re all right, and I was wrong in one thing (or several), doing some tests, I noticed that the Coroutines do not really stop (not under certain conditions). Look at this code:
public bool gamePaused = false;
private void Start()
{
StartCoroutine(CoroutineTest());
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!gamePaused)
{
gamePaused = !gamePaused;
Time.timeScale = 0f;
}
else
{
gamePaused = !gamePaused;
Time.timeScale = 1f;
}
}
updateText.text = "Update: " + Random.Range(1, 10);
}
private void FixedUpdate()
{
fixedUpdateText.text = "FixedUpdate: " + Random.Range(11, 20);
}
private void LateUpdate()
{
lateUpdateText.text = "LateUpdate: " + Random.Range(21, 30);
}
private IEnumerator CoroutineTest()
{
while (true)
{
coroutineText.text = "Coroutine: " + Random.Range(31, 40);
yield return null;
}
}
Testing this code, one thing I noticed is that every time the timeScale was set to zero, Update(), LateUpdate() continued to run. And Coroutines rode under certain circumstances…
As I said before, many things stop with timeScale at zero, however, I was wrong to say that Coroutines also stopped, actually Coroutines only stop if you return a WaitForSeconds (). But if you return null or some other data types do not. So I did some testing and noticed that you can use Coroutines to do things while timeScale is zero.
For Coroutines the following returns will run while timeScale is zero:
yield return null,
yield return new WaitForEndOfFrame(),
yield return new WaitForSecondsRealtime().
Thank you for helping!