敌人 AI 不动

Enemy AI not moving

我最近对在 Unity 中创建游戏产生了兴趣,但是我不知道该语言是如何工作的 (C#)。

我目前做的这个测试"game"是让敌人自己四处游荡。我创建了一个我希望能起作用的代码,但不幸的是敌人不会移动。我不确定它有什么问题,因为我没有收到任何错误,希望有人能提供帮助。所有 Debug.Log 值都正常返回,但敌人没有正常移动。

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
    private void Start(){
        findPosition();
    }


    //Variables
    public Transform enemy;

    private Vector2 position;

    /* not necessary
    private float maxPosX;
    private float maxPosY;
    private float maxNegX;
    private float maxNegY;
    */

    public float wanderSpeed = 3;

    private float totalWalkDistanceUp;
    private float totalWalkDistanceDown;
    private float totalWalkDistanceLeft;
    private float totalWalkDistanceRight;




    private void findPosition(){
        position.x = enemy.position.x;
        position.y = enemy.position.y;

        /* not neccessary
        maxPosX = position.x + 5;
        maxNegX = position.x - 5;
        maxPosY = position.y + 5;
        maxNegY = position.y -5;
        */

        Debug.Log ("Enemy position: " + position);
        //Debug.Log ("Max positive 'x' position: " + maxPosX + ". Max positive 'y' position: " + maxPosY + ". Max negative 'x' position: " + maxNegX + ". Max negative 'y' position: " + maxNegY);
        Debug.Log ("Finished!");

        enemyWalk();
    }

    private void enemyWalk(){
        //Distance Generator
        float walkDistance = Random.Range (1f, 5f);
        totalWalkDistanceUp = walkDistance + position.x;
        totalWalkDistanceDown = position.x - walkDistance;
        totalWalkDistanceRight = walkDistance + position.y;
        totalWalkDistanceLeft = position.y - walkDistance;
        Debug.Log ("Distance generated up: " + totalWalkDistanceUp + ". Distance generated down: " + totalWalkDistanceDown + ". Distance generated left: " + totalWalkDistanceLeft + ". Distance generated right: " + totalWalkDistanceRight);

        //Direction generator
        int directionGenerator = Random.Range (1, 4);

        if(directionGenerator == 1){ //up
            enemy.transform.Translate(new Vector2(totalWalkDistanceUp, 0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: up");
        }
        else if(directionGenerator == 2){ //down
            enemy.transform.Translate(new Vector2(totalWalkDistanceDown, 0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: down");
        }
        else if(directionGenerator == 3){ //left
            enemy.transform.Translate (new Vector2(0, totalWalkDistanceLeft) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: left");
        }
        else if(directionGenerator == 4){ //right
            enemy.transform.Translate(new Vector2(0, totalWalkDistanceRight) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: right");
        }
    }

}

完整代码:http://www.pastebucket.com/75289

你的 enemyWalk 方法乍一看没问题,但我看到的问题是它只被调用了一次。

您可能想要添加使用 MonoBehaviour Update 方法,它充当组件的游戏循环。 Update 每帧调用一次,因此在 Update 中调用 enemyWalk() 应该会看到你的角色移动:

private void Update () {
    enemyWalk();
}