- Home /
The question is answered, right answer was accepted
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;
}
}
}
}
}
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...
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;
}
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. :)
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.
i tried using the key P ins$$anonymous$$d of ESCAPE and it worked thanks
Follow this Question
Related Questions
Cursor.lockState does not work 2 Answers
Cursor won't stay locked in update!! 0 Answers
FPS Controller Mouse not locking consistantly 0 Answers
Trouble with unlocking cursor 1 Answer
CursorLockMode.Locked puts cursor above actual center 0 Answers