2014-01-18 95 views
0

當我按下游戲手柄或鍵盤上的按鈕時,我希望我的角色停止。當我按下按鈕時,角色必須執行特定的動畫,因此完全沒有任何動作,只有動畫。 我想弄清楚WaitForSeconds是如何工作的,但是當我嘗試使用它時,它不起作用。這裏調用WaitForSecondsWaitForSeconds沒有效果

public IEnumerator Wait() 
{ 
    yield return new WaitForSeconds (6); 
} 

功能的代碼,當布爾變量animationTest是真實的我想要的程序等待6秒

if (animationTest) 
{ 
    UnityEngine.Debug.Log ("check1"); 
    StartCoroutine (Wait()); 
    UnityEngine.Debug.Log ("check2"); 
    animationTest = false; 
} 

但這不起作用! check1和check2同時打印。我錯過了一些東西。這在FixedUpdate()運行。

+1

爲什麼不發佈在http://answers.unity3d.com? – thumbmunkeys

回答

2

協程不會像這樣工作。它開始一個新的(並行)執行。 爲了實現等待,你必須在IEnumerator中完成。

public IEnumerator SomethingElse() { 
    animationTest = false; 
    Debug.Log("check1"); 
    yield return new WaidForSeconds(6f); 
    Debug.Log("check2"); 
    yield return true; 
} 

void FixedUpdate() { 
    if (animationTest) { 
    StartCoroutine(SomethingElse()); 
    } 
} 

現在,當您設置animationTest在某些時候,你應該看到兩個日誌以6秒之間的時間間隔。

+0

謝謝你的回答。現在,如果我想停止並行執行,但是從啓動WaitForSeconds的線程開始,我該怎麼做? – Astinog