Measure distance ray has to travel

How can I get in studs the width of the section with a question mark, if:

  1. The wall can be of any shape, including chaotic shape meshes.
  2. I don’t know what axis size is that, because the walls can be rotated.

To explain in more technical terms, I want to know what distance has the ray traveled after hitting any part/mesh, and before leaving it.

As shown in the picture, also the ray will not always be a straight ray, and can be cast at an angle.

You can get the position where the ray first hit the wall. Then, you make another ray a few studs in front of where the first ray hit, but make it go in the opposite direction so it hits the wall. Then you get the positions of both rays and get the magnitude.

Not viable option, because I can’t guess how many

is. If I do it in very small increments until it hits the same wall, I may end up firing 1000 rays per frame, if for example the initial ray hit a very thick wall at a very sharp angle.

It also wont be viable in million other cases. For example if ray hits a table of a leg at an angle looking at it’s top surface, then if I fire back, it will still hit the table, so I will get the illusion that I hit the same object, when in fact I hit very different surfaces with completely different thickness.