Memory Breach is 2D Action RPG set in a Cyberpunk world falling into chaos from Lovecraftian Horrors. "The world is in free fall. People claim to see visions from other worlds and hear voices in unheard languages. The powers that be fight to keep society in motion. At the first sign of corruption, the sick are sentenced to await death in the prison. Amid the cries of addled brains and gnashing teeth, you wake up. With only the memory of a woman’s face, you must seek out the cause of the corruption and discover why you seem to be miraculously immune despite a world falling into chaos. As you travel, you’ll meet allies and enemies, and those whose motivations are harder to pin down. There’s talk of a world after this one. Of Gods and Monsters outside comprehension. Surely that’s just the ramblings of crazed cultists and the weak-minded masses. Or is there a hint of truth in the shouts of madmen?"

  • View media
  • View media
  • View media
  • View media
  • View media
  • View media
Post article RSS Articles

Creating Randomize Tiles for Unity 2D Tilemaps

Mapping/Technical Tutorial

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

Outcome:

Random Tiles


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.

PrisonTileInspector


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.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account:

X