首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在运行时检查FindGameObjectsWithTag?

如何在运行时检查FindGameObjectsWithTag?
EN

Stack Overflow用户
提问于 2020-06-02 13:23:18
回答 1查看 67关注 0票数 0

我遇到了我的敌人NavMeshAgent AI的麻烦,它首先搜索带有“防御”标记的GameObjects,然后根据我的敌人离防御有多近来设置第一个目的地,当防御被摧毁时,数组值增加一个,然后将目标更改为下一个防御。

我的问题是,我的玩家可以在游戏中创建(例如)新的防御,当我的所有防御被摧毁时,我的敌人会变得疯狂,所以,我需要一种方法把那些新的防御添加到我的数组中,下面是我的敌人脚本。

代码语言:javascript
运行
复制
 public class NavMeshEnemy : MonoBehaviour
 {
     [Header("References", order = 0)]
     [SerializeField] NavMeshAgent enemyAgent;
     [Space(10)]
     [SerializeField] GameObject[] destinations;
     [Space(10)]
     [SerializeField] float distanceObjects;
     [SerializeField] int arrayElements = 0;

     void Awake()
     {
         enemyAgent = GetComponent<NavMeshAgent>();
         destinations = GameObject.FindGameObjectsWithTag("Defenses");
     }

     void Start()
     {
         destinations = destinations.OrderBy((d) => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();
     }

     void Update()
     {
         distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);

         enemyAgent.destination = destinations[arrayElements].transform.position;

         CheckArray();
     }

     void CheckArray()
     {
         if (destinations[arrayElements].gameObject.activeInHierarchy == false)
         {
             if (arrayElements < destinations.Length - 1)
             {
                 arrayElements++;
                 enemyAgent.destination = destinations[arrayElements].transform.position;
             }
             else
             {
                 arrayElements = 0;
             }
         }
     }
 }

(谢谢各位阅读!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-02 13:39:19

我宁愿在您的预置文件上实现一个组件,其中包含一个static列表,例如。

代码语言:javascript
运行
复制
public class NavMeshDestination : MonoBehaviour
{
    public static HashSet<Transform> existingDestinations = new HashSet<Transform>();

    private void Awake()
    {
       if(!existingDestinations.Contains(this)) existingDestinations.Add(this);
    }

    private void OnDestroy()
    {
        if(existingDestinations.Contains(this)) existingDestinations.Remove(this);
    }
}

然后甚至不要按标签走,而只是简单地做

代码语言:javascript
运行
复制
public class NavMeshEnemy : MonoBehaviour
{
    [Header("References", order = 0)]
    [SerializeField] NavMeshAgent enemyAgent;
    [Space(10)]
    [SerializeField] float distanceObjects;
    [SerializeField] int arrayElements = 0;

    void Awake()
    {
        if(!enemyAgent) enemyAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        destinations = NavMeshDestination.existingDestinations.OrderBy(d => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();

        distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);

        enemyAgent.destination = destinations[arrayElements].transform.position;

        CheckArray();
    }

    void CheckArray()
    {
        if (destinations[arrayElements].gameObject.activeInHierarchy == false)
        { 
            if (arrayElements < destinations.Length - 1)
            {
                arrayElements++;
                enemyAgent.destination = destinos[arrayElements].transform.position;
            }
            else
            {
                arrayElements = 0;
            }
        }
    }
}

但是,请注意,这仍然无法解决没有可用目的地的问题。因此,如果数组为空,则应该停止执行,如

代码语言:javascript
运行
复制
if(NavMeshDestination.existingDestinations.Count == 0)
{
    // Stop the enemyAgent
    return;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62153016

复制
相关文章

相似问题

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