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
Terrain system basics
📖 Scenario: You are creating a simple terrain system in Unity for a game. The terrain will have different height values stored in a 2D array. You will write code to set up the terrain heights, define a height threshold, find which points are above this threshold, and finally print those points.
🎯 Goal: Build a Unity C# script that initializes a 2D array representing terrain heights, sets a height threshold, finds all points above this threshold using a loop, and prints their coordinates and heights.
📋 What You'll Learn
Create a 2D float array called terrainHeights with exact values
Create a float variable called heightThreshold with a specific value
Use a for loop with variables row and col to iterate over terrainHeights
Inside the loop, check if the height is above heightThreshold
Store points above threshold in a list of tuples called highPoints
Print each point's coordinates and height using Debug.Log
💡 Why This Matters
🌍 Real World
Terrain height data is used in games and simulations to create realistic landscapes and control gameplay elements like movement and visibility.
💼 Career
Understanding how to work with 2D arrays, loops, and data filtering is essential for game developers and simulation programmers working with terrain and environment systems.
Progress0 / 4 steps
1
Create the terrain height data
Create a 2D float array called terrainHeights with these exact values: {{0.1f, 0.3f, 0.5f}, {0.4f, 0.6f, 0.2f}, {0.7f, 0.8f, 0.3f}}
Unity
Hint
Use float[,] to declare a 2D array and initialize it with the exact values inside curly braces.
2
Set the height threshold
Create a float variable called heightThreshold and set it to 0.5f inside the Start method, below the terrainHeights array.
Unity
Hint
Declare a float variable named heightThreshold and assign it the value 0.5f.
3
Find points above the height threshold
Create a List<(int, int, float)> called highPoints to store points above the threshold. Use nested for loops with variables row and col to iterate over terrainHeights. Inside the loops, check if the height at terrainHeights[row, col] is greater than heightThreshold. If yes, add a tuple (row, col, terrainHeights[row, col]) to highPoints.
Unity
Hint
Use GetLength(0) for rows and GetLength(1) for columns. Add tuples with row, col, and height to highPoints.
4
Print the points above the threshold
Use a foreach loop with variable point to iterate over highPoints. Inside the loop, use Debug.Log to print the message: $"Point at row {point.Item1}, col {point.Item2} has height {point.Item3}".
Unity
Hint
Use a foreach loop to go through highPoints. Use Debug.Log with an interpolated string to print each point's row, column, and height.
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
Step 1: Understand Terrain system role
The Terrain system is designed to help build large outdoor areas in Unity.
Step 2: Compare options with Terrain purpose
Options A, B, and C relate to physics, UI, and animations, which are unrelated to Terrain.
Final Answer:
To create large outdoor environments easily -> Option B
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
Step 1: Recall object creation syntax in C#
Objects are created using the 'new' keyword followed by the class constructor with parentheses.
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.
Final Answer:
TerrainData terrain = new TerrainData(); -> Option C
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
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.
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.
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.
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.
TerrainData requires heightmapResolution to be set before calling SetHeights, otherwise it throws an error.
Step 2: Analyze code snippet for missing setup
The code creates TerrainData but does not set heightmapResolution, so SetHeights will fail.
Final Answer:
Heightmap resolution is not set before calling SetHeights -> Option D
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
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.
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.
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
Quick Check:
Center hill = 1.0, flat area = 0.5 [OK]
Hint: Center value highest for hill, edges flat for base height [OK]