Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Freddie_Tuke · Feb 09, 2021 at 05:49 PM · pausing

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.

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
0
Best Answer

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

Comment
Add comment · Show 1 · 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 Freddie_Tuke · Feb 10, 2021 at 08:43 AM 0
Share

Thanks so much! also its not really pausing its more just a menu to access settings and quit

avatar image
0

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;
 }

@Freddie_Tuke

Comment
Add comment · Show 1 · 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 Freddie_Tuke · Feb 09, 2021 at 08:13 PM 0
Share

@Llama_w_2Ls $$anonymous$$y pause script is on a separate game object to my look and movement script. Will this make a difference?

avatar image
0

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;
     }
 }

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

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

110 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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