I have a lone script (not assigned to a game object) but it does include this command:
Cursor.lockState = CursorLockMode.Locked;
And I know that the cursor doesn't actually lock, because I have tried using Debug.Log
. So below is my full mouse script (with the cursor lock) and a debug script I made.
I have tried:
- Changing my input manager to both.
- No external scripts unlocking my cursor because this is the only script in the project.
- Building and running my project
- Focusing the game window.
Full mouse script:
using UnityEngine;
public class MouseMovement : MonoBehaviour
{
public float mouseSensitivity = 100f;
public float topClamp = -90f;
public float bottomClamp = 90f;
float xRotation = 0f;
float yRotation = 0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, topClamp, bottomClamp) ;
yRotation += mouseX;
yRotation = Mathf.Clamp(yRotation, topClamp, bottomClamp);
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
}
}
Full debug script:
using UnityEngine;
public class CursorLockTest : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Debug.Log("Cursor Locked");
}
if (Input.GetKeyDown(KeyCode.U))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Debug.Log("Cursor Unlocked");
}
}
}