Grab all objects within a location/position

Howdy folks,

So my question today is what’s the most efficient way to grab all GameObjects at a certain position, let us say 1x1x1, and toss them into a list. My problem is that despite searching, I couldn’t fund a procedure/function which would grab objects at a certain position.

At the moment, I simply just grab all GameObjects with a certain tag in the world and toss them into a list. And then search for their position. But I feel that’s inefficient when I’m going to be dealing with a ton of these objects and it still isn’t necessarily what I want.

I don’t think there would be a huge difference in the ways you mentioned anyway. As I see it you would have to test each object/tagged objects you want to test against your given position anyway.

Also, keep in mind that with floating point values as the positions, it will be difficult to test for an exact position at times depending on your positioning method, so I suggest getting a vector from object position to test position and test its length against a small value.

If they have colliders attached to them then do a SphereCast or OverlapSphere

I’ve been thinking about this issue as well, and probably something like an octree or kdtree would speed things up.

An idea I’ve been toying with is to use triggers, say your 1x1x1 box, and the objects inside the trigger will have the ontrigger message sent to them.

Have you tried out just building that list? The speed hit may not be that much of a problem, and it is much simpler to implement.

Kelso that’s perfect for what I need! Cheers!

Physics.OverlapSphere might be your friend