- Home /
How do I make a scene tileable?
Hello everyone!
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?
Thanks!
Answer by runevision · Feb 04, 2011 at 09:52 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.
A B *
C D
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:
B * A
D C
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.
Before:
* B A
D C
After:
D C
*
B A
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
A B C D E F G
And the instanced list would hold X number of those (for example 3):
[C] [D] [E]
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:
[ ] [D] [E]
The first cell copies the tile reference stored in the second cell:
[D] [D] [E]
The second cell copies the tile reference stored in the third cell:
[D] [E] [E]
The new tile is created and stored in the third cell:
[D] [E] [F]
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.
Yup, exactly what I suggested in the OP's cross-post to the forums: http://forum.unity3d.com/threads/76635-How-do-I-make-a-scene-tileable. (@The OP: When posting to both UA and the forums, it's nice if you can cross-link the posts so that people don't duplicate their efforts in answering.)
I posted a follow up as a solution below. Can't login with SirTimmyTimbit for some reason. I'd really appreciate your help again :)
Your answer
Follow this Question
Related Questions
Procedural Texturing on Multiple Terrain Mesh 1 Answer
How to repeat a scene in unity3d. 1 Answer
Texture repeat not working! 4 Answers
Snapping models 3 Answers
repeating textures (how to solve it) 3 Answers