[英]New to unity, why does this script not move the object? (Unity 3D)
[英]Unity 3D Move object over specific time on GetButtonDown
我有一個空的游戲對象,里面充滿了我所有的玩家組件,在其中是一個模型,當按下由GetButtonDown實例化的控件時,該模型會對其自身進行動畫處理。 當前,玩家游戲對象中的所有內容都會立即移動到下一個點(基本上是在沒有過渡的情況下傳送到該點)。 有沒有辦法讓我將所有內容移動超過1秒鍾而不是立即到達那里? 這是GetButtonDown發生的情況
if (Input.GetButtonDown ("VerticalFwd"))
{
amountToMove.y = 1f;
transform.Translate (amountToMove);
}
我已經嘗試過transform.Translate(amountToMove * Time.deltaTime * randomint); 以及各種使用時間的方式,盡管這似乎是最合乎邏輯的方式,但似乎仍然行不通。 我猜是因為GetButtonDown僅在按下時“運行”,並且沒有更新功能來“計時”每一幀的移動? 有任何想法嗎? amountToMove也保存為Vector3。
第一個公式是錯誤的。 正確的公式為:
this.transform.position += (Distance/MoveTime) * time.deltatime
嘗試.Lerp,它作為協程在某個時間量Vector3.Lerp(Vector3開始,Vector3結束,浮動時間)上插值
在這里查看文檔
這應該使您大致了解發生了什么情況
Vector3 Distance = End - Start;
// this will return the difference
this.transform.position += Distance/time.deltatime * Movetime
// this divides the Distance equal to the time.deltatime.. (Use Movetime to make the movement take more or less then 1 second
IE:如果Distance為1x.1y.1z,並且time.deltatime為.1且Movetime為1 ....,則您每跳動的移動值為.1xyz,花費1秒到達終點
僅供參考:transform.Translate就像一個奇特的.position + =,因此您可以互換使用它們
編輯這里有一些解決方案
簡單 ::
Vector3 amountToMove = new Vector3(0,1,0); // Vector3.up is short hand for (0,1,0) if you want to use that instead
if (Input.GetButtonDown ("VerticalFwd"))
{
transform.position = Vector3.Lerp(transform.position ,transform.position + amountToMove, 1);
}
困難,可能不必要:
撰寫協程,做了一種線性插值為特定應用程序查看此文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.