Make Mesh Always On Ground? (Procedural Generation)

What do I need to add to the construction script to make sure the meshes are always touching the ground? I’m trying to do EXACTLY what a procedural foliage spawner does, but using a blueprint actor rather than a foliage type.

Any other advice on how to recreate foliage spawner in a blueprint would be greatly appreciated! Thank you!

I’m using UE 4.18

First you have to randomly generate a location above the ground, like you are doing already. Then, from that location, use LineTraceByChannel to fire a ray downward (negative Z direction) to the terrain. Get the Hit Results from the line trace and extract the Location (or Impact Point). That is the location on the ground where you need to finally spawn your foliage mesh.

Sorry to be difficult but what goes in to the execution pin for the line trace? And from hit actor, do I cast to landscape? Finally, as far as a negative ‘Z’ value for the line trace, can it be any value so long as it is negative?

After the line trace you can indeed use a Branch to check that the ray hit something, then verify that that something is the landscape. You can avoid that by selecting Landscape as trace channel in the Line Trace by Channel node. You will still need to check that the ray hit something (Return Value should be true).

If you are use the Landscape trace channel there should be no need to verify the ray hit the landscape. This will always be the case.

Your negative Z should be long enough to reach the landscape. Not sure what an appropriate value would be in your case, it depends very much on the spawning logic. Start with something like 5000 (50 meters above) and increase it gradually if you have many missing hits.

You’ll have to pardon my ignorance, as I’m still learning… I’m just confused as to what to hook up to the execution pin on the line trace. Is this correct so far?

Yes, look good so far. The IN execution pin of the Line Trace should come from some sort of event, a mouse click or a key pressed. The OUT pin should go to a Branch checking Return Value and, if true, should go to your foliage spawning.

Hm, no matter what I do, there are trees floating above or sinking below ground.I have a very hilly/mountainous landscape So maybe I’m just not getting the numbers correct. Also, I’m setting this up in the construct, and I’m not able to find any mouse click events other than “bind to on clicked”

In this case I think the execution input will be the construction event. (so when the BP is created that executes and places the mesh on the ground)

It might be too simple but, can’t you use an static navmesh and use to get random locations to spawn?

Sorry, I was offline during the past two days. Indeed, thanks !