I’ve been following a Brackeys tutorial for first person movement and have followed the code exactly. It uses cursor lockstate to lock the cursor to the center of the screen, but when I run the game, it doesn’t work. The cursor is hidden when it’s within the game window, but as soon as it gets further, it reappears, meaning that cursor lock mode isn’t working. Does anyone know how to get it to work? I know there are other questions on unity answers about this same thing, but I’ve reviewed them and none of them are helpful. They are either outdated, or saying “use cursor.lockstate = cursorlockmode.locked” instead of the outdated methods. Here is the code:
`using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float MouseSensetivity = 250.0f;
public Transform playerBody;
float xRot = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * MouseSensetivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * MouseSensetivity * Time.deltaTime;
xRot -= mouseY;
xRot = Mathf.Clamp(xRot, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
if (Input.GetKey(KeyCode.Tab))
{
Cursor.lockState = CursorLockMode.None;
}
}
}`