How to detect click on empty space with 2D RaycastAll?

In this picture, the dot is the Raycast2D origin.
Circles are the objects with 2D collider.
Star is an instance of non-Game Object empty space
How can I detect the empty spaces.
For example, when I click on an empty space represented by the star, the last hit from the raycastHit2D array returned by RaycastAll would be the furthest circle from the dot instead of null.
However, I would like RaycastAll to return null as no collider is being clicked.
Are there any approach/methods to detect clicks in empty spaces ?

1 Answer

1

Just use this:

RaycastHit hit
if (!Physics.Raycast(*mouse position* + new Vector3(0, 0, -100), Vector3.forward, out hit, 100) {
  // No object is hit, do stuff.
}

This checks to see whether the position underneath the mouse is occupied by an object with a collider.

Hope this helps!

Thanks, but Physics Raycast doesn't interact with 2D colliders right?