x


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

more ▼

asked Apr 04, 2012 at 06:15 PM

ostrich160 gravatar image

ostrich160
38 4 8 14

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

1 answer: sort voted first

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.

more ▼

answered Apr 04, 2012 at 11:22 PM

Rabbit-Stew-dio gravatar image

Rabbit-Stew-dio
633 1 1 17

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?

Dec 03, 2012 at 08:04 PM PsychoticBob

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

Dec 03, 2012 at 09:08 PM Rabbit-Stew-dio
(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:

x1045
x208
x42
x33

asked: Apr 04, 2012 at 06:15 PM

Seen: 8871 times

Last Updated: Dec 03, 2012 at 09:08 PM