2015-10-17 91 views
1

我最近決定編寫自己的AI來在監獄遊戲中向前走和向後走。重要的是要注意他走到左邊然後重複。我有一個while循環來設置他的座標並在公共無效函數之間切換,但它似乎並不奏效:守衛只是永遠朝着一個方向走。敵人AI無法正常工作

還有其他的錯誤,所以我需要一個專家誰可以解決這個問題,如果你得到我想要做的。這裏是代碼:

using UnityEngine; 
using System.Collections; 

public class enemyAI : MonoBehaviour 
{ 

private float rightDistance; 
private float leftDistance; 

public void moveRight(float rightDistance) 
{ 
    rightDistance = 0.02f; 
    transform.position = new Vector3(transform.position.x + rightDistance, transform.position.y, transform.position.z); 
    yield return new WaitForSeconds(1); 
} 

public void moveLeft(float leftDistance) 
{ 
    leftDistance = 0.02f; 
    transform.position = new Vector3(transform.position.x - leftDistance, transform.position.y, transform.position.z); 
    yield return new WaitForSeconds(1); 
} 



public IEnumerator move() 
{ 
    int i = 0; 
    while (i < 10) 
    { 
     moveRight(); 
     yield return new WaitForSeconds(1); 
     moveLeft(); 
    } 
} 


// Update is called once per frame 
void Update() 
{ 
    StartCoroutine(move()); 
} 

} 
+1

我不知道Unity,但我懷疑包含「收益回報」的「無效」方法。 –

+0

何時「我」不少於10?返回後調用左移,因此不會調用。 – Catwood

+0

'StartCoroutine(move());'每次更新都被調用?我會重新開始,也許根本不使用協程。 – Catwood

回答

0

你怎麼稱呼move()?需要撥打StartCoroutine(move());來枚舉Unity的WaitForSeconds。有關類似問題,請參見this question

while (i < 10) 
{ 
    moveRight(); 
    yield return new WaitForSeconds(1); 
    moveLeft(); 
} 

此外,你忘了增加i