c# - 统一寻找最近的目标
问题描述
谁能帮我?我不知道出了什么问题,但是当我有超过 2 个目标时,我的脚本无法正常工作。
GameObject[] cores;
GameObject closest_core = null;
cores = GameObject.FindGameObjectsWithTag ("bldg_core");
if (cores.Length != 1)
for (int x = 0; x < cores.Length - 1; x++)
{
if (distanceToPlayer (cores[x + 1]) < distanceToPlayer (cores[x]))
closest_core = cores [x + 1];
}
解决方案
您需要在循环时存储最近的核心。目前,您只是将阵列中的当前物距与下一个物距进行比较。
GameObject[] cores
GameObject closest_core = null;
cores = GameObject.FindGameObjectsWithTag ("bldg_core");
if (cores.Length != 1)
for (int x = 0; x < cores.Length - 1; x++) {
if (distanceToPlayer (cores [x + 1]) < distanceToPlayer(closest_core))
closest_core = cores [x+1];
}
推荐阅读
- spring-boot - 使用 Spring 在 ArangoDB 中启用审计
- python - 博客 - 查找上次更新时间
- reactjs - 子组件参考给出错误
- sql-server - InterSystems 缓存数据库和 SSMS 集成服务目录
- java - 使用 Apache poi 插入具有不同列号的新表行
- excel - 如果单元格包含“拾取”,如何在另一个工作表中返回列值
- javascript - 从光滑的滑块拖动外部事件到 FullCalendar
- excel - Greater Then 使用时间公式 EXCEL 中的假数字
- css - Bulma 全高布局,中间有可滚动区域
- hyperledger-fabric - 在 Hyperledger Fabric CA 中注册和注册证书有什么区别