Unity 镜头漫游代码
时间: 2024-05-14 13:14:28 浏览: 164
以下是一个简单的Unity镜头漫游代码示例:
```
using UnityEngine;
public class CameraController : MonoBehaviour
{
// 镜头移动速度
public float moveSpeed = 10f;
// 镜头旋转速度
public float rotateSpeed = 100f;
// Update is called once per frame
void Update()
{
// 镜头移动控制
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float up = Input.GetAxis("Up");
transform.Translate(new Vector3(horizontal, up, vertical) * moveSpeed * Time.deltaTime);
// 镜头旋转控制
float rotateX = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime;
float rotateY = Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
transform.Rotate(-rotateY, rotateX, 0);
}
}
```
该代码会将镜头位置移动到用户输入的方向,并且允许用户使用鼠标旋转镜头。用户可以通过调整`moveSpeed`和`rotateSpeed`变量来修改镜头移动和旋转的速度。
阅读全文
相关推荐








