0
votes

I am new to Unity as well as C#. Yet, I am trying to make a simple 2D platform game where I made a prefab of an object called Block. What I want to be able to do is to create an array tile map with 0s and 1s where the 1s are blocks and 0s are nothing. Also, I don't want the tile map to be random. I want the blocks to be instantiated from another object called GameController. A perfect example of what I would like to achieve is something like this.

enter image description here

But I don't really know how to do this with an array. I want to keep things simple since I am trying to learn how unity and c# work. Any help would be appreciated.

2

2 Answers

0
votes

So you can use some assets from the asset store( for example: https://www.assetstore.unity3d.com/en/#!/list/2965-procedural-generation) This is a pretty hard challange. I would recomand this video: https://www.youtube.com/watch?v=k1pWpYEt2UE , but the closest one to what you want to achieve is this one: https://www.youtube.com/watch?v=gIUVRYViG_g Hope it helped.

0
votes

You can make a 2 dimensional array, e.g. int[40, 100] and loop through it twice, and if the number in the array is one, multiply your position in the array by the length or width of your block respectively. For example:

int[,] positions = new int[40,100];

for (int i = 0; i < 41; i++) {
    for (int j = 0; j < 100; j++) {
        if (positions[i,j] = 1) {
            GameObject temp = Instantiate(block, new Vector3(j * blockWidth, i * blockHeight, 0), Quaternion.identity) as GameObject;
        }
    }
}

It would take a long while to set all of the coordinates for an array this large, but you could loop through with parameters, or just do it the hard way if it is smaller. Otherwise, I would just try doing it without a script.