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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
[SerializeField] ulong widthGeneratedLevel;
[SerializeField] GameObject dirt, grass, bush, tree, coin;
void Start()
{
GenerateLevel();
}
void GenerateLevel()
{
for (ulong x = 0; x < widthGeneratedLevel; x=x+3)
{
if (x > 2)
{
int coinRandom = Random.Range(0, 100);
if (coinRandom < 10)
{
spawnObj(coin, x, coinRandom +2);
}
}
int bushRandom = Random.Range(0, 100);
if (bushRandom > 70)
{
spawnObj(bush, x, 1);
}
int treeRandom = Random.Range(0, 100);
if (treeRandom > 80)
{
spawnObj(tree, x, 1);
}
spawnObj(grass, x, 0);
spawnObj(dirt, x, -1);
}
}
void spawnObj(GameObject obj, ulong width, int height)
{
obj = Instantiate(obj, new Vector2(width, height), Quaternion.identity);
obj.transform.parent = this.transform;
}
}
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?