Search Unity

Raycast not working with double sided materials?

Discussion in 'Scripting' started by TOES, Jun 20, 2019.

  1. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    I have an object, say a cube, with a Mesh Collider on it. When I am casting a ray from the outside using Physics.Raycast I get a hit. But when I cast it from the inside of the cube, the hit does not register.

    In my case I need the hit to register no matter what direction the ray comes from. How to achieve this?
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Note that the material you apply on the mesh has no effect on the Physics.Raycast function, so there is no way for the Physics package to know if the material you apply on the mesh is single- or two-sided.

    So generally, you'll have to cast the ray from both sides. Here's an accepted answer to this same question.
     
  3. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    Thanks, but that does not work in my case as other geometry might be blocking on the other side.

    Anyway, I fixed it by making my own raycast function checking all triangles in relevant objects.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    When you try raycast from inside to outside, as you noticed, after inverting ray direction (outside to inside), you can take last raycasted object, instead first one.