创建一个相机,挂载这个脚本即可
public class FilmPlaybackMgr
{
Camera wanderCamera;
bool isWanderCam = false;
int CameraIndex = 1;
/// <summary>
/// 玩家输入控制
/// </summary>
Vector2 userInput = Vector2.zero;
Vector2 mouseDelta = Vector2.zero;
/// <summary>
/// 鼠标移动灵敏度
/// </summary>
float sensitivity = 2;
/// <summary>
/// 移动中平滑
/// </summary>
float smoothing = 1.5f;
Vector2 velocity, frameVelocity;
bool isRunning = false;
private void Start()
{
wanderCamera = GetComponent<Camera>();
}
private void Update()
{
GetUserInput();
}
void GetUserInput()
{
userInput.x = Input.GetAxis("Horizontal");
userInput.y = Input.GetAxis("Vertical");
mouseDelta.x = Input.GetAxisRaw("Mouse X");
mouseDelta.y = Input.GetAxisRaw("Mouse Y");
}
public float wanderCameraMoveSpeed = 10;
[HideInInspector]
public float wanderCameraRunSpeed=20;
void WanderCameraCtrl()
{
float moveSpeed= isRunning ? wanderCameraRunSpeed : wanderCameraMoveSpeed;
userInput = userInput * Time.deltaTime * moveSpeed;
wanderCamera.transform.Translate(userInput.x, 0, userInput.y);
Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
velocity += frameVelocity;
wanderCamera.transform.localRotation = Quaternion.Euler(-velocity.y, velocity.x, 0);
}
}
简单的相机漫游就完成了,如果有问题欢迎大家评论里面提出来哦,
|