【问题标题】:Unity2D: Enemy doesn't follow Player when in its radiusUnity2D:敌人在其半径内时不会跟随玩家
【发布时间】:2019-10-04 09:36:52
【问题描述】:

大家好,我最近遇到了一个我似乎无法解决的问题。

精灵应该在其半径内没有任何东西时(就像它那样)四处漫游,但是如果玩家靠近它,精灵理论上应该向它移动并停止漫游。

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

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

【问题讨论】:

  • 试试hit.gameObject.CompareTag("Player")
  • 不幸的是,它什么也没做。但是现在它实际上显示它在数组中看到了一个标签,因为hits 正在显示它的内容
  • transform.position 的位置是否在精灵的左上角?如果是这样,它是否在您的可碰撞播放器的 10 像素范围内?如果您将半径扩大到 20 或 50 或更多只是为了测试呢?
  • 你的代码不会改变行为,除了循环和调试说“看到”如果你在一个半径......它总是运行followplayer,它总是运行randommove......
  • 我把它改成了 100 还是没有。

标签: c# unity3d collision


【解决方案1】:

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

试试这个:

void Update()
{
    hits = Physics2D.OverlapCircleAll(transform.position, 10);
    //...
}

你也可以考虑给敌人附加一个触发对撞机,它会在玩家进入/退出触发时发送通知,而不是依赖OverlapCircleAll()

void OnTriggerEnter2D(Collider2D col)
{
    if(col.gameObject.tag == "Player"){
        roam = false;
    }
}

void OnTriggerExit2D(Collider2D col)
{
    if(col.gameObject.tag == "Player"){
        roam = true;
    }
}

【讨论】:

  • 很好地抓住了本地 hits 变量
  • 谢谢。不幸的是,这并没有奏效。我也尝试了OnTriggerEnter2D,但由于某种原因它本身不起作用:/
  • 如果OnTriggerEnter2D 没有注册,最好查看Unity's Collision action matrix 并确认您的对撞机设置将发送触发消息。还要确保您的collision layers 设置正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多