Skip to main content
3 of 3
moved raw URL into text link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

If you set up an edge collider on the objects you can use:

void Update()
{
    //this detects the mouse position
    Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    //this check to see if the mouse is over the collider documentation below
    if (this.GetComponent<EdgeCollider2D>().ClosestPoint(mousePos) == mousePos)
    {
         print("mouse is hovering over object");
         if(Input.GetKeyDown(KeyCode.Mouse0))
         {
             print("mouse clicked on object");
         }
    }
 }

For more information, refer to the documentation for Collider.ClosestPoint.