private GameObject FindNearest(float x, float y, float z, ref double shortestDistance) { GameObject closest = null; //Reached a root node, check its objects if (childNodes == null) { searched = true; //We're a root node, check the objects we have foreach (GameObject obj in objects) { double distance = Mathf.Sqrt( Mathf.Pow(x - obj.transform.position.x, 2.0f) + Mathf.Pow(y - obj.transform.position.y, 2.0f) + Mathf.Pow(z - obj.transform.position.z, 2.0f)); if ((distance > shortestDistance)) { continue; } shortestDistance = distance; closest = obj; } return(closest); } //Keep stepping into the children until we reach a root (above) foreach (Octree child in childNodes) { double childDistance = GeneralUtils.DistanceToBoundsEdge(child.nodeBounds, x, y, z); if (childDistance > shortestDistance) { continue; } GameObject tmpObject = child.FindNearest(x, y, z, ref shortestDistance); if (tmpObject != null) { closest = tmpObject; } } return(closest); }