向量插值

Unity引擎 · 01-16 · 177 人浏览
public Transform A;
public Transform B;
public Transform Target;

private Vector3 startPos;
private float time;
private Vector3 nowPos;

public Transform C; 
// Start is called before the first frame update
void Start()
{
    startPos = B.position;
}

// Update is called once per frame
void Update()
{
    //公式:resuly=start+(end-start)*t
    //1.先快后慢 每帧改变start位置 位置无限接近 但不会得到end位置
    A.position = Vector3.Lerp(A.position, Target.position, Time.deltaTime);

    //2.匀速 每帧改变时间 当t>=1,得到结果
    //参3是不断变化的
    //这种匀速移动 当初time>=1时 改变了目标位置后 它会直接瞬移到我们的目标位置
    if (nowPos != Target.position)
    {
        nowPos = Target.position;
        time = 0;
        startPos = B.position;
    }
    time += Time.deltaTime;
    B.position = Vector3.Lerp(startPos, nowPos, time);


    //球形插值
    //弧形移动到目标点
    C.position = Vector3.Slerp(Vector3.right * 10, Vector3.forward * 10, time);

    //总结
    //1.线性插值-用于跟随移动,摄像机跟随
    //2.用于曲线移动,模拟太阳运动弧线
}
Theme Jasmine by Kent Liao