- Home /
Physics2D.OverlapBoxAll seems inaccurate
I've been trying to get Physics2D.OverlapBoxAll to work, but it seems like it works inconsistently; sometimes it seems to not include objects that are within the bounding box.
This is the code I'm using :
Vector3 direction = (endPos - origin).normalized;
endPos = origin + direction * length;
Vector2 center = origin + direction * (length / 2.0f);
Vector2 size = new Vector2(width, length);
float angle = Vector2.Angle(Vector2.up, direction);
Collider2D[] enemies = Physics2D.OverlapBoxAll(center, size, angle, 1 << EnemyManager.GetEnemyLayer());
I have code to visualize the box that seems like it has the right width and length and orientation, but it must be that the visualization isn't matching the collider2D, or that the collider2d is inaccurate. Is there a more straightforward way to do a debug visual for the box? Is the function just inaccurate?
edit: After more testing it seems like it's getting colliders that aren't anywhere near the box and missing colliders that it should be hitting. It's not a small inaccuracy, it seems like there's something fundamentally wrong with the code or my interpretation of the function.
As a follow-up, it's unclear to me what "angle" represents. How much the box will be rotated clockwise? If so, is my calculation of angle correct?
Answer by Limnage · Aug 16, 2017 at 04:45 AM
I figured it out. I needed to take the cross product to determine the sign of the angle.