Unity: Animated menus when the game is paused

You can pause your game by setting Time.timeScale = 0.0;
If you set it to zero, Time.time will no longer increase and the FixedUpdate function will no longer be called. In principle this is great. It is not so great though if you want to have an animated menu on top of your paused game because…

yield new WaitForSeconds(x); will no longer work,
and Mathf.SmoothDamp will also stop because it relies on Time.deltaTime

But there’s a way around this:

1) Create your own version of WaitForSeconds that works with realtime instead of gametime:


static function WaitForSecRealtime(sec : float) {
var waitTime : float = Time.realtimeSinceStartup + sec;
while (Time.realtimeSinceStartup < waitTime) {
yield; //wait until next frame

You can now call this from any script by doing something like this:

yield StartCoroutine(Wait.WaitForSecRealtime(2.0));

2) Calculate a realtime deltaTime and feed it into Mathf.SmoothDamp instead of the default Time.deltaTime

xPos = Mathf.SmoothDamp(xPos, targetPos, xVel, sec, Mathf.Infinity, realtimeDeltaTime);

Thanks a lot to Digitalos, ducket, NCarter and ToreTank on IRC #unity3d for helping me with this!

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>