- Home /
A Proper Way To Pause A Game
Sorry i am a Beginner at Unity GUI and Time Scripting Stuff
I Want to Know How to Properly Pause the game in 3d
I dont want to use Time.timescale stuff Because most of my Game is in
FixedUpdate() And my Animated GUI Doesnt Work Nice with my Game
And If Your going to give scripts Id Prefer Unity Javascript More than C Sharp
C Sharp is OK If you cant provide Unity JS
I just want to know a technique
Or If you know Something else useful for this please inform me
My pause Script Is Something ike this
#pragma strict
public var pauseKey : String;
private var GamePaused : boolean;
function FixedUpdate(){
//Dostuff
if(Input.GetKey(pauseKey) ){
if(GamePaused){
//Activate GUI
//Pause Game with Time.timescale=0; stuff
}
else{
//Play Game with time.timescale=1; stuff
//Deactivate GUI
}
}
}
Im not allowed to share the real script
Plz help me quick
Other Details:
I use Unity 5 Personal edition
I use InstantGUI Asset for GUI But I can also work with custom GUI
My Scene Has 3 cameras
1) Game Screen
2) radar
3) Additional Information
My Scene has a terrain
My scene contains Custom Meshes From Blender
I Have More than 15 Javascript scripts
My scene contains Changing skyboxes Via scripts
My scene has over a 1000 characters controlled by one "Operator" script
I also have 2048*2048 tiling textures made in GIMP
I have Normal Maps Made with CrazyBump
I want to publish my Game to Non-Touch devices like PC,Mac,Linux,Xbox
I dont think you need any more details do you?
One line code:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) Time.timeScale = 1 - Time.timeScale;
Answer by CausticLasagne · Aug 16, 2016 at 10:03 PM
Usually whenever I pause the game, I freeze the player's rigidbody and disable any input other than that required for the menu. I can't give you any explicit 'How to pause your game' code, because each game is different. Just freeze everything that moves and stop the player from controlling the character.
CausticLasagne
Answer by danivdwerf · Aug 18, 2016 at 09:46 AM
He, I know your question has been answered already, but here is some sample code :)
this uses the panel object, it's really easy this way
public class Pause : MonoBehaviour
{
[SerializeField] private GameObject pausePanel;
void Start()
{
pausePanel.SetActive(false);
}
void Update()
{
if(Input.GetKeyDown (KeyCode.Escape))
{
if (!pausePanel.activeInHierarchy)
{
PauseGame();
}
if (pausePanel.activeInHierarchy)
{
ContinueGame();
}
}
}
private void PauseGame()
{
Time.timeScale = 0;
pausePanel.SetActive(true);
//Disable scripts that still work while timescale is set to 0
}
private void ContinueGame()
{
Time.timeScale = 1;
pausePanel.SetActive(false);
//enable the scripts again
}
}
Good Luck
I didn't need any animation in my pause menu so this worked perfectly for me. Thanks!
This code don't work, you need to add a 'else' line 17, otherwise the game pause then immediatly unpause
This code snippet uses Time.timeScale even though OsmiousH explicitly said: "I don't want to use Time.timescale stuff Because most of my Game is in FixedUpdate".
Answer by ScaniX · Aug 17, 2016 at 12:38 PM
I am using Time.timeScale to pause the game as well. I have too many objects doing some sort of animation or rigidbodies jumping around in order to freeze them all and reset them to their old velocity afterwards. I would have to check for pause state in every single script. :(
Timescale works perfectly for this. My animated pause menu is just using Time.unscaledTime.
I agree. You can also set this from the Inspector for your Animators.
Answer by OsmiousH · Aug 18, 2016 at 09:15 AM
I think I understand
first, i need to disable physics
next I need to Stop checking Input
//javascript
if(!paused){
if(input.GetKey("up")){
moveForward();
}
}
then I need to stop my players reactions just in case
//javascript
function moveForward(){
if(!paused){
transform.Translate(Vector3.forward*SpeedMultiplier);
}
}
finally I start the GUI
//javascript
if(paused){
//enable GUI
}
Anyways If you want to use these scripts then I license them CC-0!
You should probably add this as a comment ins$$anonymous$$d. You will find plenty of good script examples here, especially for movement. A movement function should always take something like Time.deltaTime into account to make the speed independent from the framerate.
It's probably way better to turn off the movement script.
(I don't know the javascript syntax, but i imagine it's cloase to C#)
private SCRIPTNA$$anonymous$$E VARNA$$anonymous$$E
private void Start()
{
VARNA$$anonymous$$E = GameObject.FindObjectOfType();
}
private void Pause()
{
VARNA$$anonymous$$$$anonymous$$enabled = false;
}
Answer by brotar · Aug 09, 2017 at 09:21 AM
I think @CausticLasagne is right. Setting time scale as 0 is not a good idea if you want to implement UI animation like cool down.