- Home /
how to stop player movement and look when paused
Hi, I'm pretty new to unity (6 months or so). I'm making a menu screen but I can still move and look when I'm paused. I want the player to stay still when paused. Please help
Here is my movement script -
 using UnityEngine;
 using Photon.Pun;
 
 public class PlayerMovement : MonoBehaviourPunCallbacks
 {
     public CharacterController controller;
     public float speed;
 
     void Update()
     {
         if (photonView.IsMine)
         {
             Move();
         }
     } 
 
     void Move()
     {
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
 
         Vector3 move = transform.right * x + transform.forward * z;
 
         controller.Move(move * speed * Time.deltaTime);
     }
 }
 
here is my look script -
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 
 public class MouseLook : MonoBehaviourPunCallbacks
 {
     public float mouseSensitivity;
     public Transform playerBody;
     float xRotation;
 
     public bool isPaused;
 
     private void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
 
         if (!photonView.IsMine)
         {
             GetComponent<Camera>().enabled = false;
             GetComponent<AudioListener>().enabled = false; 
         }
     }
 
     private void Update()
     {
         if (photonView.IsMine && !isPaused)
         {
             Look();
         }
     }
 
     void Look()
     {
         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
 
         xRotation -= mouseY;
         xRotation = Mathf.Clamp(xRotation, -80, 80);
 
         transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
         playerBody.Rotate(Vector3.up * mouseX);
     }
 }
and here is my pause script -
 sing UnityEngine;
 using Photon.Pun;
 
 public class PauseMenu : MonoBehaviourPunCallbacks
 {
     public int menuSceneIndex;
     public GameObject pauseMenu;
     private bool isPaused = false;
 
     private void Update()
     {
         Pause();
     }
 
     void Pause()
     {
         if (Input.GetKeyDown(KeyCode.Escape) && !isPaused)
         {
             isPaused = true;
         }
         else if (Input.GetKeyDown(KeyCode.Escape) && isPaused)
         {
             isPaused = false;
         }
 
         if (isPaused)
         {
             pauseMenu.SetActive(true);
             Cursor.lockState = CursorLockMode.None;
         }
         else
         {
             pauseMenu.SetActive(false);
             Cursor.lockState = CursorLockMode.Locked;
         }
     }
 
     public void OnResumeButtonClicked()
     {
         pauseMenu.SetActive(false);
         isPaused = false;
     }
 
     public void OnSettingsButtonClicked()
     {
 
     }
 
     public void OnQuitButtonClicked()
     {
         PhotonNetwork.LeaveRoom();
         PhotonNetwork.LoadLevel(menuSceneIndex);
     }
 }
I know my code isn't best but I did the pause script myself and I'm just trying to get better.
Answer by SunnyChow · Feb 10, 2021 at 03:45 AM
add "static" on one "isPaused" paramter, and make all other components check that "isPaused"
  public class PauseMenu : MonoBehaviourPunCallbacks
  {
      ...
      public static bool isPaused = false;
      ...
And then
  public class MouseLook : MonoBehaviourPunCallbacks
  {
  ...
      private void Update()
      {
          if (photonView.IsMine && !PauseMenu.isPaused)
          {
             ...
IMO, it's weird to have pausing in a multiplayer game
Thanks so much! also its not really pausing its more just a menu to access settings and quit
Answer by Llama_w_2Ls · Feb 09, 2021 at 05:59 PM
You'll need references to the scripts from your pause script. Then, you can enable/disable them on pause. For example:
 public PlayerMovement movement;
 public MouseLook cameralook;
 
 public void Pause()
 {
      movement.enabled = false;
      cameralook.enabled = false;
 }
 
 public void Resume()
 {
      movement.enabled = true;
      cameralook.enabled = true;
 }
@Llama_w_2Ls $$anonymous$$y pause script is on a separate game object to my look and movement script. Will this make a difference?
Answer by Megaboy238 · Feb 09, 2021 at 06:44 PM
This might be more what you want
To Stop game clock
     Time.timeScale = 0f;
To Start again
     Time.timeScale = 1f;
This maybe more what you want.
 public class PauseMenu : MonoBehaviour
 {
     public static bool GameIsPaused = false;
 
     public GameObject pauseMenuUI;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if (GameIsPaused)
             {
                 Resume();
             }
             else
             {
                 Pause();
             }
         }
     }
 
     public void Resume()
     {
         pauseMenuUI.SetActive(false);
         Time.timeScale = 1f;
         GameIsPaused = false;
 
     }
 
     void Pause()
     {
         pauseMenuUI.SetActive(true);
         Time.timeScale = 0f;
         GameIsPaused = true;
     }
 
     public void Options()
     {
 
     }
 
     public void Controls()
     {
 
     }
 
     public void Quit()
     {
         SceneManager.LoadScene(0);
         ScoreScript.scoreValue = 0;
         ScoreScript.easterEggValue = 0;
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
disabling a script (MouseLook) 5 Answers
Pause game when user pulls down the notification panel 0 Answers
Prevent Unloading assets for Reloading scene for a second time! 1 Answer
Pausing a State Machine FlexFSM 1 Answer
i have some pausing errors 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                