帮我unity生成一个通过鼠标移动控制物体旋转的代码
时间: 2024-05-12 17:13:04 浏览: 180
好的,以下是通过鼠标移动控制物体旋转的Unity C#代码:
```csharp
using UnityEngine;
public class RotateObjectWithMouse : MonoBehaviour
{
public float sensitivity = 100.0f; // 鼠标灵敏度
private float rotationX = 0.0f;
private float rotationY = 0.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;
// 计算物体在x和y方向上的旋转角度
rotationX += mouseY;
rotationY += mouseX;
// 限制物体在x方向上旋转角度的范围
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
// 应用旋转
transform.localRotation = Quaternion.Euler(-rotationX, rotationY, 0f);
}
}
```
将此脚本添加到需要旋转的物体上,通过移动鼠标来旋转物体。
阅读全文
相关推荐















