- Home /
How To Add Fallof to perlin?
So recently I have been experimenting with Perlin noise and wanted to start making some islands using a tile-based system. And so I found a youtube tutorial that helped me make a Perlin tiled based environment because when it comes to programming I'm still very much an amateur. However, I never figured out how to make an island until recently and it turns out that I need to add a fall of map. The problem is I don't know how to do that with the code I have. And so id like to ask if someone could help me out with implementing some sort of fall off map in my code that can help create an island or islands.
public class MapFunction : MonoBehaviour
{
Dictionary<int, GameObject> tileset;
Dictionary<int, GameObject> tile_groups;
public GameObject prefab_snow;
public GameObject prefab_snowForest;
public GameObject prefab_mountain;
public GameObject prefab_closeocean;
public GameObject prefab_ocean;
int map_width = 320;
int map_height = 180;
List<List<int>> noise_grid = new List<List<int>>();
List<List<GameObject>> tile_grid = new List<List<GameObject>>();
// recommend 4 to 20
float magnification = 50.0f;
int x_offset = 1; // <- +>
int y_offset = 1; // v- +^
void Start()
{
CreateTileset();
CreateTileGroups();
GenerateMap();
}
void CreateTileset()
{
/** Collect and assign ID codes to the tile prefabs, for ease of access.
Best ordered to match land elevation. **/
tileset = new Dictionary<int, GameObject>();
tileset.Add(4, prefab_mountain);
tileset.Add(3, prefab_snowForest);
tileset.Add(2, prefab_snow);
tileset.Add(1, prefab_closeocean);
tileset.Add(0, prefab_ocean);
}
void CreateTileGroups()
{
/** Create empty gameobjects for grouping tiles of the same type, ie
forest tiles **/
tile_groups = new Dictionary<int, GameObject>();
foreach (KeyValuePair<int, GameObject> prefab_pair in tileset)
{
GameObject tile_group = new GameObject(prefab_pair.Value.name);
tile_group.transform.parent = gameObject.transform;
tile_group.transform.localPosition = new Vector3(0, 0, 0);
tile_groups.Add(prefab_pair.Key, tile_group);
}
}
void GenerateMap()
{
/** Generate a 2D grid using the Perlin noise fuction, storing it as
both raw ID values and tile gameobjects **/
for (int x = 0; x < map_width; x++)
{
noise_grid.Add(new List<int>());
tile_grid.Add(new List<GameObject>());
for (int y = 0; y < map_height; y++)
{
int tile_id = GetIdUsingPerlin(x, y);
noise_grid[x].Add(tile_id);
CreateTile(tile_id, x, y);
}
}
}
int GetIdUsingPerlin(int x, int y)
{
/** Using a grid coordinate input, generate a Perlin noise value to be
converted into a tile ID code. Rescale the normalised Perlin value
to the number of tiles available. **/
float raw_perlin = Mathf.PerlinNoise(
(x - x_offset) / magnification,
(y - y_offset) / magnification
);
float clamp_perlin = Mathf.Clamp01(raw_perlin); // Thanks: youtu.be/qNZ-0-7WuS8&lc=UgyoLWkYZxyp1nNc4f94AaABAg
float scaled_perlin = clamp_perlin * tileset.Count;
// Replaced 4 with tileset.Count to make adding tiles easier
if (scaled_perlin == tileset.Count)
{
scaled_perlin = (tileset.Count - 1);
}
return Mathf.FloorToInt(scaled_perlin);
}
void CreateTile(int tile_id, int x, int y)
{
/** Creates a new tile using the type id code, group it with common
tiles, set it's position and store the gameobject. **/
GameObject tile_prefab = tileset[tile_id];
GameObject tile_group = tile_groups[tile_id];
GameObject tile = Instantiate(tile_prefab, tile_group.transform);
tile.name = string.Format("tile_x{0}_y{1}", x, y);
tile.transform.localPosition = new Vector3(x, y, 0);
tile_grid[x].Add(tile);
}
}
Your answer
Follow this Question
Related Questions
How to scale perlin noise 2 Answers
[C#] Wondering what is amiss with my 1D Perlin Noise Terrain Generator? 2 Answers
Generating Chunks of Ore Using Perlin Noise 3 Answers
Always same result from Mathf.PerlinNoise() 2 Answers
How to create a perlin noise heightmap with the values being the highest around a point? 1 Answer