(Unity) how to write my own raycasting functions

I am making a game in curved space in Unity and I am trying to figure out the best way to handle raycasting. For my purposes the Ray struct doesn't carry enough information so I started by writing a custom ray struct. I suppose next I need to write a custom collider class but I'm not sure whether I should inherit from Unity's Collider class. The Collider class has a Raycast function that I could overload, but the function signature takes normal Rays. It would be cool if I could just interface with the normal Unity physics raycasting system, but it looks like that won't work because it's all built around passing around Rays.

So what I'm thinking is I'll write a custom static physics class which will have a raycast function which will loop over all the custom colliders in the scene and find which one my custom ray hits first. I suppose I will also have to write my own version of the RaycastHit class since the existing RaycastHit has a field that carries Colliders.

The alternative is my custom collider could inherit from the Unity Collider and add a new version of Raycast that takes my custom rays. Then I could use the existing RaycastHit class, but I would still have to write my own physics class.

So my first question is how I should structure all this. I've given it some thought but I'm looking for input from someone with a deeper understanding of Unity. Next, I have one particular object that I'm interested in raycasting that will have a large mesh and I need to know which triangle is hit by the raycast. I suppose I should make a custom mesh collider class that inherits from MeshCollider.

So I'm just looking for insight into the cleanest way to organize all this. Thank you for any advice you can offer!

• What do you feel you would gain from inheritance, if you have to re-implement everything for curved space anyway? If your space is "locally flat" in some sense, you may be able to approximate a raycast in curved space by a sequence of conventional ray/spherecast segments tracing out your curve. But otherwise I think inventing a parallel system is more practical than trying to bolt onto one that fundamentally does not do what you need. If you can describe your geometry in more detail, we may be able to give you more concrete guidance. – DMGregory Aug 14 '20 at 19:17
• How are you displaying your curved-space game world to the player? Are you remapping the curved space coordinates into Unity's euclidean space? – Kevin Aug 14 '20 at 20:08
• If so, one consideration is to start out by simulating a curved raycast using a series of linear raycasts using the built-in physics. This will get increasingly expensive for longer raycasts (since you'll need more linear raycasts the longer your curved raycast is), but could save you a lot of development time, and if you don't have prior experience writing physics code it might still be faster than a custom solution. If after profiling you discover this is too expensive, you can always re-approach the idea of a custom solution later. – Kevin Aug 14 '20 at 20:30
• I think just doing the raycast in spherical space might be cleaner and more stable. My plan is to represent a ray by it's start point pos and a point 90 degrees away in spherical space nor. Then an arbitrary point on the ray is cos(t)pos + sin(t)nor. If we want to know when the ray hits a plane then there's some nice linear algebra we can do to solve for cos(t) and sin(t), and then I have to figure out how to check if the point is inside a given triangle in the plane. I think this might be faster and more stable than linear approximations. – A. Kriegman Aug 14 '20 at 21:06
• In my experience, inheritance is only rarely the right answer in Unity. – DMGregory Aug 14 '20 at 22:06