Is there a simple way just to stop everything when P is pressed, and then play everything when P is pressed again. Basically wondering if there is a simple command to pause everything
Use
Time.timeScale = 0;
Further reading…
This works with everything apart from mouse movement. Is there a quick way to stop input from mouse
Do you mean like click/drag input or do you mean moving the mouse … or both?
Just curious.
Simply just stop adjusting your view or mouse position for that period of time?
You could create a bool called alive. Then only have the ability to move your mouse if that bool is true and if your game is paused then set the bool to false so you no longer are getting the mouse input in the game.
you can hide the mouse, and show it back:
public void LockCursor(bool locked)
{
if (locked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
public void PauseGame(bool pauseState)
{
if (pauseState)
{
Time.timeScale = 0f;
}
else
{
Time.timeScale = 1;
}
}
Update() will still continue to be called when Time.timeScale is zero. The Time.deltaTime will be zero in this case.
Depending on your mouse look code, that may enable you to continue looking around.
At the start of your Update() you can do a check for Time.timeScale == 0 and just return early.
Moving the mouse.
Ive just got home so not tried out any of the suggestions yet but, Just wanted to say thanks for the replies. Will try out the suggestions tonight.
Alright, well see how it goes with some things you try. =)
ok finally got enough time to try this out.
Ive manage to get the pause working and the mouse not moving when paused. However I cant get it to start moving again. I am assuming its just something stupid in the code I am missing but cant figure out what, any ideas?
Code for pause
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
Time.timeScale = 0f;
TempText.text = ("The game is paused.Press U to unpause. Press F12 to quit the game!");
}
if (Input.GetKeyDown(KeyCode.U))
{
Time.timeScale = 1f;
TempText.text = ("");
}
}
}
Code for mousemovement
void Update ()
{
if (Time.timeScale > 0)
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); // md = mouse delta
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing); // left/right
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing); // up/down
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
else
{
Debug.Log("timescale");
sensitivity = 0;
smoothing = 0;
}
}
I think just don’t set your sensitivity and smoothing to 0 when the timescale is 0. Since the code won’t be running anyways. And when you’ve set it to zero, and it resumes, you’re now multiplying your ‘md’ by zero, I think. ![]()
Huh, thats weird. I did try it without the else statement and it was not working correctly. Just tried it again, and now working fine
.
Oh well. Thanks for all the help ![]()
no prob. Glad it’s working for you.