繁体   English   中英

基于2D矢量的Ai运动

[英]2D Vector based Ai movement

我正在做基于回合的移动,如果射线被击中,我希望敌人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边进行比较,看看哪个更大以决定运动。

因此,您是否要让ai沿播放器的轴向对齐移动,使其相对播放器的绝对位移更大(关系垂直)?

//shorter variables for my own sanity
var dx = player.x - ai.x;
var dy = player.y - ai.y;
if(Math.Abs(dx) > Math.Abs(dy){
    //assume that move is previously declared/initialised
    move.x = amount_to_move * Math.Sign(dx);
    move.y = 0;
}else{
    move.y = amount_to_move * Math.Sign(dy)
    move.x = 0;
}
//implementation of amount to move, and not going too far when already very close, left to the reader.

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

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM