Set height of lots of objects to level of terrain?


I’d like to ask for ideas on how to achieve this, Say I have a scene with 1000 buildings that of varying height , but I want all of these objects to be the same height as the terrain underneath them… instead of manually moving them individually, how can I move these automatically?

Thanks for any help in advance.

It’s easy! You just use Physics-Raycast to cast a ray downwards from each building. If the ray hits something (the terrain), you change the building’s transform.position to the hit.point. Done! Note that this will only take into account the pivot point of the building, so if there is sloped ground underneath, the corners of the building might end up buried underground oder in mid-air. To circumvent this, you can make it more complex and cast four rays at each of the four corners of the building mesh’ bounds, and use the highest or lowest of those four hit points as the y value of the building’s transform.position. I have a function for that so if you need it I can post it here.

Once you have written the function, you can just loop through each building and call it for every instance.