Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Other Raycasts not hitting any colliders

Discussion in 'Editor & General Support' started by NormyHaddad, Nov 29, 2023.

  1. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    26
    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.
    Code (CSharp):
    1.         // Use raycasting to find and disable any doors that are blocking connected tunnels
    2.         foreach (GameObject wall in connectorWalls)
    3.         {
    4.             Debug.Log("Step 1");
    5.             Ray ray = new Ray(wall.transform.position, wall.transform.forward);
    6.             Debug.DrawRay(wall.transform.position, wall.transform.forward * 0.25f, Color.green, 1000f);
    7.             RaycastHit hit;
    8.             if (Physics.Raycast(ray, out hit, 0.25f)) // if it hits another object
    9.             {
    10.                 Debug.Log(hit.transform.name);
    11.                 if (hit.transform.GetComponent<HabController>() != null) // if the other object is a hab
    12.                 {
    13.                     // look through all that hab's own walls
    14.                     foreach (GameObject wall2 in hit.transform.GetComponent<HabController>().connectorWalls)
    15.                     {
    16.                         Debug.Log("Step 3");
    17.                         // find one with a distance short enough
    18.                         if (Vector3.Distance(wall.transform.position, wall2.transform.position) >= distanceThreshold)
    19.                         {
    20.                             Debug.Log("Step 4");
    21.                             // disable both walls
    22.                             wall.SetActive(false);
    23.                             wall2.SetActive(false);
    24.                         }
    25.                     }
    26.                 }
    27.             }
    28.         }
    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.
    upload_2023-11-29_13-33-6.png
     
    Last edited: Nov 29, 2023
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,203
    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.
     
  3. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    26
    The raycasts do hit other objects, but not the one that I want to hit, even though they probably hit themselves too.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,203
    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.
     

    Attached Files:

  5. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    26
    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?
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,203
    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:

    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.
     
  7. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    26
    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.
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,203
    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.