How to make a raycast follow terrain?

So I have a case where I need a raycast to follow the ground. Why I need that is pretty complicated, so instead here’s an analogy; let’s say I have a hitscan gun whose bullets are always Y units above the ground, regardless of how steep etc the ground is. The bullets only travel X distance before disappearing. So basically I need a raycast to “follow” any hills and valleys that may be in the way.

One advantage is the ground will always be low poly, so there will be few but sudden changes instead of lots of gradual changes. However the ground is not tile based, so the length and angle of my ramps will vary.

One idea I had was to make all edges of my terrain have colliders which “redirect” the raycst, like so:

but I don’t know if that will be performant. Another challenge is that I’m going to be potentially making thousands of these calls in a frame.

Apologies if this question has been asked already, my searches mostly turned up people trying to make a vehicle’s rotation follow terrain which isn’t what I’m looking for.

There’s a couple of solutions to this. Generally, if your object moves sufficiently slow it’s enough to do a raycast to the terrain to establish its height and adjust it accordingly. You could also use the normal vector obtained by the raycast hit to align your projectile’s orientation with the terrain gradient. Make sure to only use the part of the normal vector that changes your projectile’s pitch (and not its yaw.) An issue arises when your projectile/object moves very fast, in which case it might be “under the terrain” before it gets to raycast the terrain’s elevation. This could be prevented by raycasting from a relatively high Y position, which should be a viable solution if you have only few of those projectiles at any given time.