镜头跟随
第一种:简单的跟随
固定位置,跟参考对象保持一个固定的位置,跟随参考的对象移动
using UnityEngine;
using System.Collections;
public class CameraFlow : MonoBehaviour
{
public Transform target;
private Vector3 offset;
void Start()
{
//设置相对偏移
offset = target.position - this.transform.position;
}
void Update()
{
this.transform.position = target.position - offset;
}
}
按照上述方式,当参照物的重心更改的过于频繁时,就会造成卡顿
解决方法:加入插值
插值:
//让目标跟随
Vector3 targetPos = target.position - offset;
this.transform.position = Vector3.Lerp(transform.position, targetPos, 5 * Time.deltaTime);