Unity的Lerp插值运算

Unity的Lerp插值运算floatresult Mathf Lerp floata floatb floattime Vector3res Vector3 Lerp Vector3start Vector3endPo floattime 以上两种方法均为插值运算 第一个是一维插值 第二个是三维插值 其方法含义以第一个方法为例 代表的意义是 result a b

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232712.html原文链接:https://javaforall.net

(0)
上一篇 2025年9月20日 上午10:01
下一篇 2025年9月20日 上午10:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号