- Home /
How can I generate an infinite world as the player walks through it?
I've made a prefab wich generates other prefabs, to make a terrain. Now, I need to make this terrain infinite. What I thought of is to make a script that creates automatically the prefab-making prefab, but, to make the game not create the entire world once, I need to know if I can create an inactive by default prefab, and activate it only if it is in a range chosen by me or the player. Is it possible?
Answer by Tarlius · Jun 12, 2014 at 08:13 AM
You can't create an infinite world by instantiating inactive prefabs. You would need a (smaller) infinite number of them!
You'll probably want to make an algorithm that will generate terrain of a certain square size (which it sounds like you might have done already), and add (and remove!) them as the player moves around. Do not forget the remove part! ;)
A lot of open world games take this kind of approach (although typically they load data rather than generate it, of course)
Depending how big/detailed they are, you'll probably want at least 9 active at a time. One in the centre, 4 direct adjacents and 4 diagonal adjacents.
If you want to make all players have the same "random" world, you can set the seed for the random number generator you use to build the world.
$$anonymous$$aybe I didn't explain well what I meant: the map/chunk-making prefab should have a code that generates the same prefab on the 9 squares you talked of above. Although, now I find some things that will, according to $$anonymous$$urphy's laws, go wrong. Now I found a great tutorial to explain how to make the chunks, but I can't understand how can I make the game create chunks automatically. $$anonymous$$aybe using the distance between the player and the corner of the chunks? (Forgive grammatical errors, English is not my first language)
You could probably check the players distance from the center of the chunk and if he is a certain distance away then generate the next chunk.
How would someone go about creating a script that does this? I'm trying to achieve the same exact thing but I don't know how I could do it.
Answer by stuart6854 · Jun 12, 2014 at 08:18 PM
I have semi-infinite terrain generation(almost infinite) and the way i surround the player in them is at the start i make the first 9 tiles or so. Then when the player steps onto on one of those tiles the game will search in the 9(or so) tiles around that tile and if their is empty space it will create a new one.
Hope it makes sense.
Point to add:
I use a function to find the closest tile/chunk using vector2.distance(NOT vector3) but make sure your using the center of the tile/chunk for the distance
Yeah, it makes sense... maybe I'll try to do that with chunks, because using small tiles you can see lots of them. You can use nine tiles if you make something in the dark, like Slender, making the farthest fading to black. Thanks a lot! Edit: I've seen your comment just now... why Vector2 and not Vector3?
its up to you but in my game i use vector2 because i dont want to take into fact how high/low the player is just the how far away he is on 2d plane.
Answer by sradomski · Feb 09, 2016 at 05:12 PM
We have created thousands (>100k) of Unity terrains with different levels of detail from the SRTM data at NASA. There is a video of an interactive fly-over with 1/4th of the data at youtube, and another one with full resolution and an interpolated camera path. Frame-rate of the first video is slower as we calculate splatmaps dynamically, they are pre-calculated in the second video.
The challenge is the sheer size of it. Unity will throw warnings if you have any coordinates >100k and for a convincing impression and actual "walk-size", you'd need a size of 40.000.000 x 20.000.000. As such, you need to move the terrain under a camera fixed at (0,0,0), but this will complicate the placing of any other object.
You could parent the objects to the chunks. Then they would move with them.