- Home /
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.
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.)
I add that to what script? The First Person Controller or $$anonymous$$ouseLook?
Well, in the $$anonymous$$ouseLook script you can probable add these lines in the Update function
void Update(){
if (Time.timeScale != 0){ ...original blahblah... }
}
It didn't worked. I can still look around. What am I doing wrong?
"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.
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);
}
}
}
Np, plz put a thumbs up on my first answer so other people can quickly find the solution Grtz, kaaJ
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.
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");
}
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
you need to add "StopLook = GetComponent("$$anonymous$$ouseLook");"
Your answer
Follow this Question
Related Questions
In Game Pause Menu Keydown Issue 1 Answer
Pause Button Menu loader 2 Answers
Pause Menu Audio 1 Answer
hi i just got the ultimate fps camera and it is not compatible with pausemenus 0 Answers
Restart Problem 1 Answer