- Home /
Question by
Chazzwazzler · Aug 30, 2020 at 02:02 AM ·
c#2dtilemapgeneration
2D world generation script generating strange world made up of large lines
When I generate the world with my world generation script, it generates worlds like this:
When it is supposed to look like this:
Whats wrong with my script that is causing this? I have been testing for several days and still cannot find out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class worldGeneration : MonoBehaviour
{
//The types of tiles
public Tile[] tiles;
//World generation properties//
//how many neighbours of the same type of tile there needs to be for a tile to convert
public int sameNeighbourRequirment = 4;
//how many cycles of smoothing are done after generating noise
public int smoothCycles = 0;
//the width in tiles of the original world before scale up
public int width = 0;
//the height in tiles of the original world before scale up
public int height = 0;
//Other//
//default tilemap for the world
public Tilemap tilemap;
void Start()
{
//generate basically random noise with all the possible tiles (likeliness of certain tiles vary)
tileNoise();
//smooth out that noise
cellularAutomatica(smoothCycles);
}
//Generates noise out of the tiles
void tileNoise()
{
//gos through all possible tile spots
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
//places random tile
Vector3Int pos = new Vector3Int(x, y, 0);
int rand = Random.Range(0,tiles.Length);
tilemap.SetTile(pos, tiles[rand]);
}
}
}
//finds the count of a certain type of tile surounding a tile at a selected position
int findNeighbourTypeCount(int posX, int posY, Tile tileType)
{
int tileTypesCount = 0;
//gos through all tiles in a 3x3 area centered around the posX and posY variables inputted into the function
for (int neighbourOffsetX = -1; neighbourOffsetX < 2; neighbourOffsetX++)
{
for (int neighbourOffsetY = -1; neighbourOffsetY < 2; neighbourOffsetY++)
{
//finds the position of the neighbour tile
int neighbourXPos = posX + neighbourOffsetX;
int neighbourYPos = posY + neighbourOffsetY;
Vector3Int neighbourPos = new Vector3Int(neighbourXPos, neighbourXPos, 0);
//checks if the neighbour tile is not actually the tile we are finding the neighbour stuff for
if (neighbourXPos != 0 && neighbourYPos != 0)
{
//checks if the neighbour is the type of tile inputed into the function and adds to the count of that tile if it is
if (tilemap.GetTile(neighbourPos) == tileType)
{
tileTypesCount++;
}
}
}
}
return tileTypesCount;
}
//smooths out everything
void cellularAutomatica(int cycles)
{
//repeats the code for cycles amount
for (int cyclesI = 0; cyclesI < cycles; cyclesI++)
{
//gos through all possible tile positions
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
//gos through all possible types of tiles
for (int i = 0; i < tiles.Length; i++)
{
//changes the type of tile that the tile is if the neighbour type count is more than or equal to the same neighbour requirment
if (findNeighbourTypeCount(x, y, tiles[i]) >= sameNeighbourRequirment)
{
Vector3Int pos = new Vector3Int(x, y, 0);
tilemap.SetTile(pos, tiles[i]);
}
}
}
}
}
}
}
screenshot-109.png
(129.2 kB)
50x50-3.png
(97.0 kB)
Comment