How to make a basic infinite tilemap

Ok, first just a basic rundown of what I have:
The world is made up of tiles, that are all the same size, arranged in a square grid pattern. You don’t play as a character, but rather you move the camera around as you would in an RTS. So I have two tile types: Snow and forest. I have the tilemap displaying correctly (well, for the most part, curse you texture atlas!). But now I want to get a very basic infinite world going. I have a function called “GetTile(int x, int y)” that you feed an x and y to and receive the tile that should be at that position. For now it always returns the snow tile, so the entire world is just snow. In the end I want the world to be produced by a seed.

So with that in mind:

  1. Am I going about world generation in the right way, with my “GetTile” function? That way it can generate the world as you pan the camera around in it. Even if so, if you have any alternatives I would love to hear them! Always good to see multiple ways to tackle the problem.

  2. How would I go about making it so that each tile would have a 1/10 chance of being a forest tile, but is otherwise a snow tile, but each tile adjacent to a forest tile has a 1/2 chance of being a forest tile? I would like this to be derived from the seed, so that for the given ‘x’ and ‘y’ “GetTile” would return the same thing every time.

  3. Slightly more advanced: How would I make it so that there is a 1/10 chance of each tile being a forest tile, and then each adjacent tile to a forest tile has a 1/4 chance of also being a forest tile?

This will probably get a bit more technical than I think it is, but I wanted to start here before I moved on to something like perlin noise :slight_smile: Thanks in advance Unity Community, you have been a huge help to me so far! I hope I can pay this community back some day with my own knowledge.

I wanted to start here before I moved on to something like perlin noise

But using perlin noise is actually the simple way to go

Give it your x and y (multiplied by some scale), if you get a value 1/10 (0.1f) or less, use forest
You can’t reasonably implement a % chance for adjacent tiles being the same, because that would no longer be repeatable randomness

2 Likes

Oh interesting, I didn’t realize a Mathf function existed for that. Got it working rather well, we each seed giving a reasonably different result.

float scale = .1f;
float value = Mathf.PerlinNoise((GameManager.seed + x.ToString().GetHashCode()) * scale, (GameManager.seed + y.ToString().GetHashCode()) * scale);

        if (value <= .25f)
{
    return Tile.database["Forest"];
}
else
{
    return Tile.database["Snow"];
}

One point of interest is that a negative value will give you the same result, meaning that somewhere your world will be mirrored. I quick-fixed this by changing the x and y value into a string and getting the hash code of that string.

So I guess my first question still stands, am I going about this the right way or is there something obvious I am missing?