Search Unity

Generating a simple random terrain

Discussion in 'Scripting' started by Victus, Mar 18, 2016.

Thread Status:
Not open for further replies.
  1. Victus

    Victus

    Joined:
    Mar 18, 2016
    Posts:
    2
    Hey there.

    I'm trying to create a scene that generates a random heightmap for a terrain, textures it, and then populates with trees and grass. However, I can't seem to figure out how to handle the heightmap generation. I don't need need the terrain to be infinite or anything fancy like that, but I also don't want to use pre-built toolkits, like Gaia and the like.

    Any idea how I would do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    There are plenty of examples on the interwebs, complete tutorials on how to do this. Google for "procedural geometry in unity" and you will learn a lot.

    It is really great fun making procedural geometry in Unity, but you will not master it overnight. It's a HUGE field of expertise with lots of areas of confusion, so tackle it patiently and you may enjoy it as much as I do.
     
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Use Perlin Noise. It works wonders, I've made something which allows me to generate a random terrain from a seed. I'm trying to adapt it so it makes a planet out of the terrain. (currently mind bogged haha) let me know if you want my script to have a look over, i wouldn't say use it because everyone has different ways of doing things but it'll maybe help you out.
     
  4. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    1. Open blender
    2. Enable ant script
    3. Generate mesh/obj
    4. Drag blend file into unity.
    5. Done
     
    Kurt-Dekker likes this.
  5. Victus

    Victus

    Joined:
    Mar 18, 2016
    Posts:
    2
    Hey guys!

    Thanks for the tips! I've started working on a random generator using perlin noise. So far I have this.

    Code (CSharp):
    1. public class HeightMapGenerator : MonoBehaviour {
    2.  
    3.     public float Tiling = 10.0f;
    4.     public Terrain terrain;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         GenerateHeights(terrain, Tiling);
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.  
    16.     public void GenerateHeights(Terrain terrain, float tileSize)
    17.     {
    18.         float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
    19.  
    20.         for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
    21.         {
    22.             for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
    23.             {
    24.                 heights[i, k] = Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize) / 10.0f;
    25.             }
    26.         }
    27.         terrain.terrainData.SetHeights(0, 0, heights);
    28.     }
    29. }
    30.  
    I like this because it generates the heightmap on the fly, but I'd like to add some variety. It gives me some nice hills, but I'd like to make the terrain a bit sharper and more ridged. Any idea how I would do that?
     
  6. Superelectr0nic

    Superelectr0nic

    Joined:
    Jun 17, 2018
    Posts:
    13
    Hi! I am wondering the same thing, and was wondering if I could Use the script? I could mess around with it, and maybe figure out what you want? Just one question, though - does this create a world on pressing play, or on running the script? Thanks!
     
  7. OnejmNik29

    OnejmNik29

    Joined:
    Jan 30, 2023
    Posts:
    1
    I'm Having The same Prob
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Please don't necro-post old threads. Start your own.. it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    If you're just looking for procgen stuff, here's mine:

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
Thread Status:
Not open for further replies.