Search Unity

Procedural generation of a endless flat surface

Discussion in 'World Building' started by Keralor, Feb 26, 2021.

  1. Keralor

    Keralor

    Joined:
    Feb 23, 2021
    Posts:
    4
    Hey there,
    For my game, I would like to generate random Levels. These should only be flat surface (no height differences) with random decoration elements / different biomes.
    So far, with my script I can generate a flat surface, but when it gets too big, the game won't even load...
    A width of 10000 works fine, but I need far bigger levels (thats why I also used ulong for width of the level)

    Here is my current script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGenerator : MonoBehaviour
    6. {
    7.     [SerializeField] ulong widthGeneratedLevel;
    8.     [SerializeField] GameObject dirt, grass, bush, tree, coin;
    9.     void Start()
    10.     {
    11.         GenerateLevel();
    12.     }
    13.  
    14.     void GenerateLevel()
    15.     {
    16.         for (ulong x = 0; x < widthGeneratedLevel; x=x+3)
    17.         {
    18.             if (x > 2)
    19.             {
    20.                 int coinRandom = Random.Range(0, 100);
    21.                 if (coinRandom < 10)
    22.                 {
    23.                     spawnObj(coin, x, coinRandom +2);
    24.                 }
    25.             }
    26.             int bushRandom = Random.Range(0, 100);
    27.                 if (bushRandom > 70)
    28.                 {
    29.                 spawnObj(bush, x, 1);
    30.                 }
    31.          
    32.             int treeRandom = Random.Range(0, 100);
    33.             if (treeRandom > 80)
    34.             {
    35.                 spawnObj(tree, x, 1);
    36.             }
    37.  
    38.                 spawnObj(grass, x, 0);
    39.                 spawnObj(dirt, x, -1);
    40.            
    41.  
    42.         }
    43.  
    44.     }
    45.  
    46.     void spawnObj(GameObject obj, ulong width, int height)
    47.     {
    48.         obj = Instantiate(obj, new Vector2(width, height), Quaternion.identity);
    49.         obj.transform.parent = this.transform;
    50.     }
    51. }
    52.  
    It's not complete (biomes etc. missing), but I would first like to solve the problem I have.
    Is there any way, to make it work, so it really generates a endless plane without performance issues?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    need to spawn objects only when they become visible, if you create too many objects at start, that kills performance.

    (like minecraft view distance, can see those further away meshes appearning only when necessary. Of course can generate them before they become visible to avoid that popping in effect)

    and to spawn and despawn lots of objects, should use object pooling (to recycle objects)
    https://learn.unity.com/tutorial/introduction-to-object-pooling
    (and search other similar tutorials: unity object pooling endless
     
    Keralor likes this.
  3. Keralor

    Keralor

    Joined:
    Feb 23, 2021
    Posts:
    4
    Thank you, this will help a lot :)