In this tutorial we will build a terrain generator for Unity 3D.
The terrain generator will have the following features:
- It will work in the editor as well as at runtime.
- It will be able to create virtually endless terrains.
- It will not only create the terrain but also ground textures, grass and trees.
- It will have a couple of post processing steps to further tweak the shape of the terrain.
In this first step we will setup the Unity project that we will use in all further steps.
Note: I am using Visual Studio for code editing but you can use any editor you like. And I am still using Unity 4.x but the tutorial should also work with Unity 5.x.
|Create a new Unity project and name it TerrainGen.
|Import the two Unity standard assets „Character Controller“ and „Terrain Assets“. (Assets/Import Package/Character Controller and Assets/Import Package/Terrain Assets in the menu).
|Create a new terrain (GameObject/3D Object/Terrain in the menu).
|Assign one of the standard Terrain Textures of the Terrain Assets to the first texture of the terrain. („Grass (Hill)“ works good.) Unity will automatically paint the whole terrain with this texture.
|Create a new C# script and name it „TerrainGenerator„. Attach the script to the terrain you created. For the sake of simplicity we will put all the functionality into one script – at least in the basic steps. When it comes to configuring the generation process we will add some more classes.
|Create a directional light (GameObject/Light/Directional Light in the menu).
|Create a First Person Controller from the Character Controller asset. Position it at 1000/2/1000. This puts the controller in the middle of the terrain and slightly above the ground.
|Position the view in the editor so that you can see the whole terrain. It should be looking something like this:
|Save the scene and name it „main“ (or whatever you like). Run the scene and you should be able to move around in the (very flat) terrain:
That’s it for now – we’re set up for the following steps.