How to restrict building on uneven surfaces?

I have been looking all over the place for any information regarding building objects on uneven surfaces / restricting the area to build / ie. building a turret only in designated places on a 3D map…

for instance have a quick look at the below screenshots


The red is for enemies path as well as mountains.

Now what technique do I use to restrict the building of turrets in certain places? Do I need to write my own custom class based raycast and pretty much hard code the dimensions ??

I will appreciate any informations and insights on how to go about it. Thanks for your time.

So you can get the height map as an array of floats. if you are free form placing buildings I would suggest testing these floats for 16 positions (could be more or less based on your scale I guess) representing the rectangle that is the floor plan of your turret - if they are all the same height then allow building.

Alternatively, scan the map in an editor script and and for each location specify the largest rectangular area that can be built at that point by doing the same thing as above, but scanning every float, store this as either a square size in an array or a pair of max width and max height. Then when you go to build something, lookup the closest location in the array and check whether your turret would fit in this space, update the arrays to indicate that further building is not possible for the dimensions of the thing you’ve put there or alternatively use OverlapSphere with a layer mask ignoring the terrain to see if anything is in the way.

The traditional way to do this is to put an invisible grid on the map, and hand-mark each legal space. Hand-mark, since maybe you have a space that’s flat enough, but is quick-sand, or you just want to be sure it’s always clear. Every game where you can tell each building is 2x2, or 3x3 “grid squares” does it this way.

WhyDoIDoIt writes “if you are free form placing buildings.” And the advice on how to accomplish that seems fine. But look back at your inspiration. Does it let you move a tower 8-1/2 inches to the left? Do you want that?