Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
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

 #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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image s7887177 · Feb 23, 2020 at 08:09 PM 0
Share

One line code:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) Time.timeScale = 1 - Time.timeScale;

9 Replies

· Add your reply
  • Sort: 
avatar image
10
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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OsmiousH · Aug 18, 2016 at 12:57 AM 0
Share

Thanks for the help

avatar image
28

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
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, 2019 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
9

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
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 $$anonymous$$ode 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

 //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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScaniX · Aug 18, 2016 at 09:15 AM 0
Share

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.

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 SCRIPTNA$$anonymous$$E VARNA$$anonymous$$E
private void Start()
{
    VARNA$$anonymous$$E = GameObject.FindObjectOfType();
}
private void Pause()
{
    VARNA$$anonymous$$$$anonymous$$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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image

Related Questions

State Machine Question 1 Answer

How to move the object to where the object is already pointing to? 1 Answer

NavMeshAgent, goes to the wrong place 0 Answers

how i save that in my data in load or save 2 Answers

Pause a script without pausing the whole game 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges