Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Top Down 2D Random Terrain

Discussion in 'Scripting' started by afakih629, Jan 23, 2015.

  1. afakih629

    afakih629

    Joined:
    Jun 20, 2014
    Posts:
    4
    Hello Unity community, I've been struggling with trying to figure out how to create random top-down terrain.
    I'm not trying to create complete randomness, where each tile is just random, but more procedurally generated terrain in C#.
    I've tried doing two for loops, but didn't know how to randomize it.
    Here's what I have:
    (By the way, I have 3 GameObjects: Grass, Stone, and Water.
    The following code works but I need to know how to add the rest of the GameObjects and make it procedurally random. Also, just in case you were wondering, my textures are 64x64)
    Code (CSharp):
    1.  
    2.     public GameObject Grass;
    3.     public GameObject Stone;
    4.     public GameObject Water;
    5.  
    6.     public int x_length;
    7.     public int y_length;
    8.  
    9.     void Start () {
    10.         for(int x = 0; x < x_length; x++){
    11.             for(int y = 0; y < y_length; y++){
    12.                 GameObject currentObject = Instantiate(Grass,new Vector3(0,0,0),Quaternion.identity) as GameObject;
    13.                 currentObject.transform.localPosition = new Vector3(x*0.64f,y*0.64f,0);
    14.                 currentObject.transform.parent = transform;
    15.                 currentObject.transform.name = "GrassTerrainPiece " + x + " " + y;
    16.             }
    17.         }
    18.     }
    19.  
     
    Last edited: Jan 23, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Try putting the grass, stone and water objects into an array instead, then use a random index when instantiating
    Code (CSharp):
    1.    
    2.        // public GameObject Grass;
    3.        // public GameObject Stone;
    4.        // public GameObject Water;
    5.         public GameObject[] Tiles;
    6.  
    7.         public int x_length;
    8.         public int y_length;
    9.    
    10.         void Start () {
    11.             for(int x = 0; x < x_length; x++){
    12.                 for(int y = 0; y < y_length; y++){
    13.                     GameObject currentObject = Instantiate(Tiles[Random.Range(0,Tiles.Length)] ,new Vector3(0,0,0),Quaternion.identity) as GameObject;
    14.                     currentObject.transform.localPosition = new Vector3(x*0.64f,y*0.64f,0);
    15.                     currentObject.transform.parent = transform;
    16.                     currentObject.transform.name = "RandomTerrainPiece " + x + " " + y;
    17.                 }
    18.             }
    19.         }
    20.    
    21.  
     
  3. afakih629

    afakih629

    Joined:
    Jun 20, 2014
    Posts:
    4
    Thanks for the answer.
    However, how do I generate the terrain procedurally?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  5. afakih629

    afakih629

    Joined:
    Jun 20, 2014
    Posts:
    4
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Here's some code which uses Perlin noise to create this. The relevant code is the nested loop in the CreateLevel function; as you can see there's not actually much to it. It's just two kinds of tiles but the idea could be extended to use more.

    --Eric
     
    Deleted User likes this.