敌人AI无法工作

时间:2015-10-17 19:11:07

标签: c# unity3d artificial-intelligence unity5

我最近决定编写自己的人工智能,让警察在监狱游戏中向前和向后走。重要的是要注意他向左走,然后重复。我有一个while循环来设置他的坐标并在公共虚函数之间切换,但它似乎没有工作:守卫只是朝着一个方向走。

还有其他错误,所以我需要一位能够解决这个问题的专家,如果你得到了我想做的事情。这是代码:

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 个答案:

答案 0 :(得分:0)

你如何致电move()?需要使用StartCoroutine(move());调用它来枚举Unity的WaitForSeconds。有关类似问题,请参阅this question

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

此外,您是否忘记增加i