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.
If you’re using Unity 5 then you can use the ‘IsTouching’ to check what was touching from the last physics update:
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.
… and that’s exactly what those methods check for; if the specified colliders are touching.
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.
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.
That’s really useful but what about if you want to check if two 3D colliders are overlapping?
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.
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).
Sorry for digging up an old post but for anyone looking for that now and wants a simple answer: Collider.Bounds.Intersects(OtherCollider.bounds);
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.