Does adding a Layermask improve efficieny of Physics.OverlapSphere?


Can anyone tell me if adding a layermask reduces the overhead of physics.overlapshere significantly? In this case, about 90% of objects detected are irrelevant. I have added a layermask, but it’s difficult to say if it’s more efficient.

Also, is it the case that the nearest object will be in position 0 in the returned array?

Thanks for reading

It will definitely have an efficiency improvement, but by how much depends on number of objects and computer specifications. You will not see any specific improvement just by using it one time as it is usually fast enough to be used a lot of times each update. The best feature of using a layermask is the ability to filter out the objects you do not need or want to detect just as in your case, so I recommend you to continue doing so.

There is unfortunately no guarantee that the object in position 0 of the returned array is the closest. To find the closest you would have to iterate through the array and sort it by distance.