Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by SamReha · May 03, 2016 at 06:36 PM · pause menu

How to lock / unlock Cursor and Mouselook when using FPSController

I'm working on a Unity 5 project and am using the First Person Character prefab to handle the player character. I'm struggling to implement a pause menu because I cannot figure out how to correctly lock and unlock the cursor and mouselook.

In my current code, the cursor unlocks when the game is paused, but locks as soon as the player clicks (even if they game is still paused). It also fails to lock when the game is unpaused, again unless the player clicks. The MouseLook also never seems to disable, meaning the camera is visibly swinging around when the game is paused.

I want a way to lock the mouse look and unlock the cursor for the entire time the game is paused so that the player can interact with the menu, and do the reverse whenever the game is not paused to let the player play the game.

Here is a sample of my UI controller that is meant to handle the logic for pausing and unpausing the game:

 public class UIManager : MonoBehaviour {
     private AudioSource pauseSource;
 
     public GameObject PanelPause;
     public SettingsManager settingsManager;
     public FirstPersonController player;
     public bool isPaused = false;
 
     void Start () {
         pauseSource = GetComponent<AudioSource> ();
         
         pauseSource.ignoreListenerPause = true;
         pauseSource.Play (); 
         pauseSource.Pause ();
         
         PanelPause.SetActive(isPaused);
     }
         
     void Update () {
         if (Input.GetButtonDown("Cancel")) {
             isPaused = !isPaused;
         }
 
         if (!isPaused) {
             PanelControls.SetActive(viewControls);
         }
 
         setPause(isPaused);
     }
 
         public void setPause(bool pauseState) {
             player.m_MouseLook.SetCursorLock(!pauseState);
 
             PanelPause.SetActive(pauseState);
             AudioListener.pause = pauseState;
 
             if (pauseState) {
                 // Do logic to pause
                 Time.timeScale = 0.0f;
 
                 pauseSource.Pause();
 
                 Cursor.lockState = CursorLockMode.None;
             } else {
                 // Do logic to unpause
                 Time.timeScale = 1.0f;
 
                 pauseSource.UnPause();
 
                 settingsManager.exitSettings();
 
                 Cursor.lockState = CursorLockMode.Locked;
             }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Adventuruss · Jun 22, 2017 at 10:58 AM

In order to unlock the cursor with the First Person Controller, you need to know where the FPC script is located in relation to your code. For me, when I imported it into the scene, I left it in its default folder. Because of that, I had to access it through that folder structure. You also need to make the m_MouseLook variable public in the FirstPersonController script, and the m_cursorIsLocked variable in the MouseLook script in order to access them.This will still allow the mouse movement to control the camera, but will also free the mouse.

 public GameObject FPC;  // Assign the First Person Controller to this in the Editor.

 FPC.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().m_MouseLook.m_cursorIsLocked = false;



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 menonarjun98 · May 06, 2020 at 03:22 PM 0
Share

@Adventuruss the cursor is visible but we can not use it in the game to click buttons how can i solve that i used your above code thank you

avatar image
0

Answer by RaDus1 · Aug 12, 2016 at 04:04 PM

I don't know the context, but wouldn't it be better to ignore the paused state and just unlock/lock the mouse movement when called for?

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

56 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

Related Questions

Pause menu not working!! 1 Answer

i can get my code to recognize if object is active or not 0 Answers

pause video texture and animation in pause menu 0 Answers

Pause menu works in editor but not after the game is built. (New Developer) 0 Answers

Pause Menu Help 0 Answers


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