我有这个函数从我的播放器返回我的近物体。它部分工作正常,但有时不返回正确的对象。还有其他方法吗?
GameObject GetNearPoint()
{
GameObject minDistObject = null;
float minDist = Mathf.Infinity;
for (var i = 0; i < gameObject.transform.childCount; i++)
{
GameObject childGo = gameObject.transform.GetChild(i).gameObject;
float dist = Vector3.Distance(childGo.transform.position, VRPlayer.transform.position);
if (dist == 0)
{
Debug.Log("equal distnace :: " + childGo.name);
continue;
}
if (dist < minDist)
{
minDistObject = childGo;
minDist = dist;
}
}
Debug.Log("mindistance: " + minDistObject.name, minDistObject.gameObject);
return minDistObject;
}