Unity的Lerp插值运算

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)
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值