[我正在尝试根据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
我的问题是,如何根据我的enemyAgent和arrayElement索引之间的距离对元素进行排序?
此答案可能会帮助:
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