How to make a ground targeting system for any terrain type

I am trying to make an actor that can place objects in the world some distance in front of him. I always want the object to be 1000 units in the horizontal axis in front of the actor but placed on the ground.

Below I have added an image where the green triangle is my actor and the red square is the desired placement of my object.

First, I find a starting point that is 1000 units in front of the actor by getting his forward vector.

The methods I have tried:

  1. Line trace from starting point straight downwards. This works for cases 1, 2 and 4, but doesn’t work for case 3 because the starting point is already below terrain.
  2. Line trace from xy coordinates of starting point but + 999z (higher up). This works for cases 1, 2 and 3, but doesn’t work for case 4 because the ceiling intercepts the trace.
  3. Method 1 for cases 1,2,4 but Line trace from starting point straight upwards used only on case 3. This doesn’t work because the trace start is inside terrain and instantly registers a hit instead of going to the surface. I do not know if it is possible to get the first surface point from inside static meshes.

I am out of ideas how to achieve this behavior regardless of terrain. Any hints and ideas would be appreciated.

Easiest approach without knowing all the possible edge cases…

Add 1,000 units to the Z axis Start point. Thus firing forward from an elevated position above the player. At hit/end of trace, trace down n + 1000 units.

This won’t however give you an exact 1,000 units ahead via surface distance…just 1000 units in world space.

Another idea:

  1. Trace forward: if no hit, trace down and spawn on hit. If hit…
  2. Loop trace strating Z + n (leys say 50.0) over last end vector and trace down 50.0: if no hit, next loop add 50.0 Z and trace 50.00 down again untril X tries or blocking hit returns true.
  3. When tracing agains planes there is a chance that you will not hit anything on the first loops. So have a bool set to true when it hits something, then when next loop fails to hit, you know last hit was solid ground.

BP is a bit messy but just to get the idea across… hope it helps.
Imgur

SpawnActorsAtDistance

2 Likes

Thanks. This solves the issue.