RaycastHit2d problems

Hello fellow unity users :slight_smile:

I have a small problem with the new 2d raycastHit

My code

   void Update()
    {
        var i = 0;
        while (i < Input.touchCount)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                RaycastHit2D hit = Physics2D.Raycast(transform.position, Input.GetTouch(i).position, 1000);
                if (hit != null)
                {
                    Destroy(hit.transform.gameObject);
                }
            }
            ++i;
        }
    }

It seems the position on the phone where i hit is random.
Can anybody help me with this ??

Best regards

Wacky Moose

I have solved the problem.

Here is my code

    public Camera cam;
    void Update()
    {
        var i = 0;
        while (i < Input.touchCount)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.GetTouch(i).position), new Vector2(0,0));
                if (hit != null)
                {
                    Destroy(hit.transform.gameObject);
                }
            }
            ++i;
        }
    }

Best regards

Wacky Moose