Tag Archives: Cursor Lock

Unity Cursor Lock& Camera Lock

Functional requirements:
When the menu pops up, the camera is locked and the mouse is displayed; when the menu is closed, the mouse is hidden;
Video tutorial (over the wall required) :
https://www.youtube.com/watch?v=nMgk72JSxz8& list=PLPV2KyIb3jR5PhGqsO7G4PsbEC_Al-kPZ& index=27
The code is as follows:
using UnityStandardAssets.Characters.FirstPerson; // The header file needs to be added camera first person
If (Input. GetKeyDown (KeyCode. B))
{
PauseMenu. SetActive (! PauseMenu.activeSelf);
}// press B to switch the backpack
If (pausemenu.activeself)
{
cursor.visible = true;
if (cursorlockstate == CursorLockMode.Locked)
{
curb.lockstate = CursorLockMode.None; .
}
aaaa GetComponent< RigidbodyFirstPersonController> ().mouseLook.XSensitivity = 0;
aaaa. GetComponent< RigidbodyFirstPersonController> ().mouseLook.YSensitivity = 0;
}// when the backpack is opened, the mouse will display, do not lock, the camera will lock; RigidbodyFirstPersonController script component is the camera,
The motion of the camera can be controlled by controlling XSensitivity and YSensitivity
If (! Pausemenu.activeself)
{
cursor.visible = false;
Cursor. LockState = CursorLockMode. Locked;
aaaa. GetComponent< RigidbodyFirstPersonController> ().mouseLook.XSensitivity = 2;
aaaa. GetComponent< RigidbodyFirstPersonController> ().mouseLook.YSensitivity = 2;
}// when the backpack is closed, the mouse does not show, lock, camera unlock