Reputation: 494
I've a got a weird problem with my pause menu.
When I press ESC, it' opening my menu and stop the "game" except characters.
I'm using Time.TimeScale = 0f;
.
I've seen that's a problem with the TimeScale that affect everything "in the game" and if it's desn't work on my characters, that's because they're not in the same TimeScale.
I'm using UNITY 3D 5.6.0 with Visual Studio 2017.
My Code :
public static bool pause = false;
public GameObject pauseMenu;
void Update () {
if(Input.GetKeyDown(KeyCode.Escape))
{
if(pause)
{
Continuer();
}
else
{
Pause();
}
}
}
public void Continuer()
{
pauseMenu.SetActive(false);
Time.timeScale = 1f;
pause = false;
}
public void Pause()
{
pauseMenu.SetActive(true);
Time.timeScale = 0f;
pause = true;
}
Does anyone know how to pause the entire game ?
Upvotes: 1
Views: 1097
Reputation: 6125
Update()
and OnGUI()
are time-scale independent, so they are not affected by setting Time.timeScale
to 0
. Only FixedUpdate()
is affected from it.
As the Documentation says:
When timeScale is set to zero the game is basically paused if all your functions are frame rate independent. (emphasis by me)
You should use some logic behind your Update()
calls, like these:
1) Let movements be time-scale dependent. If your characters move with translation, do this:
transform.Translate(Vector3.forward * Time.deltaTime);
2) Apply Update()
logic only if the game is not paused:
void Update(){
if (Time.timeScale == 0)
return;
//...
}
or
void Update(){
if (pause)
return;
//...
}
Upvotes: 4