0
\$\begingroup\$

Problem:

I have a racing circuit that is made out of tiles, each of which is made out of 2 triangles. I need to know over which tile my craft is hovering on; but since there are tiny holes between some of the tiles, casting a ray fails sometimes.

Note: fixing the mesh from within a 3D modeler is not an option.

enter image description here

Question:

Is there a robust method for approaching this problem ?

\$\endgroup\$
2
  • \$\begingroup\$ What do you mean there are tiny holes in the mesh? From a plane like this, the only way I could think of having a hole in the mesh was if someone added a plane/mesh and scooted it over to line up with another mesh, without actually joining them. I believe fixing it is probably the best option, but if you insist, perhaps checking from +- 1 of various locations until you get a result. \$\endgroup\$
    – Zymus
    Commented Apr 18, 2016 at 17:26
  • \$\begingroup\$ If ray casting failed you are not on top of a tile. If you are always on top of a tile there are no holes. Conclusion: It is not the mesh you want to ray cast against. \$\endgroup\$
    – Andreas
    Commented Apr 20, 2016 at 19:21

2 Answers 2

2
\$\begingroup\$

As DMGregory has pointed out in the comment, you can use SphereCast instead. A spherecast works a lot like a raycast, only that instead of checking for hits in a single line, it has a 'radius' parameter which defines the size of the sphere, effectively making it behave like a thick ray.

Example:

RaycastHit hit;

if(Physics.SphereCast(transform.position, 0.1f, Vector3.down, out hit)){
    ...
}

You can easily change your code by replacing Physics.Raycast with Physics.SphereCast, and passing a float value after the origin parameter depending on how big the holes are.

\$\endgroup\$
0
\$\begingroup\$

The only solution I can think of is to use two or more raycasts that are placed close together but not in the same position. That way if one misses due to a mesh seam, the other should hit.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ A spherecast will have a similar effect — it's basically a "thick ray" so it won't squeeze through tiny gaps as easily. \$\endgroup\$
    – DMGregory
    Commented Apr 18, 2016 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .