hello so i am trying to create a random biome generation system for my game but whenever it generates a snow tile it doesnt stop and permanantly changes it for the rest of the generation. here is my current code but any help would be appreciated:
public class ProceduralGeneration : MonoBehaviour
{
[SerializeField] int width,height;
[SerializeField] int minStoneheight, maxStoneHeight;
[SerializeField] GameObject dirt,grass,stone, snow,oldGrass;
private int treeChance = 10;
private int snowBiomeChance = 5;
private int snowBiomeLength = 0;
[SerializeField] private GameObject tree;
void Start()
{
Generation();
}
public void Generation()
{
for (int x = 0; x < width; x++)//This will help spawn a tile on the x axis
{
// now for procedural generation we need to gradually increase and decrease the height value
int minHeight = height - 1;
int maxHeight = height + 2;
height = Random.Range(minHeight, maxHeight);
int minStoneSpawnDistance = height - minStoneheight;
int maxStoneSpawnDistance = height - maxStoneHeight;
int totalStoneSpawnDistance = Random.Range(minStoneSpawnDistance, maxStoneSpawnDistance);
//Perlin noise.
for (int y = 0; y < height; y++)//This will help spawn a tile on the y axis
{
if (y < totalStoneSpawnDistance)
{
spawnObj(stone, x, y);
}
else
{
spawnObj(dirt, x, y);
}
}
if(totalStoneSpawnDistance == height)
{
spawnObj(stone, x, height);
}
else
{
spawnObj(grass, x, height);
int t = Random.Range(1, treeChance);
if (t == 1)
{
//generate a tree
spawnObj(tree, x, height + 2);
}
int s = Random.Range(1, snowBiomeChance);
if (s == 1)
{
grass = snow;
snowBiomeLength = snowBiomeLength + 1;
}
if (snowBiomeLength <= 10)
grass = oldGrass;
snowBiomeLength = 0;
}
}
}
void spawnObj(GameObject obj,int width,int height)//What ever we spawn will be a child of our procedural generation gameObj
{
obj = Instantiate(obj, new Vector2(width, height), Quaternion.identity);
obj.transform.parent = this.transform;
}
public void Reload()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}