首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检测范围内的特定对象,在人工智能上?

如何检测范围内的特定对象,在人工智能上?
EN

Stack Overflow用户
提问于 2017-06-30 09:10:02
回答 1查看 76关注 0票数 0

所以我正在从光谱中复制游戏“蛇坑”。你可以像你的玩家一样移动,还有多条人工智能控制的蛇。当蛇头就在你旁边的时候,我想弄清楚如何让它们移动到你的位置。下面是我想要将其实现到的代码。

代码语言:javascript
运行
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-30 12:09:02

在蛇头下创建空游戏对象,并将其称为“DetectRange”或类似的东西。然后将它的宽度和高度设置为您希望您的蛇跟随玩家的范围的宽度和高度。因此,如果玩家进入这个范围,蛇将开始跟随。然后,您可以将player对象的标记设置为"Player“或类似的东西(在检查器的顶部)。然后将脚本附加到DetectRange中,然后在其中执行以下操作:

代码语言:javascript
运行
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44842758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档