简体   繁体   English

基于2D矢量的Ai运动

[英]2D Vector based Ai movement

Im doing turned based movement and I want the enemy Ai will move towards the player if the raycast is hit. 我正在做基于回合的移动,如果射线被击中,我希望敌人Ai朝玩家移动。 I have the raycast down but Im having trouble with the movement. 我的光线下降了,但我的动作有麻烦。 What I first do is determine what quadrant the Ai is in relative to the player being the origin then I want to determine, from the line draw between the player and the enemy, if what the x and y components. 我首先要做的是确定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
    }

` `

for example if in the 1st quad and the x component is bigger I want the enemy to move left otherwize down. 例如,如果在第一个四边形中,并且x分量较大,我希望敌人向左移动,否则将向下移动。

edit: what I was trying to do here is create a right triangle where the distance between the enemy and the player is the hypotenuse. 编辑:我在这里试图做的是创建一个直角三角形,敌人和玩家之间的距离是斜边。 Then I would compare the x side with the y side and see which is bigger to determine movement. 然后,我将x边与y边进行比较,看看哪个更大以决定运动。

So you want the ai to make an axis aligned movement toward the player in the (axis aligned) direction that it has the greater absolute displacement from the player (ties go vertically)? 因此,您是否要让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.

As the two dimensions are separable in this case you can just do this. 由于在这种情况下这两个维度是可分离的,因此您可以执行此操作。

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