• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity
×
 

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel
    • Ask a question
    • Spaces
    • Home /
    avatar image
    6
    Question by OsmiousH · Aug 16, 2016 at 09:35 PM · c#javascriptscripting problembeginnerpause

    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

    1. #pragma strict
    2. public var pauseKey : String;
    3. private var GamePaused : boolean;
    4. function FixedUpdate(){
    5. //Dostuff
    6. if(Input.GetKey(pauseKey) ){
    7. if(GamePaused){
    8. //Activate GUI
    9. //Pause Game with Time.timescale=0; stuff
    10. }
    11. else{
    12. //Play Game with time.timescale=1; stuff
    13. //Deactivate GUI
    14. }
    15. }
    16. }

    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?

    Add comment

    7 Replies

    · Add your reply
    • Sort: 
    avatar image
    6
    Best Answer

    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

    Add comment · Hide 1 · Share
    avatar image OsmiousH · Aug 18, 2016 at 12:57 AM 0
    Share

    Thanks for the help

    avatar image
    20

    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

    1. public class Pause : MonoBehaviour
    2. {
    3. [SerializeField] private GameObject pausePanel;
    4. void Start()
    5. {
    6. pausePanel.SetActive(false);
    7. }
    8. void Update()
    9. {
    10. if(Input.GetKeyDown (KeyCode.Escape))
    11. {
    12. if (!pausePanel.activeInHierarchy)
    13. {
    14. PauseGame();
    15. }
    16. if (pausePanel.activeInHierarchy)
    17. {
    18. ContinueGame();
    19. }
    20. }
    21. }
    22. private void PauseGame()
    23. {
    24. Time.timeScale = 0;
    25. pausePanel.SetActive(true);
    26. //Disable scripts that still work while timescale is set to 0
    27. }
    28. private void ContinueGame()
    29. {
    30. Time.timeScale = 1;
    31. pausePanel.SetActive(false);
    32. //enable the scripts again
    33. }
    34. }
    35.  

    Good Luck

    Add comment · Hide 3 · Share
    avatar image RareRooster · Jun 15, 2017 at 10:46 AM 0
    Share

    I didn't need any animation in my pause menu so this worked perfectly for me. Thanks!

    avatar image Milvus · Jun 18, 2017 at 01:42 PM 2
    Share

    This code don't work, you need to add a 'else' line 17, otherwise the game pause then immediatly unpause

    avatar image Bip901 · Jan 04 at 01:54 PM 1
    Share

    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".

    avatar image
    8

    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.

    Add comment · Hide 1 · Share
    avatar image tenshiko · Apr 19, 2018 at 05:16 PM 1
    Share

    I agree. You can also set this from the Inspector for your Animators.

    Set Update Mode to Unscaled Time in Inspector for UI Animators in pause menu

    avatar image
    0

    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

    1. //javascript
    2. if(!paused){
    3. if(input.GetKey("up")){
    4. moveForward();
    5. }
    6. }

    then I need to stop my players reactions just in case

    1. //javascript
    2. function moveForward(){
    3. if(!paused){
    4. transform.Translate(Vector3.forward*SpeedMultiplier);
    5. }
    6. }

    finally I start the GUI

    1. //javascript
    2. if(paused){
    3. //enable GUI
    4. }

    Anyways If you want to use these scripts then I license them CC-0!

    Add comment · Hide 2 · Share
    avatar image ScaniX · Aug 18, 2016 at 09:15 AM 0
    Share

    You should probably add this as a comment instead. 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.

    avatar image danivdwerf · Aug 22, 2016 at 01:15 PM 0
    Share

    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 SCRIPTNAME VARNAME
    private void Start()
    {
        VARNAME = GameObject.FindObjectOfType();
    }
    private void Pause()
    {
        VARNAME.enabled = false;
    }
    
    avatar image
    0

    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.

    Add comment · Share
    • 1
    • 2
    • ›

    Your answer

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    Welcome to Unity Answers

    The best place to ask and answer questions about development with Unity.

    To help users navigate the site we have posted a site navigation guide.

    If you are a new user to Unity Answers, check out our FAQ for more information.

    Make sure to check out our Knowledge Base for commonly asked Unity questions.

    If you are a moderator, see our Moderator Guidelines page.

    We are making improvements to UA, see the list of changes.



    Follow this Question

    Answers Answers and Comments

    16 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    I'm clueless about how to make my script change its position based on how close it is to the wall.. 0 Answers

    Add score from C# to Js 0 Answers

    I want to have a cannon that infinitely fires after a certain number of time but my script crashes unity 1 Answer

    Help change the subject of this Script 1 Answer

    Changing TPP view to a FPP view after picking up object 1 Answer