Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


OverlapArea

Checks if a Collider falls within a rectangular area.
Read time 3 minutesLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule

OverlapArea(Vector2, Vector2, int, float, float)

Checks if a Collider falls within a rectangular area.
public static Collider2D OverlapArea(Vector2 pointA, Vector2 pointB, int layerMask, float minDepth, float maxDepth)

Parameters

pointA

One corner of the rectangle.

pointB

Diagonally opposite the point A corner of the rectangle.

layerMask

Filter to check objects only on specific layers.

minDepth

Only include objects with a Z coordinate (depth) greater than or equal to this value.

maxDepth

Only include objects with a Z coordinate (depth) less than or equal to this value.

Returns

Type

Description

Collider2DThe Collider overlapping the area.

Remarks

The rectangle is defined by two diagonally opposite corner coordinates in world space. You can think of these as top-left and bottom-right but the test will still work if the ordering of the points is reversed. The optional layerMask allows the test to check only for objects on specific layers.
Although the Z axis is not relevant for rendering or collisions in 2D, you can use the minDepth and maxDepth parameters to filter objects based on their Z coordinate. If more than one Collider falls within the area then the one returned will be the one with the lowest Z coordinate value. Null is returned if there are no Colliders in the area.
Additional Resources: Physics2D.OverlapAreaAll & OverlapAreaNonAlloc.

OverlapArea(Vector2, Vector2, ContactFilter2D, Collider2D[])

Checks if a Collider falls within a rectangular area.
public static int OverlapArea(Vector2 pointA, Vector2 pointB, ContactFilter2D contactFilter, Collider2D[] results)

Parameters

pointA

One corner of the rectangle.

pointB

Diagonally opposite the point A corner of the rectangle.

contactFilter

The contact filter used to filter the results differently, such as by layer mask, Z depth. Note that normal angle is not used for overlap testing.

The array to receive results. The size of the array determines the maximum number of results that can be returned.

Returns

Type

Description

intReturns the number of results placed in the results array.

Remarks

This function returns the number of Colliders found and places those Colliders in the results array.

OverlapArea(Vector2, Vector2, ContactFilter2D, List<Collider2D>)

Checks if a Collider falls within a rectangular area.
public static int OverlapArea(Vector2 pointA, Vector2 pointB, ContactFilter2D contactFilter, List<Collider2D> results)

Parameters

pointA

One corner of the rectangle.

pointB

Diagonally opposite the point A corner of the rectangle.

contactFilter

The contact filter used to filter the results differently, such as by layer mask, Z depth. Note that normal angle is not used for overlap testing.

The list to receive results.

Returns

Type

Description

intReturns the number of results placed in the results list.

Remarks

The integer return value is the number of results written into the results list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the results list does not need to be resized, and improves garbage collection performance when the query is performed frequently.