- Home /
Question by
jprocha101 · Sep 23, 2015 at 03:40 AM ·
2dcolliderboxcollider2doverlapping
Detecting largest overlap area for BoxCollider2D/Collider2D
I have 3 box colliders and one of them is draggable. When the user lets go of the collider I want it to move to the position of the other collider it overlaps the most. I have not been able to figure it out. Right now it moves towards which ever it overlapped first. Any help would be appreciated.
Here is my code:
Collider2D[] colliders = Physics2D.OverlapAreaAll(
draggableCollider.bounds.max,
draggableCollider.bounds.min,
someLayerMask);
Collider2D mostOverLappedCollider = new Collider2D();
foreach (Collider2D c in colliders)
{
// if overlapped the most, this is where I need help :)
mostOverLappedCollider = c;
}
if (col != null &&
col.gameObject != null)
{
// move it here;
}
Comment
How about picking the one which has the nearest distance to the draggableCollider?
You mean look at the center of each collider and the one with the shortest distance to the center of the draggableCollider would be the one I want to drop it on?
By golly that is genious!
Your answer
![](https://koobas.hobune.stream/wayback/20220612034729im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scale collider from one side for 2d Object 0 Answers
RayCasts and BoxCollider2D not working? 2 Answers
No Collision in 2d Scene 0 Answers