Home > Back-end >  Why raycast doesn't hit any collider?
Why raycast doesn't hit any collider?

Time:09-07

I try to make a roulette racetrack for a mobile game.

enter image description here

There is 37 box(sprite) in the racetrack, each square box have a Box Collider 2D and each rounded box have a Polygon Collider 2D.

my code:

  void Update()
    {
        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            RaycastHit hit;
            Debug.DrawRay(ray.origin, ray.direction*100, Color.yellow, 100f);
            Debug.Log("Touch Something");
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("Something Hit");
                Debug.Log(hit.collider);
                if (hit.collider != null)
                {
                    Debug.Log("collider Hit");
                }
            }
        }

    }

I would like to do something when I hit the collider of a box. When I test my code, the only debug.log that work is "Debug.Log("Touch Something")".

result for Debug.DrawRay

enter image description here

We can see that the raycast doesn't hit any collider but I don't know what I should change, please help! ^^

CodePudding user response:

I replace my 2D colliders by 3D colliders and now everythings is working.

  • Related