unity3d摄像头移动代码
时间: 2023-09-15 21:19:08 浏览: 69
以下是Unity3D中摄像头移动的示例代码:
```
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public float moveSpeed = 10f; // 摄像头移动速度
public float rotateSpeed = 100f; // 摄像头旋转速度
void Update()
{
// 移动摄像头
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float depth = Input.GetAxis("Depth");
transform.Translate(new Vector3(horizontal, depth, vertical) * moveSpeed * Time.deltaTime);
// 旋转摄像头
float rotateX = Input.GetAxis("Mouse X");
float rotateY = Input.GetAxis("Mouse Y");
transform.Rotate(new Vector3(0, rotateX, 0) * rotateSpeed * Time.deltaTime, Space.World);
transform.Rotate(new Vector3(-rotateY, 0, 0) * rotateSpeed * Time.deltaTime, Space.Self);
}
}
```
这个脚本将允许你使用键盘和鼠标移动和旋转摄像头。键盘的方向键可以移动摄像头,鼠标可以旋转摄像头。你可以根据需要调整摄像头移动速度和旋转速度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)