这段代码只从一开始计算对象,而不计算示例化对象
void Update()
{
FindClosestEnemy();
}
public void FindClosestEnemy()
{
float distanceToClosestEnemy = Mathf.Infinity;
Enemy closestEnemy = null;
Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();
foreach (Enemy currentEnemy in allEnemies)
{
float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
if (distanceToEnemy < distanceToClosestEnemy)
{
distanceToClosestEnemy = distanceToEnemy;
closestEnemy = currentEnemy;
}
}
我尝试了其他最近的敌人探测器,但不知何故,只有这个工作到目前为止
1条答案
按热度按时间gfttwv5a1#
我发现了问题所在示例化对象时,z位置发生了变化,因此无法检测到对象