Maintaing an array for distance checks or using physics.overlapsphere?

So i’m optimizing a mobile game at the minute, its a pretty simple game but it heavily uses boids and needs to identify when things are close. At the minute i am using overlapsphere, this seemed like the sensible approach. But i have read on alot of websites/forum posts that maintaining an array of distances of each object to one-another is actually faster and i just cant understand why as that is a ridiculous number of distance calculations.

Can anyone explain why this is may be faster before i test it tomorrow? This is one of the forum threads i mentioned but its way too old to bump =D

OverlapSphere() does a lot more than distance calculations. At the very least it is checking against the bounding volume of objects. From the reference:

NOTE: Currently this only checks
against the bounding volumes of the
colliders not against the actual

But at least in quick tests, it is seems to be checking against the actual colliders. For example using this setup where the sphere has the same radius and position as the OverlapSphere():


The OverlapShpere() did not report the arch until the sphere touched the arch indicating (contrary to the reference) that it is checking against colliders.

So on one hand OverlapSphere() is doing a lot more than a simple distance check, but on the other hand it is C++ code (not C#), and I wouldn’t be surprised if Unity had some sophisticated data structures tracking objects. And your results may be situational. For example, one may perform better for a diverse spread of objects, and the other might work better for closely packed objects.

When you run your test, be sure to do it in a build, not in the editor.