0
0
Unityframework~30 mins

Terrain system basics in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a 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.