Unity鼠标控制相机旋转
时间: 2024-10-28 10:10:17 浏览: 46
在Unity中,你可以通过脚本来实现鼠标控制相机的旋转。通常,这涉及到监听Input.GetAxis("Mouse X") 和 Input.GetAxis("Mouse Y") 函数,这两个函数分别返回左右键的横轴和纵轴值,代表鼠标在屏幕上的滚动和水平移动。
下面是一个简单的示例代码片段:
```csharp
using UnityEngine;
public class MouseControlCamera : MonoBehaviour
{
public float sensitivity = 10.0f;
private Camera mainCamera;
void Start()
{
mainCamera = GetComponent<Camera>();
}
void Update()
{
// 获取鼠标输入
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
// 计算相机的旋转角度
Vector3 rotation = new Vector3(mouseX * sensitivity, mouseY * sensitivity, 0);
// 更新相机的朝向
Quaternion newRotation = Quaternion.Euler(rotation);
mainCamera.transform.rotation = newRotation;
}
}
```
在这个例子中,sensitivity 变量决定了鼠标移动的速度。当运行游戏并按住鼠标滚轮或移动鼠标时,相机会按照鼠标的移动方向进行相应的旋转。
阅读全文
相关推荐

















