首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Unity2D:敌人在半径内不会跟随玩家

Unity2D:敌人在半径内不会跟随玩家
EN

Stack Overflow用户
提问于 2019-10-04 07:14:01
回答 1查看 325关注 0票数 1

嘿,伙计们,我最近一直有一个问题,我似乎解决不了。

当半径内没有任何东西时,精灵应该四处漫游(就像它所做的那样),然而,如果玩家移动到它附近,理论上精灵应该向它移动并停止漫游。

精灵不会跟随玩家,甚至看不到它的标签,因为我甚至看不到“Collider2 D[] hits”的内容。

代码语言:javascript
复制
using System.Collections.Generic;
using UnityEngine;

public class FireCultist : MonoBehaviour
{
    public float moveTimeSeconds;            //Time it will take object to move, in seconds.

    private float xMax = 10.0f; // The boundaries of the spawn area
    private float yMax = 10.0f;
    private float xMin = -10.0f; // The boundaries of the spawn area
    private float yMin = -10.0f;

    public int xDistanceRange; // The max distance you can move at one time
    public int yDistanceRange;

    private BoxCollider2D boxCollider;         //The BoxCollider2D component attached to this object.
    private Rigidbody2D rb2D;                //The Rigidbody2D component attached to this object.
    private float inverseMoveTime;            //Used to make movement more efficient.
    public Vector2 start;
    public Vector2 end;
    public bool roam = true;
    public Collider2D[] hits;

    void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
        rb2D = GetComponent<Rigidbody2D>();

        inverseMoveTime = 1f / moveTimeSeconds;
        InvokeRepeating("RandomMove", 0.0f, 5.0f);

    }

    void Update()
    {
        Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, 10); // returns all colliders within radius of enemy
        int i = 0;
        while(hits.Length > i)
        {
            Debug.Log("Sees");
            Debug.Log(hits[i]);
            i++;
        }
        followPlayer();

        if (roam)
        {
            Debug.Log("Roam");
            transform.position = Vector2.MoveTowards(transform.position, end, inverseMoveTime * Time.deltaTime); //Random move to new position
        }
    }

    public void followPlayer()
    {

        foreach (Collider2D hit in hits)
        {

            if (hit.tag == "Player") // if the player is within a radius
            {
                Debug.Log("Chasing Player");
                transform.position = Vector2.MoveTowards(transform.position, GameObject.Find("Prefabs/Player").GetComponent<Transform>().position, inverseMoveTime * Time.deltaTime); // chase player
                roam = false;
            }
            else
            {
                Debug.Log("Continues");
                roam = true; // Continue RandomMove()
            }
        }
    }

    public void RandomMove() // gets random coordinates near enemy and moves there
    {
        float xDistance = Random.Range(-xDistanceRange, xDistanceRange); // check
        float yDistance = Random.Range(-yDistanceRange, yDistanceRange);// check

        if (xDistance < xMax && xDistance > xMin && yDistance < yMax && yDistance > yMin && roam == true) // If within boundaries of walking space
        {
            start = transform.position;
            end = start + new Vector2(xDistance, yDistance);

            Debug.Log("Roaming From : " + start + " To : " + end);
        }
    }
}

漫游算法可以工作,但是对标签检测不是很确定。

The script belongs to this enemy object

Player Object Properties

EN

回答 1

Stack Overflow用户

发布于 2019-10-04 09:29:35

看起来您在每次更新时都要声明一个新的hits变量,而不是使用您的类级变量。这意味着followPlayer()中的变量永远不会被实例化,信息也不会在两个方法之间传递。

试试这个:

代码语言:javascript
复制
void Update()
{
    hits = Physics2D.OverlapCircleAll(transform.position, 10);
    //...
}

你也可以考虑给敌人安装一个触发器碰撞器,当玩家进入/退出触发器时,它会发送通知,而不是依赖于OverlapCircleAll()

代码语言:javascript
复制
void OnTriggerEnter2D(Collider2D col)
{
    if(col.gameObject.tag == "Player"){
        roam = false;
    }
}

void OnTriggerExit2D(Collider2D col)
{
    if(col.gameObject.tag == "Player"){
        roam = true;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58228022

复制
相关文章

相似问题

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