如何在AI上检测范围内的特定对象?

时间:2017-06-30 09:10:03

标签: c# artificial-intelligence unity5 detection

所以我正在复制游戏" Snake Pit"来自频谱。你可以像你的玩家一样移动并且有多个AI控制的蛇。而且我想弄清楚如何让蛇头移动到你的位置,当它们就在你旁边时。这是我想要实现的代码。

public class SnakeController : MonoBehaviour {

public int maxSize;
public int currentSize;
public GameObject snakePrefab;
public Snake Head;
public Snake Tail;
public Vector2 nextPos;
public int NESW;
int Random;
float lineTimer;
int NESWTemp;

// Use this for initialization
void Start () {
    InvokeRepeating("TimerInvoke", 0, .3f);
    lineTimer = UnityEngine.Random.Range(0, 2.5f);
    currentSize = 1;
}

// Update is called once per frame
void Update () {
    lineTimer -= Time.deltaTime;
    if (lineTimer <= 0)
    {
        ComChangeD();
        lineTimer = UnityEngine.Random.Range(0, 2.5f);
    }
}

void TimerInvoke()
{
    Movement();
    if(currentSize >= maxSize)
    {
        TailFunction();
    }
    else
    {
        currentSize++;
    }
}

void Movement()
{
    GameObject temp;
    nextPos = Head.transform.position;

    if(nextPos.y > 3.22)
    {
        NESW = 2;
    }

    else if (nextPos.y < -4.42)
    {
        NESW = 0;
    }

    else if (nextPos.x < -8.2)
    {
        NESW = 1;
    }

    else if (nextPos.x > 7.7)
    {
        NESW = 3;
    }

    switch (NESW)
    {
        case 0:
            nextPos = new Vector2(nextPos.x, nextPos.y + 0.32f);
            break;
        case 1:
            nextPos = new Vector2(nextPos.x + 0.32f, nextPos.y);
            break;
        case 2:
            nextPos = new Vector2(nextPos.x, nextPos.y - 0.32f);
            break;
        case 3:
            nextPos = new Vector2(nextPos.x - 0.32f, nextPos.y);
            break;
    }

    temp = (GameObject)Instantiate(snakePrefab, nextPos, transform.rotation);
    Head.SetNext(temp.GetComponent<Snake>());
    Head = temp.GetComponent<Snake>();
    return;
}

void ComChangeD()
{
    NESWTemp = UnityEngine.Random.Range(0, 3);
    if (NESW == 0)
    {
        switch (NESWTemp)
        {
            case 0:
                NESW = 1;
                break;
            case 1:
                NESW = 2;
                break;
            case 2:
                NESW = 3;
                break;
        }
    }
    else if (NESW == 1)
    {
        switch (NESWTemp)
        {
            case 0:
                NESW = 0;
                break;
            case 1:
                NESW = 2;
                break;
            case 2:
                NESW = 3;
                break;
        }
    }
    else if (NESW == 2)
    {
        switch (NESWTemp)
        {
            case 0:
                NESW = 0;
                break;
            case 1:
                NESW = 1;
                break;
            case 2:
                NESW = 3;
                break;
        }
    }
    else if (NESW == 3)
    {
        switch (NESWTemp)
        {
            case 0:
                NESW = 0;
                break;
            case 1:
                NESW = 1;
                break;
            case 2:
                NESW = 2;
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在蛇头下创建空的游戏对象并称之为'DetectRange'或类似的东西。然后将它在检查器中的宽度和高度设置为您希望蛇跟随玩家的范围的宽度和高度。因此,如果玩家进入该范围,蛇将开始跟随。然后你可以将玩家对象的标签设置为“玩家”或类似的东西(位于检查员的顶部)。然后将脚本附加到DetectRange并在其中添加:

public GameObject playerObject; //Drag player gameobject in inspector
private float snakeSpeed = 1.0f;


void OnTriggerStay(Collider col)
{
    if(col.gameObject.Tag == "Player") //Check if player entered collider
    {
        playerObject.transform.position = Vector3.MoveTowards(playerObject.transform.position, col.gameObject.position, snakeSpeed);
    }
}