Physics2D.OverlapCircle hits itself

so basically i’m trying to make a tile snapping system using Physics2D.OverlapCircle to detect other tiles. The problem is that Physics2D.OverlapCircle always detects the gameobject im running it from. Does anyone know how to fix it?

Put it on its own layer, something like “OverlapTest.” Then, in the Physics settings for your project, unclick the checkbox in the matrix that lets it collide with itself.

it worked for abit. then it just stopped

also the things it’s supposed to collide with have the same layer

So don’t put them on the same layer as the OverlapCircle object.

they are prefabs of the same tile they need to have the same layer

use Physics2D.OverlapCircleAll and loop through the results yourself, ignoring the objects you want to ignore

(depending on the Unity version you are using you can also use Physics2D.OverlapCircleNonAlloc, which was moved into Physics2D.OverlapCircleAll as an overloaded method lately iirc - you can slightly improve the performance when using it correctly)

no way it worked

Physics.OverlapCirce cannot hit itself, because it is not a collider. It is a query directly sent to physics system.

If it detects the object, the object has a collider attached, which matches the query criteria. In this case the idea is to filter results, as was suggested.

You could store a reference to the objects own collider. Then, do a boolean check that the collider being hit is not it’s own. I’m not sure if that’s the most efficient method, but it should work.