最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Why does my cursor not lock in Unity? (Version 6000) - Stack Overflow

programmeradmin4浏览0评论

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");
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论