Proper way to pause game, but not everything.

Time.timeScale = 0 pauses all animation in my game, which appears to be the quick an easy way to stop everything while, for instance, the player examines his inventory window.

However, this doesn’t allow me to use animations in this UI menu, is there a way to keep some animations working, but not others?

You can wrap the built-in time class in your own class, of which you create multiple instances. The instances are used for their very own contexts.

You can pass those instances around, and the various contexts in your game can use a different, or a temporary, time service.

That is, you no longer use Time.deltaTime directly, but an instance of your time service class which is controlled individually and provides Time.deltaTime * it’s current factor.

This allows to speed up / slow down / pause individual objects, groups of objects or whatever you like. Note that some systems of the engine use the built-in time internally. You’d then need to stop animations on your own or add additional timing variables to achieve the effect of modifying the global time scale.

There’re also assets on the store that you can use if you don’t want to re-invent the wheel. Not sure if the engine’s newer versions have support built-in for that. It’s definitely something that is required quite often.

4 Likes

Oh wow i didnt know you can do that, i gotta experiment with this.

I hope the docs are actually written for that functionallity, heh

You have Time.deltaTime and Time.unscaledDeltaTime. One is effected by timeScale, the other not:

If you just have 2 modes… “effected by pause” and “unaffected by pause”, this is all you’ll need.

If you need more than just these 2, as @Suddoha mentioned, you can create your own time wrapper. Here is mine:

First I have an ‘ITimeSupplier’ interface that defines the functionality (and allows for adhoc implementations of very custom time suppliers):

Then I have SPTime, which is where I house an access point to all the general time suppliers (normal, unscaled, fixed, etc):

And CustomTimeSupplier which is a general purpose time supplier which allows creating tagged time suppliers of various types:

Furthermore, the ‘SPTime’ acts as a token for time suppliers. Using a PropertyDrawer for SPTime like this one:

I get the ability to do this:

With a bit of work, there’s a lot you can do with giving Time object identity like this.

4 Likes

If you are using an Animator to animate your UI there is a setting that uses unscaled time.
4504867--416200--AnimatorInspector.png

4 Likes

Thanks for the very interesting replies.