- Home /
How to procedurally generate a super simple terrain
Hey guys,
I'm trying to make a super simple terrain (two flat levels, and a flat cliff separating them). After a little bit of research I decided that a heightmap created at runtime would be the most efficient way to achieve this. It would need to have only two colors, and divided right through the middle (with a few curves). Incase I'm not explaining this very well, here is an example of what I'm trying to explain.
The picture is from a game that I'm remaking, so please pardon the little houses and such.
Anyway, I really need some help figuring this out, so if anyone knows any good tutorials, or has any experience in this, please don't be hesitant to share your knowledge!
-Thanks for all your help and comments! Gibson
Sorry that this does not really help, but I cannot help but notice that this looks just like a map from Animal Crossing :)
It is a map from Animal Crossing, good eye! I'm remaking the gamecube version
Do you already have a plan for how you intend to generate your terrain? are you building a completely static map or is this something that needs to change at runtime?
It will only be created once at run time, so it will be a static map.
For anybody looking to actually answer this question, let me help out by giving you a little background information.
The poster is looking to recreate an old (but awesome) GameCube game known as Animal Crossing. In this game, when a player starts a game, the engine generates a new map that is entirely random. This map has a few select high and low areas, connected by a sloping walkway.
So the way I am understanding this question, the poster is looking for Unity to generate a random world based of a randomly generated height map.
Hope this clears some stuff up!
Answer by IgnoranceIsBliss · Apr 17, 2012 at 02:15 AM
Runtime terrain is actually not so hard now that Unity have made the properties available to the public.
Check out http://unity3d.com/support/documentation/ScriptReference/TerrainData.html - this is the object you'll be playing with to create your terrain.
Your Terrain object has a single terrainData member that you can access to make changes.
TerrainData.GetHeights and TerrainData.SetHeights are used to set the height for sections of your map. The heights are stored as a 2-dimensional array of floats.
float[,] HeightMap = MyTerrain.terrainData.GetHeights();
The terrain is broken into a grid (the height and width of the grid are attributes of the TerrainData) and there is a height recorded at each point. So HeightMap[0,0] will be the height of the terrain in the top-left corner. HeightMap[2,4] will be 3 points across and 5 points down.
To randomise your terrain, simply fill an array with random values. But if you do this, you may be best off by then smoothing your values by averaging them with each-other, otherwise you can end up with a very spiky terrain.
//This example shows how to flatten your entire terrain to a height of 20...
TerrainData TD = MyTerrain.terrainData;
float[,] HeightMap = new float[TD.heightmapWidth,TD.heightmapHeight];
for(int x=0;x<TD.heightmapWidth;x++)
{
for(int y=0;y<TD.heightmapHeight;y++)
{
HeightMap[x,y] = 20;
}
}
TD.SetHeights(0,0,HeightMap);
Now, you can also do the same sort of thing with the various TEXTURES for your game world, using the AlphaMaps properties of your TerrainData.
This follows a similar pattern, but it's a THREE dimensional array - one dimension is your x-coordinate, one is your y-coordinate, the last is the zero-based index of the textures you've added to your terrain.
So let's say you have two different textures on your terrain - sand (#1) and grass (#2).
After you've generated your height map, you can then go through and apply textures based on the height - making lower sections sand and upper sections grass.
TerrainTextures[x,y,0] = 0.75f;
TerrainTextures[x,y,1] = 0.25f;
This would make point x,y 3/4 sand, with 1/4 of the grass texture coming through. You MUST normalise these values (ie. make sure they add up to 1).
Then use SetAlphamaps(0,0,TerrainTextures); to set it.
Thanks for the answer explaining things! I don't have time to test it tonight, so I'll try it tomorrow, and mark it as then answer then. providing it is the answer. Thanks again!
I've marked your answer as correct because I realized I did not ask my question correctly. Thanks for your help, though!
IgnoranceIsBliss - yeah, thats right, i necro'ed an old post...
But, just to thank you for it :)
Nicely done.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070418im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
calling a class named by a string -1 Answers
Having trouble swapping GameObjects on button click 1 Answer