2014-07-08 55 views
0

我正在製作一個A.I角色走向玩家。一旦collider2D通過使用Add force進入玩家並且效果很好並且花花綠綠,但是當A.I退出玩家collider2D時,它會繼續添加力量。所以我加了onTriggerExit2D()來使力0或停止應用它。onTriggerExit2D()不工作? unity2D c#

出於某種原因,從不退出觸發器我已經使用調試,但沒有任何顯示。

void Update() { 

    if (canHearPlayer) 
    { 
     rigidbody2D.AddForce (new Vector2 (-force, 0)); 
    } 
    if (!canHearPlayer) 
    { 
     force = 0; 
    } 
} 

void FixedUpdate() 
{ 
} 

void OnTriggerEnter2D(Collider2D other){ 
    Debug.Log(gameObject.name + " was hit by " + other.gameObject.name); 

    if (other.gameObject.name == "New_Player") 
    { 
     canHearPlayer = true; 
     force = 3f; 
    } 
} 

void onTriggerExit2D(Collider2D other) 
{ 
    if (other.gameObject.name == "New_Player") 
    { 
     canHearPlayer = false; 
     Debug.LogWarning (canHearPlayer + "Trigger has been exited"); 
    } 
} 
+1

錯字? OnTriggerExit2D需要一個大寫字母「O」 –

回答

4

該函數不起作用,因爲字母「o」是小寫字母而不是大寫字母。

改變這一行:

void onTriggerExit2D(Collider2D other) 

到:

void OnTriggerExit2D(Collider2D other) 
+1

謝謝我只有第二雙眼睛,他們爲我注意到它,甚至使用它來查看我是否拼寫正確,並且仍然沒有聽到「O」哈哈感謝您的回覆! – user3294218