Issue with raycasting in 2D

I’m working on something where I can connect circles with the same color. I’m using Physics2D.Raycast to check whether or not my finger is over a circle, and if yes, start from there, and I can drag my fingers around over other circles with the same color and a line will be drawn in between the two circles.

However, there are situations where the ray cast would not work as intended. For example, I have this situation below.

52406-aaa.png

I start from the top and swipe down ward. The intentional line is to be drawn between the 3 circles in the vertical direction. But what I get is the black lines drawn. I debugged it, and as I am dragging my finger down from the 2nd circle to the 3rd, I get a log saying that the right side circle has been hit with the raycast (some times it gives a circle that isnt even near where my finger was). This issue doesnt occur in the unity editor, only on my android. The code that I am using to raycast to the circle is the following.

#if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID&& !UNITY_EDITOR)
    hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Input.GetTouch(0).position);
#endif

and I am using a 2D circle colliders on the dot so that it barely covers the boundary of the circle. Does anyone see anything wrong or have a clue on what might be happening? Any help would be appreciated!

Use Camera.ScreenPointToRay to create a ray and pass its origin and direction to your Physics2D.Raycast method.

Something like:

Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
hit = Physics2D.Raycast(ray.origin, ray.direction);