unity3d Vector3.Lerp解析
Vector3.Lerp:http://www.ceeger.com/Script/Vector3/Vector3.Lerp.html
手冊(cè)中描述的不是很詳細(xì),什么叫“按照數(shù)字t在from到to之間插值”???t代表什么鬼?還得自己測(cè)試一下才知道
?
我以前這樣用過(guò):
from.position = Vector3.Lerp(from.position, to.position, Time.deltaTime);
或者想要快一些我就這樣:
from.position = Vector3.Lerp(from.position, to.position, Time.deltaTime * 2f);
不知道大家有沒(méi)有像我這樣用過(guò)!第三個(gè)參數(shù)取值范圍0~1,其實(shí)就是距離百分比,比如填0.5f,那么就是取A點(diǎn)到B點(diǎn)的中間位置
如果像我上面那種方法去使用,就會(huì)導(dǎo)致物體在移動(dòng)的時(shí)候會(huì)越來(lái)越慢
假設(shè)A點(diǎn)到B點(diǎn)距離是10,第三個(gè)參數(shù)t我填0.5f,那么插值一次后距離就減少了一半,在執(zhí)行一次,又減少了一半距離
當(dāng)然,如果需求就是這樣的話直接用就行了,但是如果需求是勻速平滑到某點(diǎn),這咋辦呢?
既然都知道第三個(gè)參數(shù)其實(shí)就是距離百分比了,那我們自己算一下距離百分比不就行了嗎?
1 public Transform from; 2 public Transform to; 3 public float mMoveTime; 4 private Vector3 mStartPos; 5 private float t; 6 7 private bool mIsPlay = false; 8 9 void Update() 10 { 11 if (!mIsPlay) 12 return; 13 14 t += 1f / mMoveTime * Time.deltaTime; 15 from.position = Vector3.Lerp(mStartPos, to.position, t); 16 } 17 18 void OnGUI() 19 { 20 if(GUI.Button(new Rect(100,100,100,30),"play")) 21 { 22 mStartPos = from.position; 23 mIsPlay = true; 24 } 25 }?
看了上面的介紹,相信對(duì)Vector3.Lerp有些了解了!
如果我想要他移動(dòng)到指定位置后繼續(xù)保持勻速運(yùn)動(dòng)怎么做呢?也許你會(huì)說(shuō)用Vector3.Lerp完全可以啊!
可是你別忘了,它的取值范圍是0~1,也就是說(shuō)>1的值它會(huì)忽略掉,我測(cè)試了一下的確如此
那看來(lái)只能自己實(shí)現(xiàn)了!這其實(shí)很簡(jiǎn)單,下面我們就來(lái)自己實(shí)現(xiàn)一遍
1 void Update() 2 { 3 if (!mIsPlay) 4 return; 5 6 t += 1f / mMoveTime * Time.deltaTime; 7 //from.position = Vector3.Lerp(mStartPos, to.position, t); 8 from.position = mStartPos + (to.position - mStartPos) * t; 9 }?
?原文鏈接:http://www.cnblogs.com/shenggege/p/5658650.html
轉(zhuǎn)載于:https://www.cnblogs.com/shenggege/p/5658650.html
總結(jié)
以上是生活随笔為你收集整理的unity3d Vector3.Lerp解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机网络(10)-----TCP的拥塞
- 下一篇: 7.9实习报告