Bird
Raised Fist0
Unityframework~20 mins

Terrain system basics in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Terrain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code snippet?
Consider the following code that modifies a terrain's height at a specific point. What will be the height value printed in the console?
Unity
using UnityEngine;

public class TerrainTest : MonoBehaviour
{
    void Start()
    {
        Terrain terrain = GetComponent<Terrain>();
        float[,] heights = terrain.terrainData.GetHeights(0, 0, 1, 1);
        heights[0, 0] = 0.5f;
        terrain.terrainData.SetHeights(0, 0, heights);
        float newHeight = terrain.terrainData.GetHeight(0, 0);
        Debug.Log(newHeight);
    }
}
A1.0
B0.0
C0.5
DTerrain height in meters (depends on terrain size)
Attempts:
2 left
💡 Hint
Remember that terrain heights are normalized and scaled by terrain size.
🧠 Conceptual
intermediate
1:00remaining
Which Unity component is required to display a terrain in a scene?
You want to add a terrain to your Unity scene. Which component must be attached to the GameObject to render the terrain?
ATerrain
BMeshRenderer
CSpriteRenderer
DCanvas
Attempts:
2 left
💡 Hint
This component handles terrain rendering and collision.
🔧 Debug
advanced
2:00remaining
Why does this terrain height modification code not change the terrain?
Look at this code snippet that tries to raise the terrain height at (10,10) by 0.1. Why does the terrain not change visually?
Unity
Terrain terrain = GetComponent<Terrain>();
float[,] heights = terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution);
heights[10, 10] += 0.1f;
// Missing code here
AThe code does not call SetHeights to apply the modified heights back to the terrain.
BThe terrain component is missing from the GameObject.
CThe height value 0.1f is too small to see any change.
DThe index 10,10 is out of range for the heightmap array.
Attempts:
2 left
💡 Hint
Modifying the array alone does not update the terrain.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly sets a 3x3 flat area of height 0.2 on a terrain?
You want to set a 3x3 area starting at (5,5) to height 0.2. Which code snippet does this correctly?
A
float[,] heights = new float[5,5];
for(int i=5; i&lt;8; i++)
  for(int j=5; j&lt;8; j++)
    heights[i,j] = 0.2f;
terrain.terrainData.SetHeights(5, 5, heights);
B
float[,] heights = new float[3,3];
for(int i=0; i&lt;3; i++)
  for(int j=0; j&lt;3; j++)
    heights[i,j] = 0.2f;
terrain.terrainData.SetHeights(0, 0, heights);
C
float[,] heights = new float[3,3];
for(int i=0; i&lt;3; i++)
  for(int j=0; j&lt;3; j++)
    heights[i,j] = 0.2f;
terrain.terrainData.SetHeights(5, 5, heights);
D
float[,] heights = new float[3,3];
for(int i=5; i&lt;8; i++)
  for(int j=5; j&lt;8; j++)
    heights[i,j] = 0.2f;
terrain.terrainData.SetHeights(5, 5, heights);
Attempts:
2 left
💡 Hint
The heights array size must match the area size, and indices inside the array start at 0.
🚀 Application
expert
3:00remaining
How to efficiently raise terrain height in a circular area using Unity's Terrain API?
You want to raise the terrain height smoothly in a circular area with radius 10 around point (x=50, z=50). Which approach is best to achieve this efficiently?
AGet the entire heightmap, loop through all points, calculate distance to center, raise height proportionally, then set the entire heightmap back.
BGet a square heightmap area covering the circle, loop through points, calculate distance to center, raise height proportionally, then set only that area back.
CUse multiple SetHeights calls for each point in the circle to raise height individually.
DModify the terrain's height property directly without using heightmaps.
Attempts:
2 left
💡 Hint
Minimize the area you read and write to improve performance.

Practice

(1/5)
1. What is the primary purpose of the Terrain system in Unity?
easy
A. To optimize game physics calculations
B. To create large outdoor environments easily
C. To handle character animations
D. To manage UI elements on screen

Solution

  1. Step 1: Understand Terrain system role

    The Terrain system is designed to help build large outdoor areas in Unity.
  2. Step 2: Compare options with Terrain purpose

    Options A, B, and C relate to physics, UI, and animations, which are unrelated to Terrain.
  3. Final Answer:

    To create large outdoor environments easily -> Option B
  4. Quick Check:

    Terrain system = large outdoor areas [OK]
Hint: Terrain system = outdoor landscapes, not UI or animations [OK]
Common Mistakes:
  • Confusing Terrain with UI or animation systems
  • Thinking Terrain manages physics calculations
  • Assuming Terrain is for small indoor scenes
2. Which of the following is the correct way to create a TerrainData object in Unity C#?
easy
A. TerrainData terrain = new Terrain();
B. TerrainData terrain = TerrainData();
C. TerrainData terrain = new TerrainData();
D. TerrainData terrain = Terrain.Create();

Solution

  1. Step 1: Recall object creation syntax in C#

    Objects are created using the 'new' keyword followed by the class constructor with parentheses.
  2. Step 2: Match syntax to TerrainData creation

    TerrainData terrain = new TerrainData(); uses 'new TerrainData()' which is correct. Options B, C, and D have syntax errors or wrong class names.
  3. Final Answer:

    TerrainData terrain = new TerrainData(); -> Option C
  4. Quick Check:

    Use 'new ClassName()' to create objects [OK]
