2017-01-03 73 views
0

我想用我的協程來平滑插入多個GameObjects的位置和旋轉,並附上腳本。當我啓動協程時,光滑的部分工作正常,但我的所有對象都移動到同一位置,這不是我想要的。我想明白爲什麼會這樣,以及是否有一個聰明的方法來處理它。協程調用堆棧

這是我的協同程序是什麼樣子:

IEnumerator interpolate_Plate(Transform targ) 
{ 
    float passedTime = 0; 

    while (!transform.Equals(targ)) 
    { 
     passedTime += Time.deltaTime; 

     transform.position = Vector3.Lerp(transform.position, targ.position, passedTime); 
     transform.rotation = Quaternion.Lerp(transform.rotation, targ.rotation,passedTime); 
     yield return null; 
    } 

    yield break; 
} 

我在想,在它的列表創建mastercoroutine,然後調用平滑部分。 問題只是,Targ的引用總是被重置爲堆棧上的所有協程調用?

正如你所需要調用協程的功能:

public void move_Plate(Transform newPosition) 
{ 
    StartCoroutine(interpolate_Plate(newPosition)); 
} 
+0

你打算如何調用interpolate_Plate? –

+0

您必須展示如何啓動協程。此外,你的最後一次休息是無用的,因爲協程在任何時候都會退出。 – Everts

+0

無論如何,您可能不需要協程,在Update中移動邏輯可能足以滿足您的需求。 –

回答

1

好了,所以我找到了解決辦法,因爲統一或C#工程與指針等問題了。我不斷地改變了所有對象的變換,因爲我使用了指向下一個對象變換的指針。但是我移動了這個對象,所以這一切都結束了我移動的最後一個對象。

如何防止這種情況:

我創建存儲我的價值觀使舊的位置和旋轉的新類。我將一個實例存儲在我的課程中,我移動了這些盤子。 我現在從協程更改爲註釋中建議的更新方法。用一面旗幟檢查我是否應該移動物體。然後我順利地將它移動到新位置。

代碼:

private Trans MoveTo; 
private bool move; 

void Update() 
{ 
    if (move) 
    { 
     float passedTime = 0; 
     passedTime += Time.deltaTime; 

     transform.position = Vector3.Lerp(transform.position, MoveTo.position, passedTime); 
     transform.rotation = Quaternion.Lerp(transform.rotation, MoveTo.rotation, passedTime); 
    } 
} 

似乎工作。