从列表中获取靠近玩家位置的GameObject

时间:2016-12-06 14:05:39

标签: c# unity3d

我有这个函数从我的播放器返回我的近物体。它部分工作正常,但有时不返回正确的对象。还有其他方法吗?

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;
    }

0 个答案:

没有答案