U
tab 04World & Game Loop7 lessons — tap to open
01 / 07 · 10 min
World & Game Loop

Terrain and landscape

your project so far

Assets/

Scenes/

Arena.unity
FloorBlocks (parent)KnightTerrain

Scripts/

TerrainPlacer.cs

Prefabs/

Knight prefabEnemy.prefab

Materials/

terrain layers

all your scripts

you already havethis lesson adds

Arena.unity is still a grey box with your characters in it. This tab builds the world around it — around the arena you already have, not in place of it.

  1. 1

    Open Assets/Scenes/Arena.unity — the same file you have used since tab 01. Do not make a new scene: the floor, the blocks, the characters, the enemies and the camera rig all stay.

  2. 2

    Press Play before you touch anything. The Knight should walk, jump and cast exactly as it did at the end of tab 03.

  3. 3

    Do not rebuild anything. If pressing Play does not let you walk around, fix that before you sculpt a single hill.

Terrain is a single optimised mesh driven by a heightmap. You paint the height with a brush instead of modelling hills by hand.

animated diagram

The brush raises the heightmap; the mesh follows underneath it.

Create the terrain

  1. 1

    GameObject▸3D Object▸Terrain. A flat 1000×1000 m slab appears at the origin.

  2. 2

    That is far too big for one arena. In the Terrain Settings gear, set Terrain Width and Length to 200.

  3. 3

    Set Heightmap Resolution 513 and Detail Resolution 512 — plenty for a small level and much lighter.

  4. 4

    Move the terrain to -100, -0.05, -100. Its centre lands under the arena, and its surface sits just below the tab 01 floor, so the two never flicker against each other.

The five tools in the toolbar

  • Raise or Lower Terrain — left click raises, Shift + left click lowers. Start with a big soft brush at low opacity.

  • Set Height — click to flatten everything to an exact height. This is how you make a buildable plateau.

  • Smooth Height — removes the spiky noise your brush leaves behind. Use it constantly.

  • Paint Texture — layers of ground material: grass, rock, sand, path.

  • Paint Trees and Paint Details — trees are prefabs, details are grass billboards.

Painting layers

  1. 1

    Paint Texture → Edit Terrain Layers → Create Layer. The first layer covers the whole terrain.

  2. 2

    Set each layer's Tiling Size to 8–15 metres. The default of 2 makes an obvious repeating pattern.

  3. 3

    Add a rock layer and paint it only on the steep slopes; add a path layer with a small hard brush.

  4. 4

    Four layers maximum on mobile. Each layer is another texture sample per pixel.

Trees, grass and the wind

  • Trees painted with the Terrain tool get automatic billboarding at distance — far cheaper than placing prefabs by hand.

  • Set Tree Distance about 120 and Billboard Start about 60 in the Terrain Settings.

  • Grass is a Detail Mesh or a Grass texture. Detail Distance 40 on desktop, 20 or less on mobile.

  • A Wind Zone (GameObject > 3D Object > Wind Zone) makes trees and grass sway. One is enough for a whole level.

Reading terrain height from code

place anything on the ground
using UnityEngine;

public class TerrainPlacer : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    [SerializeField] private int count = 60;
    [SerializeField] private float area = 90f;

    void Start()
    {
        Terrain terrain = Terrain.activeTerrain;

        for (int i = 0; i < count; i++)
        {
            float x = Random.Range(-area, area);
            float z = Random.Range(-area, area);

            // exact ground height at that world position
            float y = terrain.SampleHeight(new Vector3(x, 0f, z)) + terrain.transform.position.y;

            Quaternion rot = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
            Instantiate(prefab, new Vector3(x, y, z), rot, transform);
        }
    }
}