Semi-Random terrain generated at runtime.

Hi. I need a terrain tool (or tools) capable of generating random terrain with some fixed features. Like flat areas to facilitate building cities, at certain spots on the map.

There seem to be a lot of tools that can randomize terrain to then be saved as a fixed terrain object, but i want this to happen every time a new map loads in the game.



You can generate any kind of terrain you want, and do anything with the results. What you’re doing is generating a 2D array of floats to use for the heightmap, so you can make flat areas just by writing the same value to a given area of the array. Here’s an incredibly cheesy “random terrain generator” that makes some random bumpy ground, with some randomly-positioned flat areas:

function Start () {
	var res = 513;
    var tData = GetComponent(Terrain).terrainData;
    tData.heightmapResolution = res;

    var heights = new float[res, res];
    for (var i = 0; i < res*res; i++) heights[i%res, i/res] = Random.Range(.49, .51);
    tData.SetHeights (0, 0, heights);
    var flat = new float[50, 50];
    for (i = 0; i < 50*50; i++) flat[i%50, i/50] = .5;
    for (i = 0; i < 10; i++) {
        tData.SetHeights (Random.Range(0, res-50), Random.Range(0, res-50), flat);

From a programming side, a random terrain generator doesn’t care it you run it on level load, or in the editor. At some point it’s going to make a 2D height map, and another with the texture values, which you can do use however you want. So, you should be able to reuse any random terrain code you already have.

But real-time random terrain is a lot of work. Off-line, a human can run one, then clean up the impossible-to-play/boring/dumb-looking parts. Look at random dungeons – they have all these 2x2 “rooms”, crazy long winding hallways, “level is one giant room” levels (really 8 rooms that got randomly jammed together.)