2011-04-20 65 views
1

我試圖製作一個遊戲,在那裏你可以刺傷敵人,敵人掙扎大約一秒鐘後就會死亡。 (布娃娃);如何在查找遊戲對象之前添加延遲

我認爲最好只顯示我的劇本,你知道我的意思:

在觸發器輸入腳本:

if(other.tag == "enemy"){ 
    other.transform.parent.gameObject.name = ("enemy" + currentEnemy); 
    print(other.name); 
    gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse(); 
    BloodParticle.emit = true; 
    Stabbed = true; 
    Character.GetComponent("MouseLook").enabled = false; 
    Character.GetComponent("CharacterMotor").enabled = false; 

} 

,並在更新功能:

if(Stabbed == true){ 
    StopBleeding ++; 
} 

if(StopBleeding > 50){ 
    Stabbed = false; 
    StopBleeding = 0; 
    currentEnemy ++; 
    Character.GetComponent("MouseLook").enabled = true; 
    Character.GetComponent("CharacterMotor").enabled = true; 
    BloodParticle.emit = false; 
} 

現在,當我的刀進入敵人的碰撞,敵人imediatly下降到地板上。 我試着把:

gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse(); 

在更新功能if(StopBleeding > 50)

如果我這樣做,我會得到一個null reverance異常的錯誤,因爲腳本cand找到了敵人。雖然它可以讓我的觸發器進入。

基本上我的問題是:有沒有辦法解決這個錯誤給它一個50幀的延遲(所有在StopBleeding作品)?

或者有沒有什麼方法可以在Ragdoll啓動前進行簡單的延遲?

在此先感謝

回答

0

可以使用StartCoroutine( 「DelayDeath」);其中DelayDeath是方法的名稱。見下文。

if(other.tag == "enemy"){ 
    other.transform.parent.gameObject.name = ("enemy" + currentEnemy); 
    print(other.name); 
    StartCoroutine("DelayDeath"); 
    BloodParticle.emit = true; 
    Stabbed = true; 
    Character.GetComponent("MouseLook").enabled = false; 
    Character.GetComponent("CharacterMotor").enabled = false; 

} 

private IEnumerator DelayDeath() 
{ 
    //this will return...and one second later re-enter and finish the method 
    yield return new WaitForSeconds(1.0f); 
    gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse(); 
} 
+0

請注意,這是C#語法。 Unity JS語法完全不需要使用Coroutines。查找Unity JS的「yield」關鍵字。 – scriptocalypse 2011-04-20 23:28:38

0

由於您要具體瞭解Unity JS而不是C#,因此您將使用yield關鍵字。

print("foo"); 
yield WaitForSeconds (5.0); // waits for 5 seconds before executing the rest of the code 
print("bar"); 
0

在刺傷時,您可以查看敵方物體,但將其保存在變量中而不是發出死亡的敵人。出血結束後,你不必查找敵人,並可以在保存的人身上觸發死亡。

另一個提示:不要將幀計數爲幀可能具有不同的長度。從Time.deltaTime加起來,直到達到讓我們說2秒,然後觸發死亡。

希望有所幫助。