Home > Software engineering >  Unity 2d infinite map generation with walls
Unity 2d infinite map generation with walls

Time:01-08

I trying to make 2d game in unity. I want to make something similar to guy in this video: https://youtu.be/OXRUEqbV2oM

I want to make world generation similar to his, but I was looking on some YouTube tutorials and didn't find how to do it.

If somebody can help me I would appreciate it.

I was looking all over the YouTube and internet and didn't find any way to do it. Even tutorial about procedural generation didn't help me.

Thanks

CodePudding user response:

You should make tiles(parts of your world), and randomly generate them.

Currently i'm trying to make it, it's problably going to take a while.

CodePudding user response:

This is what i made.

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

public class WorldBuilder : MonoBehaviour
{
    public GameObject[] Tiles;
    private float RandomNumber;
    private float PositionToSpawnX;
    public float PositionToSpawnY;

    private void Start()
    {
        RandomNumber = Random.Range(0, 3);
        PositionToSpawnX = 0;
        Destroy(gameObject, 5);
    }

    void Update()
    {
        if(RandomNumber == 0)
        {
            Instantiate(Tiles[0], new Vector3(PositionToSpawnX, PositionToSpawnY, 0), Quaternion.identity);
            RandomNumber = Random.Range(0, 3);
            PositionToSpawnX  = 5;
        }

        if (RandomNumber == 1)
        {
            Instantiate(Tiles[1], new Vector3(PositionToSpawnX, PositionToSpawnY, 0), Quaternion.identity);
            RandomNumber = Random.Range(0, 3);
            PositionToSpawnX  = 5;
        }

        if (RandomNumber == 2)
        {
            Instantiate(Tiles[2], new Vector3(PositionToSpawnX, PositionToSpawnY, 0), Quaternion.identity);
            RandomNumber = Random.Range(0, 3);
            PositionToSpawnX  = 5;
        }

        if (RandomNumber == 3)
        {
            Instantiate(Tiles[3], new Vector3(PositionToSpawnX, PositionToSpawnY, 0), Quaternion.identity);
            RandomNumber = Random.Range(0, 3);
            PositionToSpawnX  = 5;
        }
    }
}

You Get the tiles you've Made and put the in the Tiles Array, and run the game(my tiles are 5 unit in scale, if you want it to work you should make your tiles the same size as mine).

if you didn't understand something you can tell me (:

  • Related