Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Physics2D.OverlapCircle hits itself

Discussion in 'Physics' started by TheWitherLord10, Jul 12, 2023.

  1. TheWitherLord10

    TheWitherLord10

    Joined:
    Nov 18, 2022
    Posts:
    14
    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?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,744
    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.
     
  3. TheWitherLord10

    TheWitherLord10

    Joined:
    Nov 18, 2022
    Posts:
    14
    it worked for abit. then it just stopped
     
  4. TheWitherLord10

    TheWitherLord10

    Joined:
    Nov 18, 2022
    Posts:
    14
    also the things it's supposed to collide with have the same layer
     
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,744
    So don't put them on the same layer as the OverlapCircle object.
     
  6. TheWitherLord10

    TheWitherLord10

    Joined:
    Nov 18, 2022
    Posts:
    14
    they are prefabs of the same tile they need to have the same layer
     
  7. Armynator

    Armynator

    Joined:
    Feb 15, 2013
    Posts:
    59
  8. TheWitherLord10

    TheWitherLord10

    Joined:
    Nov 18, 2022
    Posts:
    14
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    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.
     
  10. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    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.