Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by Sandr0G · Jul 20, 2012 at 01:53 PM · gamemenupausestop

Pause Menu

I have this script that pauses the game, but I can still look around and shoot. I am using the First Person Controller.

Pause menu code:

 var gamePaused : boolean = false;
 
 function Update () {
 
  if(Input.GetKeyDown(KeyCode.Escape)){
  if(gamePaused){
 
  Time.timeScale=1.0;
  gamePaused = false;
 
  }else{
 
  Time.timeScale = 0.0;
  gamePaused = true;
  }
  }
 
 }
 
 function OnGUI(){
 
  if(gamePaused){
  if(GUI.Button(Rect (450, 100, 100, 30), "Resume")){
  Time.timeScale = 1.0;
  gamePaused = false;
  }
  if(GUI.Button(Rect(450, 140, 100, 30), "Main Menu")){
  Application.LoadLevel("Menu");
  }
  if(GUI.Button(Rect(450, 180, 100, 30), "Options")){
  }
  if(GUI.Button(Rect(450, 220, 100, 30), "Quit")){
  Application.Quit();
     }
  }
 }

My question is how can I completely pause the game. Stop looking around and stop shooting.

Thank you.

Comment
Add comment
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

3 Replies

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

Answer by kaaJ · Jul 20, 2012 at 02:28 PM

Hi there,

Well, Time.timescale only affects time-based functions.

Simplest solution:

Since Time.timeScale is a global var, add this to your control scripts:

 if (Time.timeScale != 0){
 
 }

Or: (EDIT: worse, because with FixedUpdate() you could miss some inputs)

Include the controls in a time-based function like fixedUpdate() or Update() with Time.deltaTime

C#

 void FixedUpdate(){
   if (Input.GetKeyDown("space")){
     Shoot();
   }
 }

Hope this helps...

kaaJ

EDIT: Just as Fafase said, it's better just to use Time.deltaTime so that it becomes time-dependent. (btw sometimes you need you controls to behave the same way while in slow motion... So I don't think it's as "useless" as Fafase said.)

Comment
Add comment · Show 9 · 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 Sandr0G · Jul 20, 2012 at 03:32 PM 0
Share

I add that to what script? The First Person Controller or $$anonymous$$ouseLook?

avatar image kaaJ · Jul 20, 2012 at 04:29 PM 0
Share

Well, in the $$anonymous$$ouseLook script you can probable add these lines in the Update function

void Update(){

if (Time.timeScale != 0){ ...original blahblah... }

}

avatar image Sandr0G · Jul 20, 2012 at 05:25 PM 0
Share

It didn't worked. I can still look around. What am I doing wrong?

avatar image kaaJ · Jul 20, 2012 at 05:29 PM 0
Share

can you post the $$anonymous$$ouseLook code?

avatar image Bunny83 · Jul 21, 2012 at 11:58 AM 3
Share

"Include the controls in a time-based function like fixedUpdate"

FixedUpdate is only ment for physics based calculations. Never use event functions like Input.Get$$anonymous$$eyDown in FixedUpdate. It will probably fail. FixedUpdate is not necessarily called each frame, so a $$anonymous$$eydown can be missed totally or even processed multiple times.

The usual approach is to disable scripts that should be deactivated.

Show more comments
avatar image
2

Answer by kaaJ · Jul 20, 2012 at 05:47 PM

Try this update function in your MouseLook Script

 void Update () { 
     if (Time.timeScale != 0){
         if (axes == RotationAxes.MouseXAndY) { 
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); 
         }
         else if (axes == RotationAxes.MouseX) { 
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0); 
         }
         else {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0); 
         }
     }
 }
Comment
Add comment · Show 6 · 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 Sandr0G · Jul 20, 2012 at 05:53 PM 0
Share

Thank you man! It worked!

avatar image kaaJ · Jul 20, 2012 at 05:56 PM 0
Share

Np, plz put a thumbs up on my first answer so other people can quickly find the solution Grtz, kaaJ

avatar image Sandr0G · Jul 20, 2012 at 06:43 PM 0
Share

Done, thanks again.

avatar image fafase · Jul 20, 2012 at 07:51 PM 0
Share

This is simply useless!!! Why would alter Time.timeScale if you simply need a boolean variable? Your

 if (Time.timeScale != 0)

is just as good as

 if(anyBoolean)

now the difference is the amount of cycles required for your version and the simple version.

if you do not have any Time.deltaTime or FixedUpdate, do not bother using Time.timeScale.

Also, what is shown above and to which you gave a +1 is only stopping this script but all others will continue until you place the if statement. That involves you have to add an if statement that is checked on every single script on every single frame.

If you add to all inputs and movements a Time.deltaTime, you make your game machine independant, that means your game will run the same speed on any computer. At the moment, your game will walk on a Pentium 4 and run on a I7. Then, when setting Time.timeScale to 0, nothing will move (GUI is still activated though) until set back to 1.

Trust me the solution above is not fixing your issue in the most efficient and easiest way.

avatar image Sandr0G · Jul 20, 2012 at 08:15 PM 0
Share

So how can I implement what are you saying?

Show more comments
avatar image
0

Answer by tw1st3d · Jul 21, 2012 at 03:11 PM

