# How does Physics2D.OverlapBoxAll work in Unity 3D?

I am using Unity to create 2D sidescroller game. In my game I have a person that does a melee attack. In order to implement this I am using Physics2D.OverlapBoxAll().

• The first parameter is the position of the box.
• The second parameter is the size.
• The third parameter is the angle.
• The fourth parameter is the layerMask.

I am having trouble understanding the second and third parameters. What does angle mean? Also I am having trouble understanding the size is relative to which units?

Relevant Code:

My Attack Function:

void Attack() {
Vector2 tempx = new Vector2 (AttackTransform.position.x, AttackTransform.position.y);
Vector2 tempy = new Vector2 (AttackTransform.position.x + swordSwingRange.x, AttackTransform.position.y + swordSwingRange.y);

Debug.DrawLine (AttackTransform.position, new Vector3(tempy.x,tempy.y, AttackTransform.position.z));
print (enemies.Length);

}

Explanation: The AttackTransform is the starting position where I would like to instantiate my attack. tempx and tempy are the starting and ending positions respectively. 'enemies' is used to find the the number of colliding objects which i will later iterate to decrease health or something.

In my Debug.DrawLine function it draws a line from the start position to the endposition. Hence shouldn't it draw a line that represents the box that will be formed for Physics2D.OverlapBoxAll() ? However as I have experimented with that it does not so. So what scale of units do the aforementioned methods use?

Another side-question: Is this the best way to detect collision in front of me? Or is there a better way? Any links would be appreciated I can study up further!

Thank you so much for taking the patience to read my question! And I greatly appreciate the help!

The size is measured in world units, measured from one side of the box to the other, with point specifying the center.

If I wanted a box running between (1, 2) at its bottom left to (3, 5) at its top-right, that would be point = (2, 3.5) (the center) and size = (2, 3) (total width & height in world units)

The angle is a z rotation applied to the box.

So if you create a new default cube primitive at the top level of your hierarchy, and set its scale equal to the size you want, and the z rotation to the angle, the result will match the area checked by OverlapBox.

We can also make a preview function to draw it:

void DebugDrawBox( Vector2 point, Vector2 size, float angle, Color color, float duration) {

var orientation = Quaternion.Euler(0, 0, angle);

// Basis vectors, half the size in each direction from the center.
Vector2 right = orientation * Vector2.right * size.x/2f;
Vector2 up = orientation * Vector2.up * size.y/2f;

// Four box corners.
var topLeft = point + up - right;
var topRight = point + up + right;
var bottomRight = point - up + right;
var bottomLeft = point - up - right;

// Now we've reduced the problem to drawing lines.
Debug.DrawLine(topLeft, topRight, color, duration);
Debug.DrawLine(topRight, bottomRight, color, duration);
Debug.DrawLine(bottomRight, bottomLeft, color, duration);
Debug.DrawLine(bottomLeft, topLeft, color, duration);
}

In your case, it looks like you want your first argument to be just AttackTransform.position if that's the center of your attack range (Vector3s can implicitly truncate to xy Vector2s, so you don't need to split out its components manually), and your second argument can be swordSwingRange on its own (assuming it's an end-to-end distance and not a half-extent)