x


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.

This picture is from the game Animal Crossing

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

more ▼

asked Apr 16 '12 at 10:54 PM

AVividLight gravatar image

AVividLight
1.9k 68 100 128

Sorry that this does not really help, but I cannot help but notice that this looks just like a map from Animal Crossing :)

Apr 17 '12 at 12:37 AM HomeSpunGames

It is a map from Animal Crossing, good eye! I'm remaking the gamecube version

Apr 17 '12 at 12:39 AM AVividLight

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?

Apr 17 '12 at 12:39 AM Atrius

It will only be created once at run time, so it will be a static map.

Apr 17 '12 at 12:40 AM AVividLight

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!

Apr 17 '12 at 12:44 AM HomeSpunGames
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 17 '12 at 02:15 AM

IgnoranceIsBliss gravatar image

IgnoranceIsBliss
591 3 7 10

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!

Apr 17 '12 at 02:29 AM AVividLight

I've marked your answer as correct because I realized I did not ask my question correctly. Thanks for your help, though!

Apr 17 '12 at 07:30 PM AVividLight
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4174
x374
x83
x7

asked: Apr 16 '12 at 10:54 PM

Seen: 2750 times

Last Updated: Apr 17 '12 at 07:30 PM