itch.io is community of indie game creators and players

Devlogs

A Guide on Procedural Generation in Unity

Forest of Freedom
A downloadable game for Windows

If you only need the Source Code, dive down to the end of this Guide to click on the Link, though I'd still recommend you to read the Guide.

After helping and subsequently being mentioned in a video of Overphil Dev, I decided to write this Guide on writing a powerful and simple to maintain Terrain Generation System, with a simple method of saving, it is currently in use in the Forest of Freedom Game.

THIS GUIDE IS FOR UNITY 2021.3.0f1 , I cannot guarantee it working for different versions, though it should unless drastic changes happened


This Guide will address writing a system for modifying the Unity Terrain based on Height & Alphamaps aswell as placing Gameobjects in the World, that can later be interacted by the player. The interaction system aswell as the saving system itself will not be mentioned in this guide, though they won't be too hard to get working.

First we have to think about the Systems used in this guide, some of them are poorly documented in the unity docs and so it might be quite confusing.

  1. "Terrain" is the physical gameobject that gets placed in the scene when you create a unity terrain
  2. "TerrainData" is the data this Terrain uses, it will automatically be generated as a prefab in your assets folder, but can still be modified, this holds most variables and functions that have an impact on our terrain.
  3. HeightMap is a float[ , ] (2Dimensional Array) with values between 0 and 1 and 1st and 2nd dimension describing the position of the float value, it has the resolution of TerrainData.heightmapResolution, which is the Array's length, for most use cases, it should be the same as TerrainData.heightmapHeight or TerrainData.heightmapWidth, so we will use this in this Guide.
  4. Alphamap is a float[ , , ] (3Dimensional Array) with dimension 1 and 2 describing the size of the Alphamap size and the 3rd dimension describing the Layer. This Value controls the color of the map later on, this for example is pretty badly explained in the docs. The values have to be between 0 and 1, with 0 being the lack of color and 1 being the picked Layer having a strength of 100%.

In this guide we will only cover a binary case of weather a layer is active or not, without talking about any amount of transitioning.

With the basics now covered, let's dive deeper into this system. We will set up a simple "island" with grass spawning above a certain height and Gameobjects getting spawned.

  • In the Hierarchy tab, under 3D Object > Terrain, generate a new Terrain, this will generate us a new Terrain alongside a TerrainData Prefab.

Thank you and have fun with your Generation System! I'd be happy to get credited, though it's not necessary!

All of the Mentioned code can be found on my Github at SpaceDave1337/TerrainGenerationSystem(link)

Click the following Link to see my newest Devlog, regarding the game this code originates from! (LINK)

Download Forest of Freedom
Read comments (2)