- Home /
Infinite Forest Generation
Hi, I am kinda new to Unity, started toying with it last year. I want to know how to make a infinte forest. Basically like how Minecraft loads a new chunk, with it's infinte generation. I am making a simple forest survival game, where you can only leave and finish the game once you have certain items. And you will have the option to stay in the forest, again, like Minecraft, with it's ending. Thanks!
Answer by fafase · May 10, 2013 at 06:14 AM
You could use that answer http://answers.unity3d.com/questions/21197/adding-trees-dynamically.html to place your trees by script
Now, Minecraft is not really infinite, it is just really big. It is not possible to make an infinite world with a computer. You would start to get unaccuracy due to float accuracy while reaching large position. Minecraft often has an ocean to stop you from going any further (note that I am not familiar at all with Minecraft).
To place your trees, you could do that randomly as well as your relief but that would mean that if you come back to the same point, it looks different. The solution is to use a hash function based on the position to return a value that will define where everything is placed. As it is based on the position when you come back to the same position, the function returns the same values and places the same environment.
I hate the sound like a noob, but the second part just... I don't... what? I know what you mean, however the ocean in $$anonymous$$inecraft is very big, but eventually generates more land. Any chance of simple example script?
It is very big but it simply cannot be infinite.
A number in computer is either a float or a integer, they are limited because they are only 32bits they can at maximum represents a value of +4 billions (2 to the power of 32). Position are more likely to be float so without getting too deep that means among those 32 bits some are used for the decimal part.
As a result, you cannot access high values anymore, actually look at unity and make an object move in one direction non-stop, you will see a warning when it reaches about 10000. $$anonymous$$inecraft makes believe it is endless but they have to stop you from going in one direction or they load a new scene.
It is simply not technologically possible to recreate the universe in a computer.
For an example of hash function, well there are many of them, you could start learning about them http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200101/homework10/hashfuncs.html.
The most basic you could do is:
int Hash(Vector3 pos, int valRange){
float a = pos.x + pos.y + pos.z;
a *=100; // This is just here to make it look more random
a = $$anonymous$$athf.Round(a);
return (int)a % valRange; // valRange is 2 so that you get 0 or 1
}
The point of a hash function is to use some values to generate a result that seems to be random. It is like you give 1 and 2 and you get 25. Then 2 and 6 and you get 10. But if you enter again 1 and 2 you get 25 again. So it is not random.
In my example, whatever values you pass, you get 0 or 1 that you can use for tree or no tree.
You could tile your map with tiles so that you only perform the function every 1m to avoid getting trees too close or on top of each other
So far I just made a huge terrain and used the biggest tree brush, with variant settings. Still took 10 $$anonymous$$utes to finish it.
Fact is even though you could use the Hash function to put trees or not, you would have to access the terrain data to place the trees.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
map generation issues 0 Answers
Voxel based Terrain generation. Pointers please. 2 Answers
Minecraft (Cube) Terrain 1 Answer
Object Refrence Not Set To An Instance Of An Object? 2 Answers