• 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 /
This question was closed Aug 26, 2016 at 06:12 PM by TBruce for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SrGonzalez · Aug 26, 2016 at 04:47 AM · cursorcodepagelock-cursorlockcursor

Cursor.LockState = CursorLockMode.Locked doasnt works.

i wanted to create a pause menu screen and make it when it is on the cursor lock mode is in none and when the menu is off the cursor is locked, but when i try to lock exiting from the menu it doasnt locks.

here is my code:

using UnityEngine; using System.Collections; using System.Text; using UnityEngine.UI;

namespace UnityStandardAssets.ImageEffects { public class PAUSEMENU : MonoBehaviour {

     public GameObject menu;

     public Slider sensitivity;
     public Text S_Value;

     void Start()
     {
         Cursor.visible = false;
         Cursor.lockState = CursorLockMode.Locked; //<- here works
     }

     void Update()
     {
         Debug.Log(Cursor.lockState);

         Blur effect1 = GetComponentInChildren<Blur>();
         Tonemapping effect2 = GetComponentInChildren<Tonemapping>();

         MouseLook camov = GetComponent<MouseLook>();
         camov.sensitivityX = sensitivity.value;
         camov.sensitivityY = sensitivity.value;

         S_Value.text = "" + sensitivity.value;

         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if (Time.timeScale == 1)//pause ON
             {
                 Time.timeScale = 0;
                 menu.active = true;

                 Cursor.lockState = CursorLockMode.None;
                 Cursor.visible = true;

                 effect1.enabled = true;
                 effect2.enabled = true;

                 camov.active = false;
             }
             else if (Time.timeScale == 0)// pause OFF
             {
                 Time.timeScale = 1;
                 menu.active = false;

                 Cursor.lockState = CursorLockMode.Locked; //<-- but here no
                 Cursor.visible = false;

                 effect1.enabled = false;
                 effect2.enabled = false;

                 camov.active = true;
             }
         }
     }
 }

}

Comment
Add comment · Show 7
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 ScaniX · Aug 26, 2016 at 08:28 AM 0
Share

Are you doing this in the Unity editor? Escape is used to free the cursor, so you are able to do things in the inspector here. In the build the lock is working more consistently.

Although in Windows 10 the cursor gets magically unlocked after not moving the mouse for a few seconds, so it needs a relock, but I digress...

avatar image b1gry4n · Aug 26, 2016 at 09:44 AM 0
Share

I dont think anything is wrong with your code and I am unsure how the cursor lockstate actually works. It shouldnt matter if it is in the editor or not. $$anonymous$$aybe you have code somewhere else that is also trying to modify the cursor lock state? Heres an example from my code that 100% works in displaying/hiding the cursor. It is something I set up temporarily and is called every frame in update, but it shouldnt matter if you made it a function or made sure it was only called once.

    if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Escape))
             {
                 mouseVisible = !mouseVisible;
             }
     
                 if (mouseVisible)
                 {
                     Cursor.lockState = CursorLock$$anonymous$$ode.None;
                     Cursor.visible = true;
                 }
                 else {
                     Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
                     Cursor.visible = false;
                 }
avatar image ScaniX b1gry4n · Aug 26, 2016 at 09:55 AM 0
Share

In editor it is different as Escape is used to unlock cursor there. But probably using Get$$anonymous$$eyUp() would make a difference in editor as it might be processed after the editor unlocked it if that one uses the key down event. Will try that later, but it is probably a good idea to keep that functionalty in the editor while developing. :)

avatar image jcrosby ScaniX · Dec 30, 2016 at 03:28 AM 0
Share

Should hitting Escape in a build work to release the mouse too then? I'm running into same issues and it does not work as it should in build or editor. But I notice that I can hit Escape in a release build and it release the mouse.

Show more comments
avatar image SrGonzalez · Aug 26, 2016 at 06:11 PM 0
Share

i tried using the key P ins$$anonymous$$d of ESCAPE and it worked thanks

avatar image z741092665 · Jun 04, 2017 at 05:57 AM 0
Share

Has this problem been solved?

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cursor.lockState does not work 1 Answer

Cursor won't stay locked in update!! 0 Answers

FPS Controller Mouse not locking consistantly 0 Answers

Trouble with unlocking cursor 1 Answer

Can you lock the cursor to specific coordinates? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges