I'm trying to create a city in Unity. I don't want to spend a lot of time on the prototype.
I was wondering if there is a way to assemble a tileable chunk of the city and repeat the opposite edge on the fly if the player approaches an edge. The tileable part would probably be a few city blocks long and wide. This is just to give the player a sense of a never ending city without building one.
I've searched around the questions and LoadLevelAdditive() seems like what I am looking for. Do I just load the scene and as the player reaches one edge of it load the same scene again adjacent to current scene? How would I position it?
asked Feb 04 '11 at 09:37 AM
I'm not sure LoadLevelAdditive is the best approach here, since it doesn't handle positioning and it would also mean that your city just keeps on growing.
Instead I would suggest to have 4 instances of the tile block active from the beginning in a 2x2 grid.
When the player (*) approaches the right side of the 2x2 grid (for example the right side of tile B), move the two tiles at the left (A and C) over on the right, i.e. move them a distance of 2 tiles, effectively shifting the grid by 1 tile:
Same for the other 3 directions. For example, if the player then moves up and appraoches the top of the 2x2 grid, shift the bottom two tiles up.
This way you can keep the player within your grid and never need more than 4 tiles active at a time.
Note that this method also works for bigger grid sizes. You could have a 3x3, 4x4, or even 10x10 grid of tiles that constantly shift the rows and columns at the edges of the grid to keep the grid approximately centered around the player.
Update - Technical Implementation Hints
Demonstrating a working solution in code or pseudo-code is beyond the scope of this answer. With a solid understanding of two-dimensional arrays it's not too hard.
If it seems too tricky, I'd suggest to first start with the simpler problem of doing it in 1D: Have a 1D array of active tiles, for example along the X axis.
The "potential world" would be like this
And the instanced list would hold X number of those (for example 3):
If the player moves sufficiently to the right, the whole world is shifted. It would work like this:
The tile in the first cell is destroyed:
The first cell copies the tile reference stored in the second cell:
The second cell copies the tile reference stored in the third cell:
The new tile is created and stored in the third cell:
Now the world is shifted by one.
When shifting the other way, the process needs to be done from right to left instead.
Extending this to 2D is not really different - if the process above is for one row in a grid, it just needs to be repeated for each row. Then when shifting vertically, the same procedure is done for a column and repeated for each column.