- Home /
Physics2D.OverlapBoxAll and Physics2D.OverlapAreaAll are inaccurate.
Open this image in a new tab if it's hard to read the small text.
Attached to the white square is code that should detect any overlapping rectangles and print the results to the console. In this case, only the red rectangle should be detected since the red rectangle is the only rectangle that overlaps the white square. However, the green rectangle is also considered to be overlapping the white square. Why?
Upon closer inspection, you can see that the green rectangle is in fact NOT overlapping the white square. Using Physics2D.OverlapAreaAll provides the same results. Am I doing something wrong?
void Overlap()
{
Vector2 point = transform.position;
Vector2 size = transform.localScale;
float angle = 0;
Collider2D[] colliderHits = Physics2D.OverlapBoxAll(point, size, angle);
Debug.Log("Point: (" + point.x + ", " + point.y + ")");
Debug.Log("Size: (" + size.x + ", " + size.y + ")");
for (int i = 0; i < colliderHits.Length; i++)
{
Debug.Log("- overlapping " + colliderHits[i].name);
}
}
FYI, all 4 of the colored rectangles have a BoxCollider2D (trigger) and a Rigidbody2D (kinematic). Also, if the blue and yellow rectangles were moved slightly closer to, but not quite touching, the white square, they provide results just as the green rectangle does now.
Check if all transform is on Z axis = 0.
Sometime I make a mistake that copy paste 2D colliders on z axis = 0.01 and 0. 2 of them never collide with each other.
How are you drawing the rectangles? $$anonymous$$aybe the actual bounding box is bigger than the visuals if the visuals have a small invisible border. Try logging position and size of both allegedly overlapping rectangles and see if the numbers check out.
Your answer
Follow this Question
Related Questions
overlayCircle not working 1 Answer
Physics2D.OverlapBoxNonAlloc not return all overlapped collider 1 Answer
How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer
Physics2D.OverlapCircleAll and OverlapCapsuleAll behaves differently? 0 Answers
Physics2D.OverlapArea inaccurate? 2 Answers