The Free Spot Finder 2D is a highly performant area analyzer that can detect remaining unoccupied spots in circular and rectangular 2D areas. It provides three main capabilities:
- Calculate the percentage of occupation in a rectangular or circular area.
- Retrieve a random free spot from a rectangular or circular area.
- Check if a defined rectangular or circular area is unoccupied or not.
The algorithm used is 5-20x faster than using any Physics.Cast approach and works with internal shapes or existing colliders. It analyzes the area and retrieves a random free spot usually within a few milliseconds.
The package supports various concepts, including:
- Fixed boundaries to retrieve free spots
- Padding for placed objects to avoid overlapping with occupied areas
- Rectangular or circular detection areas
- Flexible resolution of the detection algorithm
- Rectangles, circles, ellipses, and points as occupied areas
- Collider2D (any) as occupied areas
- Rotation and scaling of Collider2D
- DebugDraw functions to display results of analysis
The provided class is easy to use, and every public function, method, and property is well documented. To retrieve a free spot, you can follow these three easy steps:
- Create an instance with preferred settings (or use properties later)
- Add occupied areas manually by providing shapes or enable detection of existing colliders
- Request a new random available spot for an object of defined size
The class is encapsulated in a single class called ScanArea2D, which can be placed anywhere in your project folder structure. It has no specific dependencies on external libraries and requires only System and UnityEngine.
The ScanArea2D class is a single class that encapsulates all the logic. It can be created ad-hoc just before requesting a free spot or maintained for a longer duration for better performance in specific cases. The class has the following exposed methods and properties:
Exposed analytical methods:
- GetOccupation
- GetRandomFreePosition
- GetRandomFreePositionForCircle
- GetRandomFreePositionForRectangle
- TryGetRandomFreePosition
- TryGetRandomFreePositionForCircle
- TryGetRandomFreePositionForRectangle
- IsFree
Exposed shape methods:
- AddPoint
- UpdatePoint
- AddCircle
- UpdateCircle
- AddRectangle
- UpdateRectangle
- RemoveShape
Exposed setting methods:
- SetBoundaries
- RemoveBoundaries
Exposed properties:
- boundariesCenter
- boundariesSize
- baselineResolution
- fixedResolution
- ignoreAllColliders
- ignoreAdvancedColliders




