我做转身的移动,我想敌人艾将移动到球员,如果光线投射被击中。我的光线下降了,但我在运动上遇到了困难。我首先要做的是确定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边,看看哪个更大来决定运动。
发布于 2015-12-17 07:25:12
在这种情况下,由于这两个维度是可分离的,所以您可以这样做。
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);发布于 2015-12-17 07:20:17
所以你想让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.https://stackoverflow.com/questions/34327889
复制相似问题