How to check which object was clicked on with Physics2d.Raycast() ?

Simply put, I want the ray from camera to hit the object on which the mouse was clicked, it’s a 2D game so I want physics2D to used.

My current script:

using UnityEngine;

public class clickOnObject : MonoBehaviour {


    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButton(0))
            senseObject();
       
    }
   
    void senseObject(){

        RaycastHit2D hit2D = Physics2D.Raycast(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if(hit2D.collider != null)
            Debug.Log("Hit! " + hit2D.transform.name);

        Debug.DrawRay(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition)* 100);
         
    }
}

Screenshot (I clicked on the box):

Hey, something like this might work out:

using UnityEngine;

public class clickOnObject : MonoBehaviour
{


    // Update is called once per frame
    void Update()
    {

        if (Input.GetMouseButton(0))
            senseObject();

    }

    void senseObject()
    {

        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            if (hit.collider != null)
                Debug.Log("Hit! " + hit.transform.name);
        }

        Debug.DrawRay(Camera.main.ScreenPointToRay(Input.mousePosition).origin, Camera.main.ScreenPointToRay(Input.mousePosition).direction, Color.blue);

    }
}

I know it works but it’s physics 3d, wouldn’t physics 2d be more performance efficient since I’m creating a 2d game.

Don’t think so since the Physics2D.Raycast takes Vector2 for direction.

If it’s 2D physics then use OverlapPoint after you’ve calculated the point in world-space, a Ray doesn’t really make sense. That said, there’s GetRayIntersection that allows you to specify a ray and it’ll perform all the 2D projection to give you a pseudo 3D raycast results.

4 Likes

Thanks, I tried OverlapPoint, it’s working.

Actually I did get it to work with raycast too, with this:

RaycastHit2D hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.forward * 1000);

        if(hit2D.collider != null){

          //Do something...

        }

With OverlapPoint it looks like this:

Collider2D col = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if (col != null)
        {
      
            //Do something...

        }

Both are working great and both use same lines of code, so which one should I use, OverlapPoint I guess…?
Is there anything else I can do to make it more efficient?

Thanks a lot.

It does:

RaycastHit2D hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.forward * 1000);

        if(hit2D.collider != null){

          //Do something...

        }

I’m not sure the Raycast one is correct. Raycast performs its work in XY and not Z so if you’re trying to raycast along Z then you need to use OverlapPoint or GetRaycastIntersection. OverlapPoint is much faster/efficient however.

In your case using Raycast, the direction doesn’t matter. You’re probably only picking up colliders where the ray begins anyway so it’s sort of an odd case.

1 Like

I’ll use OverlapPoint, thanks a lot.

1 Like