- Home /
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.
Answer by whydoidoit · Mar 21, 2014 at 12:10 PM
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.
thanks for your answer. Can you please elaborate on the height map as an array of floats? Do you by any chance have any tutorials or references I could use for this?
height maps as an array of floats, basically your getting the height data from the terrain and converting into a 2 dimensional array of numbers with each number representing a level of height on the terrain.
You use Terrain.activeTerrain.terrainData to get to the root of the terrain info and then use GetHeights to retrieve the array of floating point heights.
Answer by Owen-Reynolds · Mar 21, 2014 at 03:35 PM
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?
This sounds like a good idea - the invisible grid but still I can't get any tutorials or info on how to implement that :/
Agreed that's a good way to go on predefined maps - however, given the resolution of a terrain you will never get the 8 1/2 inches scenario, would be significantly larger areas than that unless the terrain was very small with a high resolution.
Dynamically/procedurally created terrain can also benefit from sampling the splat map to ensure that the surface is suitable for building.
https://www.youtube.com/watch?v=p$$anonymous$$RnfwFOc_c @ 9:25 see that grid, this is pretty much what I would like but I literally have no idea how to implement it. Any advice?
Suppose you decide a 200x200 meter map should be treated as a 50x50 grid. $$anonymous$$ake a 50x50 array. Could be transforms, pointing the the placed item, and null means free, or could be a class you write "gridSpace." If you don't know arrays, this will be tricky.
Each grid space is 4x4 meters. So the equation for map-to-grid is x/4, round down (same for y.) Grid to map is x*4+2 (the +2 goes to the center of the square.)
A cheap way to see the grid is make a single line square texture, put on a 200x200 plane, then tiled 50x50.
8-1/2 inches: the real question is, will you ever want to adjust the tower by a tiny amount -- a little better cover, little more range? Seems like you would, but lots of games clearly have 2x2 cannons, and you can only move by 1/2 a cannon.
Your answer
Follow this Question
Related Questions
Terrain Generation ruins collision 1 Answer
Terrain trough floor. 1 Answer
Terrain collider - add 2 Answers
How to make a raycast follow terrain? 1 Answer
Starforge type gameplay 1 Answer