基于2D矢量的Ai运动

时间:2015-12-17 06:22:05

标签: c# unity3d

我正在进行基于转身的动作,我希望敌人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侧进行比较,看看哪个更大以确定运动。

2 个答案:

答案 0 :(得分:0)

所以你希望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.

答案 1 :(得分:0)

由于在这种情况下两个维度是可分的,你可以这样做。

Foo<string>