1:API: Lerp (起点,终点,时间)
public static Vector3 Lerp(Vector3 a, Vector3 b, float t);
我们看一下底层的解释:
// 摘要:
// Linearly interpolates between two points.【在两个点之间进行线性插值。】
//
// 参数:
// a:
// Start value, returned when t = 0. 【第一个参数为起点】
//
// b:
// End value, returned when t = 1. 【第二个参数为终点】
//
// t:
// Value used to interpolate between a and b.【第三个参数为用于在a和b之间插值的值。】
//
// 返回结果:
// Interpolated value, equals to a + (b - a) * t.【返回移动过程】
2:套用公式:
在Update方法里调用API
(要移动的物体位置).....position=Vector3.Lerp(......);
3:代码实现:
1)首先我们创建一个空物体,挂载脚本<LerpTest>
2) 公共变量有三种角色:貂蝉、吕布和董卓,我们这里可以用拖拽进行赋值。
3)
由快到慢:
(在update方法里 )吕布奔向貂蝉:
越接近终点,速度越趋于0;
补充:
transform.position=Vector3.Lerp(start,to,0.5f)
Time.deltaTime 与 0.5f 一样是固定值,start就是自己的位置transform.position,是个变量
上面我们提到的第三个参数t为固定值0.5f时,即每一帧移动到当前的(to-start)*0.5的位置,无限接近to,但是到达不了to位置
匀速运动:
startPos,endPos是常量,moveTimer是变量
我们拿董卓奔向貂蝉来举例
timer+=Time.deltaTime=>判断是否到达=>没有到达重置移动人物起点和重点+重置时间timer => Lerp
这样就可以实现匀速运动啦!