Hey,
I’m creating an Terrain based on Size using a HeightMap.
What is working in my Script?
- Terraininstancing and positioning from Prefab
- HeightMapGeneration (1 HeightMap for everything)
- Getting Terrain at Position
- Adding HeightMap to Terraintile
What is my problem?
Every Single Terrain uses the same Part of the HeightMap.
What does my code looks like?
- HeightMapGeneration
public float[,] HeightMapGeneration(int MapWidth, int MapHeight, int MapSeed, float Freq, float elev, Vector3 Offset)
{
float[,] HeightMap = new float[MapWidth, MapHeight];
for (int x = 0; x < MapWidth; x++)
{
for (int z = 0; z < MapHeight; z++)
{
float nx = ((x + Offset.x) / Freq) + MapSeed; //x / HeightMapWidth - 0.5f;
float nz = ((z + Offset.z) / Freq) + MapSeed; //y / HeightMapHeight - 0.5f;
// Create different noise levels
float e0 = Mathf.PerlinNoise(nx , nz);
float e1 = .5f * Mathf.PerlinNoise(nx * 2, nz * 2) * e0;
float e2 = .25f * Mathf.PerlinNoise(nx * 4, nz * 4) * (e1 + e0);
// Combine Noise levels
float e = (e0 + e1 + e2);
// Make flat areas
HeightMap[x, z] = Mathf.Pow(e, elev);
}
}
return HeightMap;
}
- HeightMap
// Create HeightMap for every single Terrain
// Cycle trough every Terrain
for (int Width = 0; Width < Terraincount_Width; Width++)
{
for (int Height = 0; Height < Terraincount_Height; Height++)
{
int TerrainOffset_X = Width * SingleTerrainSize + 500;
int TerrainOffset_Z = Height * SingleTerrainSize + 500;
// Get Terrain under Offset
Terrain ActiveTerrain = GetTerrain(new Vector3(TerrainOffset_X, 0, TerrainOffset_Z), Terrainarray);
// Moove heightmap to local TerrainMap
float[,] TerrainMap = new float[SingleTerrainHMSize, SingleTerrainHMSize]; // local HeightMap
int HeightMapFirstX = Width * SingleTerrainHMSize; // First Position of the local HeightMap in the Global one
int HeightMapFirstZ = Height * SingleTerrainHMSize;
for (int TerrainMapX = 0; TerrainMapX < SingleTerrainHMSize; TerrainMapX++)
{
int HeightMapPosition_X = HeightMapFirstX + TerrainMapX;
for (int TerrainMapZ = 0; TerrainMapZ < SingleTerrainHMSize; TerrainMapZ++)
{
int HeightMapPosition_Z = HeightMapFirstZ + TerrainMapZ;
// Moove 0f-1f Value to Heightvalue from MinHeight to Maxheight and then divide by MaxTerrainHeight
TerrainMap[TerrainMapX, TerrainMapZ] = Mathf.Lerp(MinHeight, MaxHeight, HeightMapData[HeightMapPosition_X, HeightMapPosition_Z]) / TerrainMaxHeight;
}
}
// Change Height
ActiveTerrain.terrainData.SetHeights(0, 0, TerrainMap);
}
}
In this Code:
Create HeightMap works, I recently logged all of it in the Console and it cycled trough every single Pixel
Create Terrain Works, It Creates Instances of each Terrain
All Terrains to Array also works
// Create HeightMap for every single Terrain does not work…
It Adds the same Heightdata to each Terrainmap (local HeightMap)
This is the result of an 2 by 2 Terrainfield
Either I’m to stupid to find the error or I’m blind ![]()
I tried to fix this for the past 2 days, so slowely but surely I’m thinking of the first.


