Hi there, I a new to unity, please helm me out with my problem. I am making a game called underwater Pressure.
How can i pause my game and resume? That everytime i click the pause button the game will pause and there will be a pop up screen shows to resume the game or go back to main menu.
All i want is, everytime you click pause the game will pause and if i click resume, the game will resume where i pause. Please I need your help guys. Thank You So Much
I prefer to use
Time.timeScale = 0;
By setting the timeScaling factor to 0, you basically pause your game, since everything time-based will be stopped. Effectively, it results in Time.deltaTime to be constantly 0, and FixedUpdate is not called anymore. (Update and LateUpdate will still be called).
If you want to have animations during pause, you have two possibilities:
- Use an animator component with Update Mode: Unscaled Time.
- If the animations are done through code (like rotation, simple movement or fading) you can use Time.unscaledDeltaTime. Like the name says, this is the unscaled delta value, and thus the same as deltaTime for timeScale = 1.
If you don’t use a physics or time based game, you don’t need to do anything in special. If your game is purely UI based, you can open a pause panel on click on the pause button. If the pause panel fills out the entire screen (can also be transparent, if you don’t want it to look like that), it blocks every underlying UI input, and your game is effectively paused.
I prefer to use
Time.timeScale = 0;
By setting the timeScaling factor to 0, you basically pause your game, since everything time-based will be stopped. Effectively, it results in Time.deltaTime to be constantly 0, and FixedUpdate is not called anymore. (Update and LateUpdate will still be called).
If you want to have animations during pause, you have two possibilities:
- Use an animator component with Update Mode: Unscaled Time.
- If the animations are done through code (like rotation, simple movement or fading) you can use Time.unscaledDeltaTime. Like the name says, this is the unscaled delta value, and thus the same as deltaTime for timeScale = 1.
If you don’t use a physics or time based game, you don’t need to do anything in special. If your game is purely UI based, you can open a pause panel on click on the pause button. If the pause panel fills out the entire screen (can also be transparent, if you don’t want it to look like that), it blocks every underlying UI input, and your game is effectively paused.
There is no special way to pause the game. You will just need to handle it as a game state no differently than if you are at the menu, mid game, showing the final score. You just need to plan to write your scripts so that they can all be paused. I typically check a global state in a manager script. So I have a Manager script for the entire game which is attached to a persistent GameObject. Then if someone clicks a GUI button for pausing, it issues the callback to the script for that GUI component which tells the manager to change state to Paused. Then all of my scripts check each frame during Update before moving through it. For physics based scripts, see this: Can I pause the physics simulation? - Unity Answers
By the way, this is a common question, so please search the forums and answers.
i was going to ask the same ques but because it is asked here i might as say it here.
i am facing problems to pause/resume my game as well. i am following lynda’s unity 3.5 essential training. they did it with the function Input.ResetInputAxes();. but when i do the same movement of my mouse in game in X-axis freezes, but it doesn’t do the same for Y-axis as shown in the tutorial
To freeze the objects in the game i used Time.timeScale = 0;
here is my code in game manager script —
#pragma strict
var totalScore : int = 0;
var temp : int = 0;
var higestDistance = 0;
var scoreInfoVariable : GUIText;
var fpc :GameObject;
internal var showMenu : boolean = false;
var platform06 : GameObject;
function Start () {
Screen.showCursor = false;
}
function Update () {
if (Input.GetKeyDown ("escape")){
if (Screen.showCursor == false) {
fpc.SendMessage("ToggleInputCursor", true);
Screen.showCursor = true;
showMenu = true;
Time.timeScale = 0;
}
else {
ResumeGame();
}
}
}
function UpdateScoreInfoGUI( distance : int) {
totalScore = totalScore + distance;
scoreInfoVariable.text = ""+ totalScore;
}
function ActivatePlatformPrefab06(){
platform06.active = true;
print("message is received");
}
function OnGUI(){
if(showMenu == true)
{
GUI.BeginGroup(Rect(Screen.width/2 - 50, Screen.height/2 - 50, 100, 90));
GUI.Box(Rect(0, 0, 100, 90), "Menu");
if(GUI.Button(Rect(10, 30, 80, 20), "Resume")){
print("you clicked RESUME");
ResumeGame();
}
if(GUI.Button(Rect(10, 60, 80, 20), "Quit")){
print("you clicked QUIT");
Application.Quit();
}
GUI.EndGroup();
}
}
function ResumeGame(){
Screen.showCursor = false;
fpc.SendMessage("ToggleInputCursor", false);
showMenu = false;
Time.timeScale = 1.0;
}
here is my code in First Person Controller script, my changes are shown with comments —
private var motor : CharacterMotor;
internal var noInput : boolean;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
// -------------- my manual code-----------------
if(noInput==true){
Input.ResetInputAxes();
}
// -------------- my manual code-----------------
// Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// -------------- my manual code-----------------
function ToggleInputCursor(state : boolean){
noInput = state;
print("message is received :"+noInput);
}
// -------------- my manual code-----------------
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")