Unity3D-按距离[重复]对GameObject数组进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D-按距离[重复]对GameObject数组进行排序相关的知识,希望对你有一定的参考价值。

[我正在尝试根据NavMesh代理与标记为“防御”的游戏对象之间的距离对GameObjects数组进行排序。

public class Enemy : MonoBehaviour
{
    [Header("Properties")]
    [SerializeField] GameObject[] destinations;
    [SerializeField] int arrayElements = 0;
    [SerializeField] float objectsDistance;
    [Space(10)]
    [SerializeField] NavMeshAgent enemyAgent;

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

    void Update()
    {
        enemyAgent.destination = destinations[arrayElements].transform.position;
    }
}

Array elements in Inspector, as the order they appear in Hierarchy

我的问题是,如何根据我的enemyAgentarrayElement索引之间的距离对元素进行排序?

答案

此答案可能会帮助:

How to Sort a List<T> by a property in the object

以下是基于该答案的摘录:

var orderedDestinations = destinations.OrderBy(d => Vector3.distance(d.transform.position, enemyAgent.transform.position)

PS:这可能不是很出色:p

以上是关于Unity3D-按距离[重复]对GameObject数组进行排序的主要内容,如果未能解决你的问题,请参考以下文章