Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AVividLight · Apr 16, 2012 at 10:54 PM · c#runtimeheightmap

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HomeSpunGames · Apr 17, 2012 at 12:37 AM 1
Share

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

avatar image AVividLight · Apr 17, 2012 at 12:39 AM 1
Share

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

avatar image Atrius · Apr 17, 2012 at 12:39 AM 0
Share

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?

avatar image AVividLight · Apr 17, 2012 at 12:40 AM 0
Share

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

avatar image HomeSpunGames · Apr 17, 2012 at 12:44 AM 1
Share

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!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AVividLight · Apr 17, 2012 at 02:29 AM 0
Share

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!

avatar image AVividLight · Apr 17, 2012 at 07:30 PM 0
Share

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

avatar image nyscersul · Mar 09, 2020 at 06:56 PM 0
Share

IgnoranceIsBliss - yeah, thats right, i necro'ed an old post...

But, just to thank you for it :)

Nicely done.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serialization at runtime 0 Answers

Can not retrive image form device to the plane at runtime. 0 Answers

Mesh collider not working correctly on voxel terrain 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges