I have a problem I need help with, I am trying to instantiate multiple terrain objects through script. Then I am trying to create a new terrain asset through another script which will control the random terrain’s properties, however, all of the terrains are sharing the same terrain properties even though they must technically have different terrain properties. How do I create terrain data assets dynamically? Pls help with this, thank you.
public int width = 64;
public int length = 64;
public float heightScale = 20f;
public float maxHeight = 10f;
private Terrain terrain;
private void Start()
{
GenerateRandomTerrain();
}
private void GenerateRandomTerrain()
{
TerrainData terrainData = new TerrainData();
terrainData.heightmapResolution = width + 1;
int heightmapWidth = terrainData.heightmapResolution;
int heightmapHeight = terrainData.heightmapResolution;
float[,] heights = new float[heightmapWidth, heightmapHeight];
for (int y = 0; y < heightmapHeight; y++)
{
for (int x = 0; x < heightmapWidth; x++)
{
float height = Mathf.PerlinNoise(x / (float)width * heightScale, y / (float)length * heightScale) * maxHeight;
heights[x, y] = height / maxHeight;
}
}
terrainData.SetHeights(0, 0, heights);
Vector3 terrainSize = terrainData.size;
terrainSize.x = width;
terrainSize.z = length;
terrainData.size = terrainSize;
terrain.terrainData = terrainData;
}
this is the terrain code.