- Home /
Can you change time scale to zero but still have GUI textures changing?
The player runs into colliders which causes the game to freeze. However the health bar is supposed to be constantly decreasing. So when the player hits the colliders that pauses the game, the health bar is also frozen. Is there a way to freeze everything besides the health bar?
Thanks
Answer by robertbu · Jun 12, 2013 at 05:40 AM
I assume your health bar is time based. The reason it stops is because Time.deltaTime is 0 and therefore you are not subtracting anything from your calculations. One solution is to make a copy of the deltaTime for one frame and assign it to a variable in Start(). You can then use it when timeScale is 0 Something like;
if (Time.timeScale > 0.01)
health -= Time.deltaTime * rate;
else
health -= myDeltaTime * rate;
myDeltaTime is assigned Time.deltaTime in Start(). Since this code only samples a single frame, the calculations using it may be a bit slow or fast, but generally it is close.
Answer by TonyLi · Jun 12, 2013 at 02:14 PM
Wrap Time in a singleton class (maybe called GameTime) where you can control different time scales -- maybe one for gameplay, one for your HUD (e.g., health bar), and one for the GUI. For example, you could call GameTime.gameplayDeltaTime for your player, but GameTime.hudDeltaTime for your health bar updates.
Or just base your health bar on Time.realtimeSinceStartup instead of deltaTime. But the singleton wrapper is a much more flexible and robust solution.
Your answer
Follow this Question
Related Questions
Change timescale to zero but still have GUI textures moving 1 Answer
Help with destroying guiRect? 0 Answers
Make more buttons appear, on button click. 1 Answer
scaling behaviour of the standard box texture? 0 Answers
Blurry Texture Help 5 Answers