所以我有一個腳本來移動遊戲對象。當我移動一個gameObject的時候,它很流暢。但是當我第二次移動它時,這個動作非常緩慢,看上去有點兒缺陷。Unity GameObject在第二次移動時不能平滑運行
移動腳本中的第一個輸入是對象,然後它需要移動到的位置和速度作爲最後一個參數。所有座標均基於本地位置。我使用等待,因爲我想在執行第二次移動之前等待。
我試圖移動其他物體兩次,但它們都最終移動非常緩慢/越野車。
我不想在更新中運行此更新,這就是爲什麼我使用協程。
這裏是我的代碼:
IEnumerator MovementGentryOne()
{
StartCoroutine(Movement(GentryOne, MovementCoords.GentryOneBasin, gentryspeed));
yield return new WaitForSeconds(2);
StartCoroutine(Movement(GentryOneArm, MovementCoords.GentryArmMoved, gentryspeed));
yield return new WaitForSeconds(2);
StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerPick, gentryspeed));
yield return new WaitForSeconds(4);
//this one is not working smooth.
StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerStart, gentryspeed));
yield return null;
}
private IEnumerator Movement(GameObject toMove, Vector3 position, float time)
{
float elapsedTime = 0;
while (elapsedTime < time)
{
toMove.transform.localPosition = Vector3.Lerp(toMove.transform.localPosition, position, (elapsedTime/time));
elapsedTime += Time.deltaTime;
yield return null;
}
toMove.transform.localPosition = position;
}
任何人的想法是怎麼回事?
親切的問候
你可以確認一個Movement-Coroutine已經完成,然後你開始一個新的?否則,你會爲運動而戰,這會造成奇怪,不平滑和不穩定的運動。 – Maakep
您可以使用yield return StartCoroutine而不是在兩者之間等待秒數嗎? –
看來協程並不會停止。使用調試器,我發現它從來沒有達到聲明後返回收益返回null –