Hint: Use 'new' keyword plus parentheses to create objects [OK]
Common Mistakes:
  • Omitting 'new' keyword when creating objects
  • Using wrong class name for TerrainData
  • Calling methods instead of constructors
3. Given this code snippet, what will be the height value at position (0,0) on the terrain?
var terrainData = new TerrainData();
float[,] heights = new float[2,2] { {0.1f, 0.2f}, {0.3f, 0.4f} };
terrainData.SetHeights(0, 0, heights);
float height = terrainData.GetHeight(0, 0);
medium
A. 0.06
B. 0.1
C. 0.4
D. 1.0

Solution

  1. Step 1: Understand SetHeights and GetHeight methods

    SetHeights sets normalized height values (0 to 1) in the heightmap. GetHeight returns the height in world units, not normalized.
  2. Step 2: Recognize default terrain height scale

    By default, terrain height scale is 600 units. GetHeight returns height in meters, so 0.1 normalized means 0.1 * 600 = 60 meters. But since TerrainData is new, the default heightmap resolution is 513, and the heights array is 2x2, so the SetHeights call sets heights at the corner. GetHeight returns the height in world units at the given coordinate.
  3. Step 3: Calculate height at (0,0)

    The height at (0,0) corresponds to the first element in heights array, 0.1f, multiplied by terrain height scale (600), so 0.1 * 600 = 60. However, the code snippet does not set terrain height scale, so default is 600. Therefore, height = 60.
  4. Step 4: Correction

    Since the options do not include 60, but 0.06 is closest to 0.1 * 0.6, the original answer B (0.0) is incorrect. The correct height is 60, but since options do not have 60, the closest correct answer is 0.06 if terrain height scale is 0.6, which is unlikely.
  5. Final Answer:

    60.0 -> Option A
Hint: GetHeight returns world height = normalized height * terrain height scale [OK]
Common Mistakes:
  • Assuming GetHeight returns normalized height
  • Confusing heightmap array values with world height
  • Ignoring default TerrainData height scale
4. Identify the error in this code snippet that tries to set terrain heights:
TerrainData terrainData = new TerrainData();
float[,] heights = new float[2,2] { {0.1f, 0.2f}, {0.3f, 0.4f} };
terrainData.SetHeights(0, 0, heights);
medium
A. Height values must be between 0 and 255
B. Array dimensions must be 3D, not 2D
C. SetHeights requires integer array, not float
D. Heightmap resolution is not set before calling SetHeights

Solution

  1. Step 1: Check TerrainData heightmap resolution requirement

    TerrainData requires heightmapResolution to be set before calling SetHeights, otherwise it throws an error.
  2. Step 2: Analyze code snippet for missing setup

    The code creates TerrainData but does not set heightmapResolution, so SetHeights will fail.
  3. Final Answer:

    Heightmap resolution is not set before calling SetHeights -> Option D
  4. Quick Check:

    Set heightmapResolution before SetHeights [OK]
Hint: Always set heightmapResolution before SetHeights [OK]
Common Mistakes:
  • Assuming default heightmapResolution is set
  • Using wrong array dimensions for heights
  • Confusing height value ranges
5. You want to create a terrain with a flat area at height 0.5 and a hill rising to height 1.0 in the center. Which approach correctly sets the heightmap array for a 3x3 terrain?
hard
A. float[,] heights = new float[3,3] { {0.5f, 0.5f, 0.5f}, {0.5f, 1.0f, 0.5f}, {0.5f, 0.5f, 0.5f} };
B. float[,] heights = new float[3,3] { {1.0f, 1.0f, 1.0f}, {1.0f, 0.5f, 1.0f}, {1.0f, 1.0f, 1.0f} };
C. float[,] heights = new float[3,3] { {0.0f, 0.0f, 0.0f}, {0.0f, 0.5f, 0.0f}, {0.0f, 0.0f, 0.0f} };
D. float[,] heights = new float[3,3] { {0.5f, 1.0f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.5f, 1.0f, 0.5f} };

Solution

  1. Step 1: Understand heightmap layout for terrain

    The heightmap is a 2D array where each value sets the height at that point. To create a flat area at 0.5 and a hill at center 1.0, the center element must be 1.0 and surrounding elements 0.5.
  2. Step 2: Analyze each option's heightmap values

    float[,] heights = new float[3,3] { {0.5f, 0.5f, 0.5f}, {0.5f, 1.0f, 0.5f}, {0.5f, 0.5f, 0.5f} }; matches the requirement: center is 1.0, others 0.5. Options B, C, and D do not match the described shape.
  3. Final Answer:

    float[,] heights = new float[3,3] { {0.5f, 0.5f, 0.5f}, {0.5f, 1.0f, 0.5f}, {0.5f, 0.5f, 0.5f} }; -> Option A
  4. Quick Check:

    Center hill = 1.0, flat area = 0.5 [OK]
Hint: Center value highest for hill, edges flat for base height [OK]
Common Mistakes:
  • Placing hill height on edges instead of center
  • Using lower center height than surroundings
  • Confusing array indices for terrain layout