using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Transform target; // 目标物体
public float distance = 10f; // 相机与目标物体的距离
public float xSpeed = 250f; // 水平旋转速度
public float ySpeed = 120f; // 垂直旋转速度
public float yMinLimit = -20f; // 垂直旋转最小角度
public float yMaxLimit = 80f; // 垂直旋转最大角度
private float x = 0f; // 水平旋转角度
private float y = 0f; // 垂直旋转角度
public float smoothSpeed;
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0))
{
// 鼠标右键按下时,根据鼠标移动改变相机角度
x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
// distance -= Input.GetAxis("Mouse ScrollWheel") * 5f; // 鼠标滚轮控制相机距离
// distance = Mathf.Clamp(distance, 3f, 15f);
// Quaternion rotation = Quaternion.Euler(y, x, 0); // 根据角度创建四元数
// Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position; // 根据四元数和距离计算相机位置
Quaternion rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * smoothSpeed); // 根据当前旋转和目标旋转进行插值
// Vector3 position = Vector3.Lerp(transform.position, rotation * new Vector3(0.0f, 0.0f, -distance) + target.position, Time.deltaTime * smoothSpeed); // 根据当前位置和目标位置进行插值
transform.rotation = rotation;
// transform.position = position;
}
}
static float ClampAngle(float angle, float min, float max)
{
// 将角度限制在min和max之间
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}```
unity 第一人称相机原地带有阻尼效果
Guess you like
Origin blog.csdn.net/qiao2037641855/article/details/129592998
Recommended
Ranking