Raycasts not hitting any colliders

I have a system in my game that’s supposed to, at some point, send out raycasts from some child objects. I checked with debug.ray, and they do pass through the objects they’re supposed to hit, but no hits are registered. Here’s the code I have.

        // Use raycasting to find and disable any doors that are blocking connected tunnels
        foreach (GameObject wall in connectorWalls)
        {
            Debug.Log("Step 1");
            Ray ray = new Ray(wall.transform.position, wall.transform.forward);
            Debug.DrawRay(wall.transform.position, wall.transform.forward * 0.25f, Color.green, 1000f);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 0.25f)) // if it hits another object
            {
                Debug.Log(hit.transform.name);
                if (hit.transform.GetComponent<HabController>() != null) // if the other object is a hab
                {
                    // look through all that hab's own walls
                    foreach (GameObject wall2 in hit.transform.GetComponent<HabController>().connectorWalls)
                    {
                        Debug.Log("Step 3");
                        // find one with a distance short enough
                        if (Vector3.Distance(wall.transform.position, wall2.transform.position) >= distanceThreshold)
                        {
                            Debug.Log("Step 4");
                            // disable both walls
                            wall.SetActive(false);
                            wall2.SetActive(false);
                        }
                    }
                }
            }
        }

Here’s what the rays look like. I haven’t set them to target any specific layer, and they definitely pass through other objects which have non-convex mesh colliders. The console also only prints out “step 1”, and nothing else. These objects are child objects of a parent with kinematic rigidbody.

I can’t really tell exactly where those green rays are in relation to the mesh I’m looking at. But I’d start with the following:

  • Extend the hit distance a bit, just to get a better sense of whether it’s hitting anything.
  • Be aware that Raycasts don’t hit backfaces of objects. With such a short raycast distance, I wonder if you’re starting your raycast inside of the collider you’re hoping to hit. If so, you don’t get a hit. (Basically, raycasting from inside of a collider won’t ever hit that collider.)
  • Try putting some other objects, like just a cube primitive, where you think the raycast should be hitting, and make sure you’re getting a proper hit. Make sure you’re getting at least some hits before moving on to why you’re not getting hits from this particular model.

The raycasts do hit other objects, but not the one that I want to hit, even though they probably hit themselves too.

In that case, I’d double check that you’re not casting from inside of the object. It’s kind of hard to guess what the issue might be. Is the collider a trigger, or a solid collider? Is it on a weird physics layer, or the default layer? Is the collider disabled for some reason, or rotated so that it’s not exactly where you think it is? Basically, I’d look for the differences between the collider it’s not hitting and the colliders it’s hitting correctly.

9499702--1337938--upload_2023-11-29_20-22-45.png

So i found out the reason they weren’t detecting the hit is i believe, that the object its supposed to has a collider that intersects with the object the ray is coming from. Is there a way to make the raycast still detect that hit?

I’m not 100% sure this works, but there’s a physics setting to allow raycasts to hit backfaces. You could try turning that on:

9501277--1338253--upload_2023-11-30_11-58-31.png

The other approach people use is to do the raycasting in pairs. You raycast from point A to point B. If you don’t get a hit, you then raycast from point B to point A. One of the two raycasts should probably hit.

Alright i got it. Even though there were still parts of each object that weren’t fully inside each other, they were too close together. Their colliders still intersect, though not as much as before, and it works.

Two colliders intersecting shouldn’t matter to a raycast. You’re not raycasting from one collider to another. You’re just raycasting between two points in space and testing if you hit any colliders. But it does sound like the origin point of your raycast was within the collider you were hoping to detect.

So, maybe one of the two options I mentioned in the last post will work, so you don’t need to worry about the exact positioning from now on.