我正在做基于回合的移动,如果射线被击中,我希望敌人Ai朝玩家移动。我的光线下降了,但我的动作有麻烦。我首先要做的是确定Ai相对于玩家作为原点的象限,然后我要根据玩家与敌人之间的界线确定x和y分量是什么。

    GameObject player_tran = GameObject.Find ("player");
    if (transform.position.x > player_tran.transform.position.x && transform.position.y < player_tran.transform.position.y) {
        //4th quad if (x is bigger than y) move (left) else move (up)


    }

    if (transform.position.x < player_tran.transform.position.x && transform.position.y < player_tran.transform.position.y) {
        //3rd quad if (x is bigger than y) move (right) else move (up)

    }

    if (transform.position.x < player_tran.transform.position.x && transform.position.y > player_tran.transform.position.y) {
        //2nd quad if (x is bigger than y) move (right) else move (down)

    }
    if (transform.position.x > player_tran.transform.position.x && transform.position.y > player_tran.transform.position.y) {
        //1st quad if (x is bigger than y) move (left) else move (down)

    }
    else {
    //if they are both equal random moement
    }


`

例如,如果在第一个四边形中,并且x分量较大,我希望敌人向左移动,否则将向下移动。

编辑:我在这里试图做的是创建一个直角三角形,敌人和玩家之间的距离是斜边。然后,我将x边与y边进行比较,看看哪个更大以决定运动。

最佳答案

由于在这种情况下这两个维度是可分离的,因此您可以执行此操作。

GameObject player_tran = GameObject.Find ("player");
double dx = 0;
double dy = 0;
if (transform.position.x > player_tran.transform.position.x)
    dx = -1;
} else {
    dx = 1;
}

if (transform.position.y > player_tran.transform.position.y) {
    dy = -1;
} else {
    dy = 1;
}
transform.position += new Vector(dx, dy, 0);

关于c# - 基于2D矢量的Ai运动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34327889/

10-13 06:57