Unity 6000版本光标无法锁定的问题求助
Unity光标锁定失效问题
我写了一段未挂载到任何游戏对象的独立脚本,其中包含代码:
Cursor.lockState = CursorLockMode.Locked;
但通过Debug.Log确认,光标并未实际被锁定。以下是我的完整鼠标控制脚本(包含光标锁定逻辑)、调试脚本,以及已经尝试过的操作:
已尝试的操作
- 将输入管理器设置为双向模式
- 确认项目中无其他脚本解锁光标(当前是唯一的脚本)
- 打包并运行项目
- 聚焦游戏窗口
完整鼠标控制脚本
using UnityEngine; public class MouseMovement : MonoBehaviour { public float mouseSensitivity = 100f; public float topClamp = -90f; public float bottomClamp = 90f; float xRotation = 0f; float yRotation = 0f; void Start() { Cursor.lockState = CursorLockMode.Locked; } 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); } }
完整调试脚本
using UnityEngine; public class CursorLockTest : MonoBehaviour { void Start() { } 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"); } } }
内容的提问来源于stack exchange,提问作者Sandwich
相关产品推荐
相关产品推荐

