- Home /
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.
I don't understand what kind of information you want back from the raycast if you could successfully implement it. Can you give a different analogy? Due to the behavior of the bullet it seems independent of raycasting whether the bullet hits the target or not because the bullet can only travel a set distance and requires path finding.
Answer by christoph_r · Nov 30, 2016 at 10:58 PM
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.
I have a feeling that would be too many raycasts, since I need potentially thousands of these rays drawn per frame. If each cast was constantly casting downwards too I feel like that wouldn't work. Also worth noting, in my example the "projectile" is hitscan, i.e. no travel time -- this all has to happen in one frame.
If the projectile is hitscan and nothing is really moving, why does it need to follow the terrain? 'Thousands of these rays per frame' sounds ambitious maybe you could elaborate on what you are actually trying to do here.
I see, I understood your question wrong then. You might want to look at reading your terrain's heightmap for its gradient data by marching from the firing position. Do a raycast until the first point where the heightmap marching detects a sufficient change in the gradient. If the raycast hit nothing, repeat while adjusting your raycast's direction.
Your answer
Follow this Question
Related Questions
Raycast up slope to find the peak in the terrain. 1 Answer
Please, I need help with Raycasting! 4 Answers
How do I determine what texture I am hitting on a terrain? 0 Answers
Make a simple tree 1 Answer
Terrain height raycasting? 1 Answer