unity鼠标镜头旋转
时间: 2023-06-22 08:17:53 浏览: 93
在Unity中,可以使用以下代码来实现鼠标控制镜头旋转:
```csharp
public float sensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
```
其中,sensitivity变量控制鼠标灵敏度,playerBody变量是要进行旋转的物体的Transform组件。在Start函数中,让鼠标锁定在屏幕中心,避免鼠标移动过程中超出屏幕导致旋转失效。在Update函数中,获取鼠标移动的距离,然后根据距离进行旋转。xRotation变量控制上下旋转,yRotation变量控制左右旋转。最后调用transform.localRotation和playerBody.Rotate函数来实现旋转。
阅读全文
相关推荐













