- Home /
Time.timeScale equal to zero does not freezes my scene.
Hello Friends,
In my survival game the health bar value of the player continuously reduces with time. In the pause menu I have used Time.timescale = 0 to pause the game and creating a panel. The problem is, the panel is created but the health bar value in the background still reduces. This is a serious drawback for my game. Please help me with this. I will attach the script as well as the health bar so that you get a clear view of what's happening.
Script: void Start () { PausePanel = GameObject.Find ("PanelForPause"); PausePanel.gameObject.SetActive (false); }
public void GamePaused()
{
Time.timeScale = 0.0f;
PausePanel.gameObject.SetActive (true);
}
Read the timescale page of the scripting api. You missed something.
Is the function GamePaused() called every frame? That may be your problem. Try using a bool.
No, it is called only if pause button is pressed. I haven't tried with bool.
Didnt read it? Ill post it here in case you missed my comment.
The scale at which the time is passing. This can be used for slow motion effects.
When timeScale is 1.0 the time is passing as fast as realtime. When timeScale is 0.5 the time is passing 2x slower than realtime.
When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.
Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class.
If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the same amount.
FixedUpdate functions will not be called when timeScale is set to zero.
using UnityEngine; using System.Collections;
public class PanelScript : $$anonymous$$onoBehaviour { public GameObject PausePanel; private GameObject SuccessPanel; bool isPaused = false; // Use this for initialization void Start () { PausePanel = GameObject.Find ("PanelForPause"); PausePanel.gameObject.SetActive (false);
// SuccessPanel = GameObject.Find ("PanelForSuccess"); // SuccessPanel.gameObject.SetActive (false); }
public void GamePaused()
{
isPaused = true;
PausePanel.gameObject.SetActive (true);
Debug.Log ("Clicked Pause button");
}
public void GameResumed()
{
isPaused = false;
PausePanel.gameObject.SetActive (false);
}
void Update()
{
if (isPaused == true)
Time.timeScale = 0;
else if (isPaused == false)
Time.timeScale = 1;
}
Answer by Bunny83 · Oct 18, 2015 at 05:36 PM
Like other have said setting Time.timeScale to 0 will do exactly those things:
Time.time will stop and not increase any more.
Time.deltaTime will return 0
FixedUpdate is no longer being called.
Everything based on Time.time or Time.deltaTime will also freeze (for example WaitForSeconds in a coroutine)
Everything else will keep running. So if something still move / changes, you most likely do it wrong. If you used Time.deltaTime whereever you change something over time, that process should stop since Time.deltaTime will return 0.
You haven't shown how your health bar is reduced. However if it still reduces when Time.timeScale is 0 you clearly doing it wrong.
public Scrollbar HealthBar; public float health = 100; private Animator animator; private GameObject Player;
void Start ()
{
animator = GetComponent<Animator> ();
Player = GameObject.Find ("player");
// PanelScript Script = gameObject.GetComponent(); // Script.Success$$anonymous$$essage (); }
// Update is called once per frame
void Update ()
{
health -= 0.030f;
HealthBar.size = health/ 100f;
. . . . . .!
}alt text
Well, as i thought. What you do here is simply wrong ^^. You are reducing your health by 0.03 every "frame". That way how fast your health is dropping depends on your frame rate.
At an assumed frame rate of 60 fps (quite usual with vsync on and a 60Hz monitor) you reduce your health by about 1.8 health per second. However if someone doesn't have vsync on and he gets something like 400fps then he would loose about 12 health a second. Likewise someone with a crappy PC that only get about 20 fps will live 3 times longer than the guy with 60fps or about 20 times longer than the 400 fps guy.
To keep things frame independent you should multiply your values by Time.deltaTime. That way you can directly work with "units" per second:
health -= 2f * Time.deltaTime; // reduces by 2 hp per second
As mentioned Time.deltaTime will return 0 when timeScale is 0. So the line will still be executed but nothing will change.
Latest script he posted was added above to the comments.
Would you $$anonymous$$d telling me how I would freeze my AI and player $$anonymous$$ovement, since they are not based of Time.deltaTime? Also, I need the answer to this soon, since I’m doing the project requiring this for Ludlum Dare
my multiplying their move rates by Time.deltatime...?
Your answer
Follow this Question
Related Questions
Why is may costom yield not working? 2 Answers
Slow Mo effect with Time.timeScale? Dont understand Lerp and Corutines... 1 Answer
Changing Time.fixedDeltaTime to create slowmotion messes up with physics. 1 Answer
Slowing down time except for player. 0 Answers
create a pause state and menu that still allows animations 0 Answers