Border Image
Border Image

Procedural Generation

Programming and System Design · 10 minute read

Creating a dynamic PCG world that adapts based on economy, AI, and weather + seasonal changes.

Border Image

To further enhance the immersion of the world, create an Endless Globe around it.



Part 0: Theory

When approaching procedurally generated worlds within Unity, there are two simple techniques that I've come across. The first being a simple Grid, assigning various prefabs such as grass, rocks, and trees, and instantiating them randomly. The second is slightly more complex, creating a randomised terrain based on Perlin Noise, before then painting certain textures on top. In this post, I'll cover how to do both, with the complexity and design falling completely on what you, the developer, can expand upon.


PCG Image

Part 1: Instantiated Grid

To implement a simple PCG into Unity we create some prefabs with various variables and just place them around. A simple way we can add complexity to this design is assigning each item at least two or three different objects, and upon instantiating leaving one active at random. This can further be expanded by assigning seasons and passage of time, potentially even having the trees change colour as the years go by. To start, create a simple "for" loop, running through the width and height of the grid, and adding a terrain prefab in each spot. In order to add variety, create a random number generator to determine what prefab to create in each coordinate, as well as some checks to allow water or certain collisions to not occur.


Astar Image

Voila! It really is that simple, the complexity and design depending completely on how it's utilised and the creativity behind it.


Astar Image


Part 2: Procedural Terrain

Terrains within Unity have always been a slight peeve of mine especially as someone who enjoys creating their custom physics. Having ground that is wobbly in some places and wobblier in others is practically begging for players to abuse wall clipping and gravity exploits. This is a problem that I still haven't fully figured out how to fix, but by limiting the maximum depth of the terrain as well as the size, you can make the world seem large whilst containing fewer polygons, preventing the terrain from getting in the way of the core mechanics, whilst remaining realistic. This is where Perlin Noise comes into play, creating smoother transitions and scales seamlessly, and it seems to work quite well. Below is an image of the terrain in practice, with some texture and height overlap meshing together.


Astar Image

To start, create a simple Terrain in Unity and inside the settings set the Terrain size, as well as Mesh Resolution and any other detail you prefer. Within the Texture settings, start creating some terrain textures that you're planning on using, making sure to order them in a way that makes sense for when you call them in code.


Astar Image

Inside the script call the Terrain component as well as assigning a public Depth, and Scale variable. The Depth consists of how high the terrain goes. The Scale is how focused the Perlin Noise will be. These values should be experimented with to get a consistent visual. Next, in order to generate the terrain, assign the TerrainData of the Terrain and the heightmap, calculating where on the map each variance should be, before applying the Perlin Noise to that specific scale. The second image below may seem slightly convoluted but is in fact simply defining where on the terrain each variance should be based on the x and y positions. In the centre, the concentration will be the strongest due to the multiplier variable, yet as it slowly expands that number will slowly decrease, allowing for a smooth transition. This, of course, doesn't have to only be a circle, with the concentration dependant on whatever you decide.


Astar Image

Astar Image

Astar Image

Painting the Terrain works exactly the same, defining where on the script you want to apply the splat weights, and creating a multiplier to strengthen its effect on the centre, slowly fading it out.


Astar Image

This is how the terrain looks like with four distinct biomes, each with varying heights and textures, yet remain seamless as you transition between them within the game.


Astar Image

Both of these methodologies could even be joined together, creating a random terrain and then instantiating resources such as trees and rocks directly on top, allowing a completely random world to be created within Unity. Utilising my Saving and Loading system, you can also set it up to save the Perlins offset, loading them once rejoining a game. I plan to eventually post all of these experiments onto my GitHub, but for now, if you have any questions please don't hesitate to get in touch.



Border Image