float result = Mathf.Lerp(float a, float b, float time)
Vector3 res = Vector3.Lerp(Vector3 startPos, Vector3 endPos, float time)
以上两种方法均为插值运算,第一个是一维插值,第二个是三维插值,其方法含义以第一个方法为例,代表的意义是:
result = a + (b-a) * time, 其中time值在【0,1】范围内起作用,
当time=0时,result = a, 当time=1时,result=b, 当time>1时,result=b
可以利用插值运算来实现各种平滑效果,比如相机的平滑移动,物体的匀速运动
相机的平滑移动:
相机由当前位置移动到targetTrans位置,smooth可调节移动的速度
void Update(){
//smooth为平滑度
cameraTrans.position = Vector3.Lerp(cameraTrans.position, targetTrans.position, smooth);
}
物体的匀速运动
物体由startPos位置移动到endPos位置,匀速移动,花了duration时间
public Vector startPos;
public Vector endPos;
public float duration;
public float time;
void Update(){
time += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, endPos, time/duration)
}