- Home /
Randomly generated/ procedurally generated world
Hi guys, I'm relatively new to untiy, I know some of the basics and I have barely begun to learn java (but I am, its a work in progress), and I was wondering if any of you knew how I could make randomly generated terrain, like in minecraft. Now I know what your thinking, I want to make a minecraft clone, but no, I just want a randomly generated world. I dont even want it to be cubes, just normal unity terrain, but created randomly. Thanks guys Tom
Answer by Rabbit-Stew-dio · Apr 04, 2012 at 11:22 PM
Have a look at the "Terrain Toolkit" Unity offers as add-on.
It contains the standard algorithms (Perlin noise, etc) to build terrains. It comes with full source code and is a good way to start experimenting. The way the toolkit manipulates terrain objects in the editor can easily be used when the game is running.
Once you have one terrain up and running, you just have to create terrain objects on the fly, position them and then connect them with their neighbouring terrains. If you make a large world, then you will have to destroy terrains that are out of scope, or you run out of memory pretty soon.
This paper gives you a great overview on how to produce a terrain, the techniques and what is working and what is not working. In combination with the terrain toolkit sources, you can't go wrong.
But how do you actually do that? What do you put in the script editor to make it do a new terrain get generated when you are in a certain position?
In my code, I have an active set of a few terrains (25 -> 5 x 5). Each terrain is 500m x 500m in side.
Now I can compute what set of terrains I need by taking my camera's position divided by 500. That gives me a terrain-index in a grid.
terrain_grid_x = (int) Camera.main.transform.position.x / 500;
terrain_grid_y = (int) Camera.main.transform.position.z / 500;
When you leave your current terrain cell (the new terrain_grid_x is different from the old one), you can move the existing terrains that are still visible on their new positions, and then you generate the new terrains for the now empty cells.
Lets assume my terrains have the numbers, and lets say I have 9 terrain cells. The central terrain '5' is where your character is walking on. The terrains around it make sure that you do not see empty space when the camera looks at the horizon.
1 2 3
4 5 6
7 8 9
When my character moves downwards, he would cross from terrain 5 to terrain 8. At that point, I move the terrains that are still visible around, so that my character is still centred. So the row "4 5 6" moves into the first row, and row "7 8 9" moves into the centre row. The whole terrain grid now looks like this:
4 5 6
7 8 9
- - -
Now generate the missing terrains and after a while, your character is happily walking again.
(I use a 5x5 grid so that I can generate the terrains in the background. The terrain generation time is far less than the time needed to walk from the top edge to the bottom of the centre tile.)