我最近决定编写自己的 AI 来让监狱游戏中的守卫前后走动。重要的是要注意他先右后左再重复。我有一个 while 循环来设置他的坐标并在公共 void 函数之间切换,但它似乎不起作用:守卫只是永远朝一个方向走。
还有其他错误,所以如果你得到我想要做的事情,我需要一位专家来解决这个问题。这是代码:
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());
}
}