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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

How to check if colliders overlap?

Discussion in '2D' started by catalincraciun, Apr 7, 2015.

  1. catalincraciun

    catalincraciun

    Joined:
    Apr 7, 2015
    Posts:
    3
    If you have to colliders (let's say an BoxCollider2D and a CircleCollider2D), how do you check if these colliders are overlapping? Colliders methods don't seem to work... Both are triggers.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,243
  3. catalincraciun

    catalincraciun

    Joined:
    Apr 7, 2015
    Posts:
    3
    Hello, thanks for your response. The problem is that I want to check if a collider hits any other collider. But I have solved that with Box Cast, thanks anyway.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,243
    ... and that's exactly what those methods check for; if the specified colliders are touching.
     
  5. catalincraciun

    catalincraciun

    Joined:
    Apr 7, 2015
    Posts:
    3
    Sorry, I have forgotten to mention that I don't know the other collider. I have a tag, let's say "Box", and I want to check if a collider is touching any collider attached to any box.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,243
    The physics system is layer-based so if you use layers then you can use 'Collider2D.IsTouchingLayers()' to see if your collider is touching anything on specific layer(s). It doesn't however tell you what is colliding which is obviously where 'OverlapArea' can help when you're not interesting in casting a box over a distance.
     
    theANMATOR2b likes this.
  7. amarnaman

    amarnaman

    Joined:
    Mar 27, 2014
    Posts:
    5
    That's really useful but what about if you want to check if two 3D colliders are overlapping?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,243
    I don't think there's the equivalent test in 3D physics apart from the ones I presume you've already look at in Physics.
     
  9. amarnaman

    amarnaman

    Joined:
    Mar 27, 2014
    Posts:
    5
    Thanks for the suggestion (obvious I know)! I hadn't spotted Physics.OverlapBox before and it worked just fine. It was a bit trickier to use than I'd thought and I cheated slightly since it was returning more hits than I'd expected (I think it was the extents I was using but it's hard to tell).
     
  10. maarten_unity795

    maarten_unity795

    Joined:
    Jan 11, 2022
    Posts:
    1
    Sorry for digging up an old post but for anyone looking for that now and wants a simple answer:
    Collider.Bounds.Intersects(OtherCollider.bounds);
     
    Joy-less likes this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,243
    Well unfortunately that isn't a good way to do it unless you want a very rough check. At best they will overlap but it'll also result in saying they might overlap because "Bounds" is an AABB.

    For 3D you might be better off using something like Physics.ComputePenetration and looking for overlap.

    This was a question about 2D though.
     
    stefanob likes this.