# Make Mesh Always On Ground? (Procedural Generation)

**URL:** <https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880>\
**Category:** World Creation\
**Tags:** how-to, ground, unreal-engine, procedural, UE4, question, Blueprint\
**Created:** [January 30, 2018, 6:08am UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880 "2018-01-30T06:08:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![four2nothin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@four2nothin](https://forums.unrealengine.com/u/four2nothin)\
**Post date:** [January 30, 2018, 6:08am UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/1 "2018-01-30T06:08:36Z")

</div>

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

 ![228863-procedural-gathering-bp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/4/b/34b6e01606b0fc3732764dc3392dc532a7a004dd.png)

---

<div class="post-metadata">

**Author:** ![vr\_marco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vr_marco/32/1194556_2.png) [@vr\_marco](https://forums.unrealengine.com/u/vr_marco)\
**Post date:** [January 30, 2018, 7:50am UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/2 "2018-01-30T07:50:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![four2nothin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@four2nothin](https://forums.unrealengine.com/u/four2nothin)\
**Post date:** [January 30, 2018, 5:16pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/3 "2018-01-30T17:16:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vr\_marco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vr_marco/32/1194556_2.png) [@vr\_marco](https://forums.unrealengine.com/u/vr_marco)\
**Post date:** [January 30, 2018, 5:27pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/4 "2018-01-30T17:27:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![four2nothin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@four2nothin](https://forums.unrealengine.com/u/four2nothin)\
**Post date:** [January 30, 2018, 6:18pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/5 "2018-01-30T18:18:23Z")

</div>

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?

 ![228920-spawn-b-p.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/c/7/1c74fd490dd4864e3fc653eaedab354525d0dd66.png)

---

<div class="post-metadata">

**Author:** ![vr\_marco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vr_marco/32/1194556_2.png) [@vr\_marco](https://forums.unrealengine.com/u/vr_marco)\
**Post date:** [January 30, 2018, 7:04pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/6 "2018-01-30T19:04:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![four2nothin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@four2nothin](https://forums.unrealengine.com/u/four2nothin)\
**Post date:** [January 30, 2018, 8:42pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/7 "2018-01-30T20:42:55Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_942e8e52](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_942e8e52](https://forums.unrealengine.com/u/anonymous_user_942e8e52)\
**Post date:** [January 31, 2018, 11:32pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/8 "2018-01-31T23:32:44Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Iotti](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Iotti](https://forums.unrealengine.com/u/Iotti)\
**Post date:** [February 1, 2018, 4:14pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/9 "2018-02-01T16:14:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vr\_marco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vr_marco/32/1194556_2.png) [@vr\_marco](https://forums.unrealengine.com/u/vr_marco)\
**Post date:** [February 1, 2018, 5:13pm UTC](https://forums.unrealengine.com/t/make-mesh-always-on-ground-procedural-generation/415880/10 "2018-02-01T17:13:53Z")

</div>

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