Search Unity

How can I get a raycast to hit the side of a mesh collider

Discussion in 'Scripting' started by McC1oud, Aug 18, 2018.

  1. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    I've built this application which generates dynamic meshes. The problem is that I wish to control stage collision data using raycasts but Mesh colliders do not want to be detected from the side and the whole map is mesh Colliders. They have no issue if they are hit face on.

    I need to be able to ray cast perpendicular X and Y and gather hit.point data.

    I found this as a possible solution, but I don't understand how to deploy it:
    https://docs.unity3d.com/ScriptReference/Physics-queriesHitBackfaces.html

    Here is the issue to view.
     
  2. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    Figured out the back face setting under physics settings.. Not quite what i need. Certainly hits the back but it still doesn't wanna communicate with a perpendicular edge.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Uh, you could explain the video? It looks like the ray is hitting something as long as you have the collider turned on.
     
  4. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    The collider works initially because there is a big box collider around the editable stage(Voxel Map) which is being used by a drawing function to edit. I disable that and the next thing it should hit is the collision mesh(Mesh collider) on the actual mesh.

    It turns red signifying that nothing is detected. I then move the object casting the ray out a bit so it may hit the faces of the mesh collider from and angle. It turns blue.

    The problem is that it doesn't want to hit an edge of a mesh collider perpendicular.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I see!

    So, with backface culling on, anything else than a completely perpendicular ray should be hitting the meshes. A perpendicular ray doesn't hit the mesh because it's not intersecting the mesh - both are infinitely thin, and the ray is parallel to the surface.

    Is this an issue in your actual game? Do you have free-standing meshes that you're hitting from the side? With something like mouselook, lining things up like that will be very hard!
     
  6. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    Nah, the games pretty well completely in 2D so Z is not being used at all. I have been using some basic rigid body stuff in order to get the character to run along the terrain which works fairly fine, but I'd like some more control rather than leaving it up the the physics engine. I've written full dynamic stage collision systems before but that was using 3D collision meshes and not stage made out of paper.

    As long as I can figure out where the character is and where the character is going. Then gravy.
     
  7. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
  8. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    My way forward at the moment is to further progress this into 3D terrain with depth which seems completely unnecessary just to catch a Vector point in space.
     
  9. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    Hi, follow up post. I went the route of converting the mesh into depth. Still got it positioned over the plane of the edge, all this just so it can hit a face normal =P

    At any rate, finally able to catch the stages collision data.

    All that pink is collision mesh.
     

    Attached Files: