- Home /
Does adding a Layermask improve efficieny of Physics.OverlapSphere?
Hi,
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
Answer by GameVortex · Feb 20, 2014 at 09:20 PM
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.
Sort it by sqr$$anonymous$$agnitude to make it more efficient - and if you are looking for the closest object, don't sort it at all, just scan it once - it's much more performant that way.
Your answer
Follow this Question
Related Questions
Efficient way to scan surrounding with large number of objects 0 Answers
OverlapSphere doesn't detect colliders 1 Answer
OverlapSphere ignoring all colliders when I use the layerMask parameter. 1 Answer
Replacing OverlapSphere with colliders (Detecting weapon collision with enemies - SOLVED) 1 Answer
OverlapSphere, Layermask problems 1 Answer