public class CameraFollow : MonoBehaviour
{
public Transform target;//跟随目标
//后上方位置,因为摄像机不是跟目标相同位置的,而是在后上方看向目标的
public Vector3 targetPos;//目标后上方的位置
public Vector3 targetNowPos;//目标后上方目前的位置
public float time;//匀速运动Lerp函数重置时间
public Vector3 startPos;//匀速运动要记录开始地点不变
// Start is called before the first frame update
void Start()
{
startPos = this.transform.position;
}
// Update is called once per frame
void Update()
{
//镜头正对世界坐标z轴,旋转时物体一直在镜头中轴上
targetPos.x = target.position.x;
targetPos.y = target.position.y + 7;
targetPos.z = target.position.z - 4;
//镜头正对世界坐标z轴,旋转时物体会偏移
//targetPos = target.position + target.up * 7 - target.forward * 4;
//先快后慢的跟随
//this.transform.position = Vector3.Lerp(this.transform.position, targetPos, Time.deltaTime);
//this.transform.LookAt(target);
//匀速跟随
if (targetNowPos !=targetPos)
{
targetNowPos = targetPos;
time = 0;
startPos = this.transform.position;
}
time += Time.deltaTime;
this.transform.position = Vector3.Lerp(startPos, targetNowPos, time);
}
}
Unity插值运算功能的镜头跟随
于 2023-04-05 12:35:29 首次发布