Skip to content

Random Get Point In Rect

Gets a random point inside a 2D rectangle.
The rectangle is defined relative to a center position, and you can control its size and offset using the Rect parameter.

Use it to spawn or position objects randomly within a defined rectangular region - such as grid-based pickups, enemy zones, or UI scatter effects.

Parameters

Parameter Description
Center The world-space center position of the rectangle.
Rect The rectangle to sample, relative to the Center. Defines width, height, and optional offset.
Store Result Vector2 variable to store the random world-space point.

Tips

  • Centered rectangle: Use a rect centered at (0, 0) to position points evenly around the center.
  • Offset rectangles: Adjust the Rect position (x, y) to shift the sampling area relative to the Center.
  • UI usage: Combine with RectTransform positions for random UI element placement.
  • Default rect: Resets to a 1x1 unit square centered on (0, 0) if not specified.

Common Pitfalls

  • Rect size = 0: Returns the Center point.
  • Negative width or height: Automatically handled by Rect, but may invert directions visually.
  • Incorrect origin: Remember the Rect values are added to the Center, not absolute world coordinates.

See Also