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

问题描述 投票:0回答:1

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

[精灵应该在其半径内没有任何东西的情况下漫游(确实如此),但是,如果玩家靠近它,则该精灵理论上应该朝它移动并停止漫游。

精灵不跟随播放器,甚至看不到它的标签,因为我什至看不到“ Collider2D []命中”的内容。

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

c# unity3d collision
1个回答
0
投票

似乎您在每次更新期间都声明了一个新的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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.