Создание рандомной генерации terrain unity
Я написал скрипт и при запуске часть terrain не заполнена вот скрипт
{
private readonly int width = 200;
private readonly int height = 200;
public static int CoreHeight = 100;
private readonly float scale = 4f;
private float offsetX = 100f;
private float offsetY = 100f;
private void Start()
{
CoreHeight = height;
offsetY = Random.Range(50, offsetY);
offsetX = Random.Range(50, offsetX);
Terrain terrain = GetComponent<Terrain>();
terrain.terrainData = GenerateTerrain(terrain.terrainData);
}
private TerrainData GenerateTerrain(TerrainData terrainData)
{
terrainData.heightmapResolution = width;
terrainData.size = new Vector3(width, scale, height);
float[,] heights = GenerateHeights();
terrainData.SetHeights(0, 0, heights);
return terrainData;
}
private float[,] GenerateHeights()
{
float[,] heights = new float[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
heights[x, y] = CalculateHeight(x, y);
}
}
return heights;
}
private float CalculateHeight(int x, int y)
{
offsetX = Random.Range(10, offsetY);
offsetY = Random.Range(10, offsetX);
float xCoord = (float)x / width * 4 + offsetX;
float yCoord = (float)y / height * 4 + offsetY;
return Mathf.PerlinNoise(xCoord, yCoord);
}
}