0
\$\begingroup\$

This one is hard to explain, so I'll show the code first -

for (var i = 0; i < Input.touchCount; ++i)
{
    if (Input.GetTouch(i).phase == TouchPhase.Began)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);

        if (hit)
        {
            clicksNeeded -= 1;
            GetComponentInChildren<TextMesh>().text = "" + clicksNeeded;
            Debug.Log(clicksNeeded);
            print("was a hit");
        }
    }
}

I need to check if hit touches the specific game object that the script is linked to, as at the moment if any of the circles are clicked, clicksneeded -= 1 to all objects currently instantiated.

Thanks for the help!

Heirarchy/inspector

Heiratchy/inspec

\$\endgroup\$
0
\$\begingroup\$

A ray is not the right tool for this job. You want to check whether your circle collider contains the touched point, using OverlapPoint:

Camera camera = Camera.main;
float depth = Vector3.Dot(
                camera.transform.forward,
                transform.position - camera.transform.position
              );

for (var i = 0; i < Input.touchCount; ++i)
{
    var touch = Input.GetTouch(i);
    if (touch.phase == TouchPhase.Began)
    {
        Vector3 screenPoint = touch.position;
        screenPoint.z = depth;

        Vector3 worldPoint = camera.ScreenToWorldPoint(screenPoint);

        if (myCollider.OverlapPoint(worldPoint))
           HandleHit();
    }
}

Or, if you want to take the thickness of the touch into consideration, you could compare the distance vs your collider/touch radius:

var worldDistance = Vector2.Distance(
                        worldPoint,
                        transform.TransformPoint(myCollider.offset)
                    );

if (worldDistance < myCollider.radius + touch.radius)
    HandleHit();
\$\endgroup\$
  • \$\begingroup\$ if you don't have time to answer this, that's fine. This is an educational question because I'm a student and I really want to get into game dev at uni. But why are rays not the way to do this? \$\endgroup\$ – Nepthyz Robson Apr 20 at 18:11
  • \$\begingroup\$ A raycast detects where a ray enters the collidable surface of an object. In 2D, the collidable surfaces are the edges of the shapes - so a Physics2D.Raycast against a CircleCollider2D checks where the ray crosses the circumference of the circle. It won't capture a hit from a camera line of sight firing along the z axis and striking somewhere in the middle of the disc, because there is no z axis in the 2D physics world. If the ray didn't cross the circumference from outside to inside in the xy plane, then the 2D raycast will register no hit at all. \$\endgroup\$ – DMGregory Apr 20 at 18:22
  • \$\begingroup\$ myCollider 'does not exist in the current context'. \$\endgroup\$ – Nepthyz Robson Apr 20 at 18:34
  • \$\begingroup\$ Right, because you need to declare and assign a variable before you use it. So, do that. \$\endgroup\$ – DMGregory Apr 20 at 18:46
  • \$\begingroup\$ excellent - for other people having this issue i created a 'public CircleCollider2D myCollider' and put my circle collider inside of it. It works, thank you very much DMGregory. \$\endgroup\$ – Nepthyz Robson Apr 20 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.