x


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!

more ▼

asked Feb 04 '11 at 09:37 AM

SirTimmyTimbit gravatar image

SirTimmyTimbit
13 1 1 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Feb 04 '11 at 09:52 AM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

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.)

Feb 04 '11 at 10:18 AM Jesse Anders

Sorry about that, I'm a total newb :)

Feb 04 '11 at 09:25 PM SirTimmyTimbit

I posted a follow up as a solution below. Can't login with SirTimmyTimbit for some reason. I'd really appreciate your help again :)

Mar 17 '11 at 08:09 AM AnotherTimmyTimbit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x719
x99
x42
x37
x16

asked: Feb 04 '11 at 09:37 AM

Seen: 908 times

Last Updated: Feb 04 '11 at 09:37 AM