Here's my Pause Menu Script, feel free to use it, just make sure you replace any GUI Textures with your own.

 import System;
     import System.Collections;
     import System.Xml;
     import System.Xml.Serialization;
     import System.IO;
     import System.Text;
     
     class TempData
     {
         var x : float;
         var y : float;
         var z : float;
         var name : String;
     }
     
      class UsernameData
      {
        public var _iUser : TempData = new TempData();
        function UsernameData() { }
     }
     private var _Save : Rect;
     private var _Load : Rect;
     private var _SaveMSG : Rect;
     private var _LoadMSG : Rect;
     private var _FileLocation : String;
     private var _FileName : String = "SaveData.xml";
     
     //#
     var Play_Button : Texture;
     var Load_Button : Texture;
     var Save_Button : Texture;
     var Quit_Button : Texture;
     
     var visible;
     var windowRect;
     var controlTexture : Texture2D;
     var PauseGame;
     var Resume;
     var ShowMenu;
     var StopLook;
     public var PlayVid : MovieTexture;
     var isPlay;
     //#
     
     var _Player : GameObject;
     var _PlayerName : String = "Joe Schmoe";
     
     private var myData : UsernameData;
     private var _data : String;
     
     private var VPosition : Vector3;
     
     function Awake () { 
           _FileLocation=Application.dataPath;
           myData=new UsernameData();
        }
     
     
     
     function Start()
     {
       visible = false;
       Time.timeScale = 1;
       Resume = false;
       StopLook = GetComponent("MouseLook");
       StopLook.enabled = true;
       Screen.showCursor = false;
       PlayVid.loop = false;
       isPlay = false;
     }
     function OnGUI()
     {
      if(visible == true){
      if(Time.timeScale == 0){
        if(isPlay == true){
        PlayVid.loop = true;
        PlayVid.Play();
          GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), PlayVid);
          Screen.showCursor = true;
          }
          }
        }
        if(visible == true){
        if(ShowMenu == true){
        if (GUI.Button (Rect (790,70,350,50), GUIContent("", Play_Button))){
        PlayVid.Stop();
      Resume = true;
      }
      }
      }
      if(visible == true){
        if(ShowMenu == true){
        if (GUI.Button (Rect (790,203,350,50), GUIContent("", Load_Button))){
        GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
            LoadXML();
            if(_data.ToString() != ""){
              myData = DeserializeObject(_data);
               VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);             
               _Player.transform.position=VPosition;
                  Debug.Log(myData._iUser.name);
                  Start();
           }
      }
      }
      }
      if(visible == true){
        if(ShowMenu == true){
        if (GUI.Button (Rect (790,335,350,50), GUIContent("", Save_Button))){
                  
            GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
           
           myData._iUser.x = _Player.transform.position.x;
          myData._iUser.y = _Player.transform.position.y;
           myData._iUser.z = _Player.transform.position.z;
           myData._iUser.name = _PlayerName;
           _data = SerializeObject(myData);
           CreateXML();
           Debug.Log(_data);
           
           Start();
      }
      }
      }
      if(visible == true){
      if(ShowMenu == true){
      if (GUI.Button (Rect (790,467,350,50), GUIContent("", Quit_Button))){
      Application.Quit();
      }
      }
      }
     }
     function Update()
     {
      if(Input.GetKeyDown(KeyCode.Escape)){
        if(Time.timeScale == 1){
        if(visible == false){
        visible = true;
        Time.timeScale = 0;
        ShowMenu = true;
        StopLook.enabled = false;
        isPlay = true;
        }
        }
        }if(Resume == true){
        if(Time.timeScale == 0){
        if(visible == true){
        StopLook.enabled = true;
      Start();
          }
        }
        }
     }
     function UTF8ByteArrayToString(characters : byte[] )
     {     
        var encoding : UTF8Encoding  = new UTF8Encoding();
        var constructedString : String  = encoding.GetString(characters);
        return (constructedString);
     }
     
     function StringToUTF8ByteArray(pXmlString : String)
     {
        var encoding : UTF8Encoding  = new UTF8Encoding();
        var byteArray : byte[]  = encoding.GetBytes(pXmlString);
        return byteArray;
     }
     function SerializeObject(pObject : Object)
     {
        var XmlizedString : String  = null;
        var memoryStream : MemoryStream  = new MemoryStream();
        var xs : XmlSerializer = new XmlSerializer(typeof(UsernameData));
        var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, pObject);
        memoryStream = xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
     }
     function DeserializeObject(pXmlizedString : String)   
     {
        var xs : XmlSerializer  = new XmlSerializer(typeof(UsernameData));
        var memoryStream : MemoryStream  = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
        var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
        return xs.Deserialize(memoryStream);
     }
     function CreateXML()
     {
        var writer : StreamWriter;
        var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
        if(!t.Exists)
        {
           writer = t.CreateText();
        }
        else
        {
           t.Delete();
           writer = t.CreateText();
        }
        writer.Write(_data);
        writer.Close();
        Debug.Log("File written.");
     }
        
     function LoadXML()
     {
        var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
        var _info : String = r.ReadToEnd();
        r.Close();
        _data=_info;
        Debug.Log("File Read");
     }
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 Vora182 · Oct 22, 2013 at 03:51 AM 0
Share

@tw1st3d

when i import your script, I've got an error, it something on the GetComponent. How can i solve this? Im just a newbie. thanks

avatar image SquidSquad · Oct 22, 2013 at 04:22 AM 0
Share

you need to add "StopLook = GetComponent("$$anonymous$$ouseLook");"

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

10 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

Related Questions

In Game Pause Menu Keydown Issue 1 Answer

Pause Button Menu loader 2 Answers

Pause Menu Audio 1 Answer

Pause menu... Isn't pausing everything... 1 Answer

How to save players position then load a level (pause menu) 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