Hi everyone, I've been puzzled for some time because I can't seem to pause my game. I've tried using Time.timeScale = 0 in a script but it doesn't pause anything. It's called in OnGUI() (maybe that's the problem) when a Raycast hits a certain object. Can somebody help me with this? Thanks in advance
What in you game is not paused? If you some scripted interaction in your game and the movement uses Time.deltaTime, the game should be paused. If you are not using Time.deltaTime, you will run into issues anyway and you should update your scripts accordingly. The cause is, that the game is not frame rate independent without Time.deltaTime, meaning it runs slower on slow computers and faster on fast computers.
To add more on what Dantus said, there is no way to pause a game in Unity like how you just press a pause button in the editor. You have to design what does a pause state mean to your game. Time.timeScale = 0 is popular because it stop all the basic physics in the game. This won't stop player from accessing or interacting with the game GUI. You must script this yourself, with some kind of flag or state machine. Something to let the game objects know that the game is in a pause state and it shouldn't interact with the player.
I'm moving a gameObject in the update function but not with deltaTime I guess (except if Update is using deltaTime itself). So when I set the Time.TimeScale to 0 it keeps on moving but it should stop moving.
If you aren't using Time.deltaTime, then you have to set some kind of flag so that it knows what not to do when the game is in a pause state. It can be as simple as Code (csharp): void Update() { if( !isPause) { UpdateMovement(); } } void UpdateMovement() { // Movement update related codes should be here this.transform.Translate( 5, 0 , 0 ); }
Even if the code works, some time in the future you are going to run into issues. Think about multiplying the numbers you are using with Time.deltaTime. You have to heavily increase the numbers. As soon as this works, you can be sure, that you get the same result on about every machine, independent of how fast it is. Code (csharp): void Update() { if( !isPause) { UpdateMovement(); } } void UpdateMovement() { // Movement update related codes should be here this.transform.Translate( 300 * Time.deltaTime, 0 , 0 ); }