Cursor lock state statement is not working in unity

Viewed 43

I am following a first person tutorial on brackeys youtube channel. This is the link: https://www.youtube.com/watch?v=_QajrabyTJc

The tutorial requires me to lock my cursor but my cursor doesn't lock, I can move anywhere I would like to. The tutorial is a bit outdated so I am not sure if the code is too.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

public float mouseSensitivity = 100f;

public Transform playerBody;

float xRotation = 0f;

void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{   
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
}
}

I have applied the script onto my camera if this information is needed.

0 Answers
Related