敌人AI不动

时间:2015-01-27 03:30:20

标签: c# unity3d

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

我正在制作的当前测试“游戏”是让敌人自己四处游荡。我创建了一个我希望可以工作的代码,但遗憾的是敌人不会移动。我不确定它有什么问题,因为我没有收到任何错误,希望有人可以提供帮助。所有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

1 个答案:

答案 0 :(得分:0)

您的enemyWalk方法一目了然,但我看到的问题是它只被调用一次。

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

private void Update () {
    enemyWalk();
}