Game
Memory Breach
8 years ago

Creating Randomize Tiles for Unity 2D Tilemaps


I want to go over how I’m laying down random floor tiles in some of the levels in Memory Breach.

Outcome:

5d0a686704a73.png

You can see how there are different floor tiles scattered about, but I definitely did not take time to “randomly” lay down different tiles to accomplish this.

By using Unity’s base Tile class, I can create a special tile that using logic to lay down sprites.

Code:

		
			public class PrisonTile : Tile {

    [SerializeField]
    private Sprite[] prisonSprites;

    [SerializeField]
    private Sprite preview;

    public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    {
        var floor2 = 15.0f;
        var floor3 = 30.0f;
        var floor4 = 32.0f;
        var floor5 = 34.0f;
        var floor6 = 36.0f;
        var floor7 = 38.0f;

        var choosen = Random.Range(0.0f, 300.0f);

        if (choosen >= 0f && choosen < floor2)
        {
            tileData.sprite = prisonSprites[1];
        }
        else if (choosen >= floor2 && choosen < floor3)
        {
            tileData.sprite = prisonSprites[2];
        }
        else if (choosen >= floor3 && choosen < floor4)
        {
            tileData.sprite = prisonSprites[3];
        }
        else if (choosen >= floor4 && choosen < floor5)
        {
            tileData.sprite = prisonSprites[4];
        }
        else if (choosen >= floor5 && choosen < floor6)
        {
            tileData.sprite = prisonSprites[5];
        }
        else if (choosen >= floor6 && choosen <= floor7)
        {
            tileData.sprite = prisonSprites[6];
        }else
        {
            tileData.sprite = prisonSprites[0];
        }

    }

#if UNITY_EDITOR
    [MenuItem("Assets/Create/Tiles/PrisonTile")]
    public static void CreatePrisonTile()
    {
        string path = EditorUtility.SaveFilePanelInProject("Save Prisontile", "New PrisonTile", "asset", "Save prison tile", "Assets");
        if(path == "")
        {
            return;
        }

        AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<PrisonTile>(), path);
    }
#endif
}
		
	

Now there are many aspects to this class, but overall its pretty simple. There are two parts I want to go over specifically: private Sprite[] prisonSprites and public override void GetTileData

private Sprite[] prisonSprites holds a list of sprites that we pick from when we randomly lay down a sprite. As you can see in the Inspector below, I just state how many sprites will be in the list and then assign the sprites in the list item spots.

5d0a68677829d.png

Lets move to public override void GetTileData. This is where we actually write some logic to pick which sprite to lay down.

var floor2 = 15.0f; var floor3 = 30.0f; var floor4 = 32.0f; var floor5 = 34.0f; var floor6 = 36.0f; var floor7 = 38.0f;

There are total of 7 different floor sprites I pick from. Here we only see 2 - 7 and this is because the first sprite is our default sprite to lay down. Floor 2 and 3 both have a 5% chance of being placed and Floors 4 - 7 only have a 0.6% chance. Lets look at how we determine the odds of a tile being placed.

var choosen = Random.Range(0.0f, 300.0f);

We choose a random float number between 0 and 300. Now look at the if statement.

The first condition states if (choosen >= 0f && choosen < floor2). This means if choosen is greater than or equals 0 and is less than floor2, aka 15.0, then we can place a Floor 2 tile.

Note that 15 divided by 300 = 0.05 or 5%. If we choose 0 to 100 then it would be 15%. I did originally start with 0 to 100 to keep it simple at first, then modified values to hone in what I thought looked good.

We do this type of comparison for each of the non-main tiles. If choosen is greater than or equal to previous floor tile value and is less than current floor tile value, then lay down current floor tile.

If no condition is met, then lay down main floor tile.



0 comments

Loading...

Next up

Title Screen

Just as Pecaminosa's musicians #improvise this rendition of #LittleSunflower 🌻, so do we improvise with this post you're reading. 😂

What's coming out of your own improvs? 🤔

#Pecaminosa | #IndieDev | #OST | #Jazz

Another house i made long time ago.

I have added rewards for quests so that villagers can give you something in return for your hard work helping them. 🥳

I want to know - what's your favourite quest reward?

Quantum precognition is one of the most powerful cards. End game cards & equipment. Demo/alpha build boss is no match for this deck.

Back in my art school days I used to ride the 710 COPSA line from Parque Del Plata to Montevideo almost everyday. This is the Marcopolo Viaggio G4 Mercedes Benz model from the late 80s, one of the older bus models that was running on the line.

Why walk when you can jump?

Spaaaace~

These are background sprites I've created for a game I'm working on at school ^w^ Click on the post to see how the sprites connect. You won't regret it! (personally, I think it's pretty heheh)

Drawn in Piskel using my mouse. Whaddya think?

If you have more of an acquired taste, the restaurants in Niravasi have you covered! Maybe skip the salad bar, though.

I mostly build 3D First Person Shooter game mechanics in Unity, and in this I'm just showing C4 mechanics I made for a little side project I am working on! :)