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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random, Infinite Terrain Generation (C#)

Discussion in 'Scripting' started by TheZiggy, Mar 21, 2015.

  1. TheZiggy

    TheZiggy

    Joined:
    Jul 20, 2014
    Posts:
    8
    I'm trying to generate random terrain for my game. All I need is water and grass. No height to it (no mountains or hills.) I'd preferably like to do it by placing individual cubes. On top of that the terrain needs to be infinite.

    I've searched every where for even a hint on how to do it, but everything i found had either height to it, didn't use individual cubes (edited the terrain as a whole), or wasn't infinite.

    Any help would be much appreciated! :)

    EDIT: I'm looking for something somewhat similar to a game called Factorio. Here's a screenshot: http://i.ytimg.com/vi/Uns15OfPWbo/maxresdefault.jpg As you can see there is a big body of water, and a ton of land. I want to create something that randomly does that every time (random bodies of water and random land shapes). Something like Minecraft without all the height.

    I've heard about something called Perlin noise, but because of a lack of tutorials and documentation, I can't figure out for the life of me on how to use it to generate random terrain.
     
    Last edited: Mar 22, 2015
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
  3. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    You can use 9 planes at most to achieve what you want. Just make a 3x3 grid with 9 planes and reposition them according to the player's position in the grid. I think this will work.
     
  4. TheZiggy

    TheZiggy

    Joined:
    Jul 20, 2014
    Posts:
    8
    Thanks for the help with the infinite part. I really need some help with the actual generation of the terrain though.
     
  5. TheZiggy

    TheZiggy

    Joined:
    Jul 20, 2014
    Posts:
    8
  6. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Unity has a perlin noise function that you pass an x and y into and get back a height. The value returned will be between 1 & -1. Just take this number see if its below 0 and make the cube water if it is else it is land. You can adjust the 0 to iincrease/decrease the amount of water.