任何人都可以帮助我吗?我不知道什么是错的,但是当我有超过2个目标时,我的脚本无效。
scala> val finalAns=if(matchedArrStr.size>0) matchedArrStr.head else "Match not found"
finalAns: String = Abercrombie Kids
答案 0 :(得分:2)
您没有将循环的当前GameObject与最接近的GameObject进行比较。
您应该执行以下操作:
GameObject[] cores;
GameObject closest_core = null;
float dist, minDist = 999999f;
cores = GameObject.FindGameObjectsWithTag ("bldg_core");
for (int x = 0; x < cores.Length; x++) {
dist = distanceToPlayer (cores [x]);
if(dist < minDist){
minDist = dist;
closest_core = cores[x];
}
}
答案 1 :(得分: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];
}