The terrain system in Unity helps you create large outdoor environments easily. It lets you shape hills, valleys, and paint textures to make realistic landscapes.
0
0
Terrain system basics in Unity
Introduction
When you want to build a natural outdoor scene like mountains or forests.
When you need a large area for a game level without modeling every detail by hand.
When you want to add trees, grass, and details quickly on a big surface.
When you want to create walkable ground with height changes for characters.
When you want to optimize outdoor scenes with built-in terrain tools.
Syntax
Unity
Terrain terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent<Terrain>(); terrain.terrainData = new TerrainData(); terrain.terrainData.size = new Vector3(width, height, length);
Use
TerrainData to define the shape and textures of the terrain.The
size property sets the width, height, and length of the terrain in world units.Examples
This creates a flat terrain of 500x500 units with a height of 100 units.
Unity
TerrainData terrainData = new TerrainData(); terrainData.size = new Vector3(500, 100, 500); Terrain terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent<Terrain>();
This code uses Perlin noise to create smooth hills on the terrain.
Unity
float[,] heights = new float[512, 512]; // Set heights to create hills for (int x = 0; x < 512; x++) { for (int y = 0; y < 512; y++) { heights[x, y] = Mathf.PerlinNoise(x * 0.01f, y * 0.01f) * 0.5f; } } terrain.terrainData.SetHeights(0, 0, heights);
This adds a tree to the terrain at the center position.
Unity
terrain.terrainData.treePrototypes = new TreePrototype[] { new TreePrototype() { prefab = treePrefab } };
terrain.AddTreeInstance(new TreeInstance() { position = new Vector3(0.5f, 0, 0.5f), prototypeIndex = 0, widthScale = 1, heightScale = 1, color = Color.white, lightmapColor = Color.white });Sample Program
This script creates a terrain with smooth hills using Perlin noise when the game starts. The terrain is 200 units wide, 50 units tall, and 200 units long.
Unity
using UnityEngine; public class SimpleTerrain : MonoBehaviour { void Start() { TerrainData terrainData = new TerrainData(); terrainData.heightmapResolution = 513; terrainData.size = new Vector3(200, 50, 200); float[,] heights = new float[513, 513]; for (int x = 0; x < 513; x++) { for (int y = 0; y < 513; y++) { heights[x, y] = Mathf.PerlinNoise(x * 0.01f, y * 0.01f) * 0.3f; } } terrainData.SetHeights(0, 0, heights); Terrain terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent<Terrain>(); terrain.transform.position = Vector3.zero; } }
OutputSuccess
Important Notes
Terrain height values range from 0 to 1, representing relative height compared to the terrain's max height.
Heightmap resolution must be 2^n + 1 (like 513) for proper terrain detail.
Use the Unity Editor's Terrain tools for easy painting and sculpting if you prefer visual editing.
Summary
The Unity terrain system helps build large outdoor scenes quickly.
You create terrain by setting size and height data with TerrainData.
Perlin noise is a simple way to make natural-looking hills programmatically.