2017-06-22 69 views
0

我使用下面的代碼在一個特定的時間段輪流我的遊戲對象:遊戲對象不旋轉糾正指定的角度

IEnumerator RotateMe(Vector3 byAngles, float inTime) 
{ 
    Quaternion fromAngle = transform.rotation ; 
    Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + byAngles) ; 
    for(float t = 0f ; t < 1f ; t += Time.deltaTime/inTime) 
    { 
     transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ; 
     yield return null ; 
    } 
} 


public void runCoroutine(Vector3 destination) { 
    StartCoroutine(RotateMe(destination, 0.5f)); 
} 

然後我把它以下列方式:

runCoroutine(new Vector3(0,0,-90)); 

我從測試中意識到我的遊戲對象不是旋轉到指定的角度,而是關閉它們。不確定是什麼導致了這一點。

+0

這是一個簡短的答案:'t <= 1' – Draco18s

+0

@ Draco18s仍然有相同的問題。我試着將時間從0.5秒減少到0.1秒,而且更加不穩定。 – Hilarious404

+0

嘗試添加'transform.rotation = Quaternion.Lerp(fromAngle,toAngle,1);'到方法的末尾。 – Draco18s

回答

0

我會說,使用Lerp方法更好。對於確切的時間相關的事情,你必須介紹lerp的速度。

transform.rotation = Quaternion.Lerp(fromAngle, toAngle, 1/value) 

您可以控制「數值」來控制旋轉速度。我通常使用這種方法有一個更受控制的旋轉。

注意:確保lerping是在「per-frame」方法調用中完成的;更新/固定更新/最新更新