# Finding objects in the vicinity of a point

Why isn’t there a function to find objects by their coordinates or in a portions of the gamespace without using raycasting? That is so a desireable function!
Something like function FindGameObjectsInVicinityOf(x,y,z,rx,ry,rz,[objecttype]) wich is to find objects in coordinates x+/-rx, y+/-ry, z+/-rz.

Such function could be very useful, for sure. Currently, a reasonable solution is to use Physics.OverlapSphere: it returns a list of all colliders touching or inside a given sphere (see example in [Rigidbody.AddExplosionForce][1]). The problem here is that only the collider bounding volume is taken into account - since the bounding volume is axis aligned, a long object may be added to the list even if no actual parts of it are touching the sphere. In order to get an effect similar to your suggested function, OverlapSphere may be used as a first approximation, and the distance of each object to the center point must be compared to the allowed range - farther objects should be discarded, like this:

``````function FindGameObjectsInsideRange (center: Vector3, radius: float): GameObject[] {
var cols: Collider[] = Physics.OverlapSphere(center, radius);
var q = cols.length; // q = how many colliders were found
var lastCol: int = 0;
// pack the objects inside range in the beginning of array cols:
for (var i = 0; i < q; i++){
var dist = Vector3.Distance(center, cols*.transform.position);*
``````

if (dist <= radius){ // if object inside range…
cols[lastCol++] = cols*; // save it at the next available position*
}
}
// create return array:
var gos: GameObject[] = new GameObject[lastCol];
// copy the game objects inside range to it:
for (i = 0; i < lastCol; i++){
gos = cols*.gameObject;*
}
return gos; // return the GameObject array
}
This isn’t the same, of course: only objects that have colliders will be found, and the range isn’t defined per axis - but this usually isn’t important: most objects in a 3D game aren’t aligned to the world axes.