I can't get Raycasting in 2D to work, what am I missing?

I’m trying to detect touches and clicks of a 2D object using Raycast, but the RaycastHit2D object comes back empty. It’s not null, but all of its properties are either null or an initialized Vector of (0,0). I’ve looked online and gone through several answers, all of which seem to have the same basic code. Here’s the Update method of the script that is meant to detect touches:

void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Vector2 coords = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            CheckForCollisions(coords);
        }

        if (Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Vector2 touchPos = Input.GetTouch(i).position;
                Debug.Log(touchPos.x + " : " + touchPos.y);

                CheckForCollisions(touchPos);
            }
        }
    }
    

    void CheckForCollisions(Vector2 touchPos)
    {
        Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos);
        Debug.Log("World: " + wp.x + " : " + wp.y);
        Debug.DrawRay(wp, new Vector3(wp.x, wp.y, 20),Color.green,1000.0f);
        RaycastHit2D hit = Physics2D.Raycast(wp, Vector3.forward, Mathf.Infinity);

        if (hit.collider != null)
                {
                Debug.Log("HIT SOMETHING");
                    Destroy(hit.collider.gameObject);
                }
    }

And here’s the method that actually checks for collisions:

 void CheckForCollisions(Vector2 touchPos)
    {
        Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos);
        Debug.Log("World: " + wp.x + " : " + wp.y);

        Debug.DrawRay(wp, new Vector3(wp.x, wp.y, 20),Color.green,1000.0f);
        RaycastHit2D hit = Physics2D.Raycast(wp, Vector3.forward, Mathf.Infinity);

        if (hit.collider != null)
                {
                Debug.Log("HIT SOMETHING");
                    Destroy(hit.collider.gameObject);
                }
    }

I’ve drawn the ray that is cast and it looks like it’s hitting the object:

[85690-raycast.png |85690]

85691-raycast2.png

I can only upload 2 pictures, but rest assured that the ray appears to be going through the blue circle from every angle in the editor.

Pertinent info:

  1. I have seen solutions using both: Physics2D.Raycast(start, Vector3.forward, Mathf.Infinity); and Physics2D.Raycast(start, Vector3.zero, Mathf.Infinity); I have tried them both (actually, I’ve gone through all of the Vector3 and Vector2 predefined directions) to no avail.
  2. The script is attached to my main camera, which is positioned at (0, 8.12, -1.5), set to Orthographic projection, has a size of 10 and whose clipping planes are Near:1, Far:5
  3. The circle is positioned at (0,0,0)
  4. The circle is a prefab that I’ve manually placed on the screen, however, there is a script that generates more circles. None can be touched.
  5. All circles (I believe) are on the default layer and the Default Layer’s collision matrices for 2D and 3D have the option ‘Ignore Raycast’ deselected.
  6. The circles have a Rigidbody 2D component and a CircleCollider2D. The CircleCollider2D has the ‘Is Trigger’ option ticked on.
  7. I had previously been using OnMouseDown() to detect touches with success on both Web and Android builds, however the method stopped working on Android. I’m not sure why, but it happened around the time that I updated my Unity installation.
  8. Unity editor version is 5.6.0b2.

All help is appreciated. Thanks for reading!

I figured this out shortly after posting my question. The cause of my issue is that the circle prefabs Collider2D had the ‘Is Trigger’ option checked but I was not handling the OnTrigger events. There are a few ways to resolve this issue:

  • Uncheck the ‘Is Trigger’ option on the Collider2D component.
  • Uncheck ‘Queries Hit Triggers’ Under Edit->Project Settings → Physics2D
  • Handle the OnTrigger events in a script.

The simplest solution for me was the first one.