This code shows how I find the single closest object (e.g. collider) within a group of hit colliders. I'd like to find the closest 4 objects. (this number might increase)
Collider closestCollider = null;
float closestColliderDistance = float.PositiveInfinity;
foreach (Collider collider in hitColliders)
{
float distance = Vector3.Distance(collider.transform.position, transform.position);
if (distance < closestColliderDistance)
{
closestColliderDistance = distance;
closestCollider = collider;
}
}
if (closestCollider != null)
{
// handle it
}
So I want to return a group (e.g. an array) of the 4 closest objects to transform.position. This doesn't need to be sorted. It can be based on this code or completely new code.
Maybe it could involve some kind of list that is sorted by distance then get the top X items. Maybe linq? (not sure how to use it)
Well yes simply sort by distance and than take X items
var xClosestItems = hitColliders
.OrderBy(o => (o.transform.position - transform.position).sqrMagnitude)
.Take(X)
.ToList();
where X
means up to the desired amount - can be less if hitColliders
doesn't have enough items.
Some notes
comparing sqrMagnitude
is more efficient than Distance
which has to take the square root. As distance is always positive we can go by a² > b² => a > b
currently this takes the distance by pivot point (transform position). This might differ from the gravitational center collider.bounds.center
generally ToList
is a bit more efficient than ToArray
in exchange to bigger memory consumption as it doesn't have to shorten the underlying array to match the exact length but simply goes in 2eX increments