Random MAP generation method in Unity [2D game]

4 minute read

With Unity Learn
Roguelike games available.

2D Roguelike

Of this
・ MAP random generation
・ Random generation of objects
I will explain the part.

This part is the liver of roguelike
Above all, it is a technique that can be reused in other games!

The environment is
MacOS Catalina:ver 10.15.4

MAP & object randomization

Random code


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Write a script for Map generation
public class BoardManager : MonoBehaviour
    //Class that determines the minimum and maximum values of randomly generated items on the map
    public class Count
        public int minmum;
        public int maximum;

        public Count(int min,int max)
            minmum = min;
            maximum = max;

    //Vertical and horizontal of Map
    public int columns = 8;
    public int rows = 8;

    //Number of items to generate
    public Count Wallcount = new Count(3, 9);
    public Count foodcount = new Count(1, 5);

    //MAP material
    public GameObject exit;
    public GameObject floor;
    public GameObject Wall;
    public GameObject OuterWall;
    public GameObject enemy;
    public GameObject food;

    //For organizing Objects(Set as the parent of a randomly placed object)
    private Transform boardHolder;

    //Manage places where there are no objects in 6x6 squares
    private List<Vector3> gridPositons = new List<Vector3>();

    void Start()
        //Map generation

        //Clear and reacquire gridPositions

        //Randomly generate walls
        LayoutObjectAtRandom(Wall, Wallcount.minmum, Wallcount.maximum);

        //Food generation
        LayoutObjectAtRandom(food, foodcount.minmum, foodcount.maximum);

        //Installation of exit
        Instantiate(exit, new Vector3(columns - 1, rows - 1, 0), Quaternion.identity);

    //Field generation
    void BoardSetup()
        //Instantiate Board and set it as boardHolder
        boardHolder = new GameObject("Board").transform;

        for (int x = -1; x < columns + 1; x++)
            for (int y = -1; y < rows + 1; y++)
                //Install the floor and prepare for instantiation
                GameObject toInsutantiate = floor;

                //Prepare for instantiation by installing an outer wall outside the 8x8 square
                if (x == -1 || x == columns || y == -1 || y == rows)
                    toInsutantiate = OuterWall;

                //Instantiate what is set to toInsutantiate
                GameObject instance =
                    Instantiate(toInsutantiate, new Vector3(x, y, 0), Quaternion.identity) as GameObject;

                //Set the parent element of the instantiated floor or outer wall to boardHolder
    //Clear gridPositions
    void InitialiseList()
        //Clear the list

        //Get 6x6 Masu to list
        for(int x = 1; x < columns -1; x++)
            for(int y = 1;y < rows -1; y++)
                //x in gridPositions,Enter the value of y
                gridPositons.Add(new Vector3(x, y, 0));


    //Get random positions from gridPositions
    Vector3 RandomPosition()
        //Declare randomIndex and randomly enter a number from the number of gridPositions
        int randomIndex = Random.Range(0, gridPositons.Count);

        //Declare randomPosition and set it to randomIndex of gridPositions
        Vector3 randomPosition = gridPositons[randomIndex];

        //Remove used gridPositions element

        return randomPosition;

    //Randomly place arguments on the Map(Enemies, walls, items)
    void LayoutObjectAtRandom(GameObject tile,int minimum,int maximum)
        //Randomly determine the number of items to generate from the minimum and maximum values, and set it to objectCount.
        int objectCount = Random.Range(minimum, maximum);

        //Loop for a few minutes of the object to be placed
        for(int i = 0; i < objectCount; i++)
            //Get a random position where no object is currently placed
            Vector3 randomPosition = RandomPosition();

            Instantiate(tile, randomPosition, Quaternion.identity);

Code commentary

I’m using a loop to generate a field

See the figure below

スクリーンショット 2020-09-01 20.30.28 2.png

** The entire field
Generated in 10x10 **,
The outermost is
This is the place to install the indestructible ** Outer Wall **.

** 8 × 8 ** is the range that the player can move,
Moreover, the place where ** floor ** is installed.

The position where the player is currently standing
Because it is (0,0)
The outer wall is placed
This is when there is a -1 or 8 position on either or both of x and y.

So start the loop from -1 and let the loop run to the number 8 which is advanced by 10.
If x, y contains -1 or 8, OuterWall,
Other than that, I try to place a floor.


The 6x6 location in the image above
** Keep as a number in the list (gridPositons) **

The numbers held in this gridPositons are
There is no object.

Use this list when placing items
Grasp the position where there is no Object and place it.


to randomIndex
From the number of elements at the position held in gridPositons
Randomly select and set a numerical value.

of gridPositons to randomPosition
Set the location held in randomIndex.

Delete the value once set to randomPosition,
I try not to put items in duplicate.
スクリーンショット 2020-09-26 11.12.10.png


Determined for each item,
Randomly determine the number to place the item from the minimum and maximum values.

Turn in a loop for a fixed number objectCount,
Install in randomPosition.


I explained about random generation of MAP.
Techniques that will continue to be useful for game development
I’m thinking of posting!

Also on my blog
** We explain carefully from 1 so that even beginners can develop games. ** **
How to make a roguelike game
If you want to know from 1, please take a look at the blog!
Peek into the blog