I have looked through my code multiple time and can not figure why the terrain objects do not line up with each other. I have a terrain object with the “SingleChunkGen” script on it, which has a empty object as its parent, with the “ChunkInfo” on it. I then have a separate empty object with the “TerrainGen” script attached, and a prefab of the first empty object set as the “ChunkPrefab” GameObject in the inspector. Also if anyone knows how to fix the cliffs at the edge of each terrain that would be nice. I have also attached an image of the heirarchy, and inspector of each object.
TerrainGen script:
using UnityEngine;
public class TerrainGen : MonoBehaviour
{
public static Vector2[] chunkXYorder = new Vector2[1];
public int maxItterations = 20;
public int chunkSize = 512;
public GameObject chunkPrefab;
void Start()
{
for (int x = 0; x <= 5; x++)
{
for (int y = 0; y <= 5; y++)
{
loadChunk(x, y);
}
}
}
void loadChunk(int x, int y)
{
if (checkChunkExistance(x, y))
{
Debug.Log("Chunk at " + x + " " + y + " exitsts, loading");
//load from saved state - not implemented yet
}
else
{
Debug.Log("Chunk at " + x + " " + y + " does not exitst, genrating new");
generateChunk(x, y);
}
}
void unloadChunk(int x, int y)
{
//save chunk - not implemented yet
}
void generateChunk(int x, int y)
{
GameObject currentChunk = Instantiate(chunkPrefab, new Vector3(x * chunkSize, 0, y * chunkSize), Quaternion.identity);
currentChunk.GetComponent<ChunkData>().chunkX = x;
currentChunk.GetComponent<ChunkData>().chunkY = y;
currentChunk.transform.GetChild(0).gameObject.GetComponent<SingleChunkGen>().GenerateTerrain(x, y, chunkSize);
}
bool checkChunkExistance(int x, int y)
{
bool found = false;
int itteration = 0;
int currentSearchPoint = chunkXYorder.Length;
int xCorrect = 0;
while (!found)
{
if (currentSearchPoint > chunkXYorder.Length - 2 || currentSearchPoint < 0 || itteration > maxItterations)
{
return false;
}
itteration++;
if (chunkXYorder[currentSearchPoint].x == x)
{
xCorrect = currentSearchPoint;
found = true;
break;
}
else if (chunkXYorder[currentSearchPoint].x > x)
{
currentSearchPoint -= chunkXYorder.Length / itteration;
continue;
}
else if (chunkXYorder[currentSearchPoint].x < x)
{
currentSearchPoint += chunkXYorder.Length / itteration;
continue;
}
}
itteration = 0;
while (chunkXYorder[currentSearchPoint].x == x)
{
currentSearchPoint++;
if (currentSearchPoint > chunkXYorder.Length - 2 || itteration > maxItterations)
{
break;
}
if (chunkXYorder[currentSearchPoint].y == y)
{
return true;
}
}
currentSearchPoint = xCorrect;
while (chunkXYorder[currentSearchPoint].x == x)
{
currentSearchPoint--;
if (currentSearchPoint < 0 || itteration > maxItterations)
{
return false;
}
if (chunkXYorder[currentSearchPoint].y == y)
{
return true;
}
}
return false;
}
}
SingleChunkGen Script:
using UnityEngine;
public class SingleChunkGen : MonoBehaviour
{
public int height = 20;
public float scale = 20;
public void GenerateTerrain(int chunkX, int chunkY, int chunkSize)
{
Terrain terrain = GetComponent<Terrain>();
TerrainData terrainData = new TerrainData();
terrainData.heightmapResolution = chunkSize;
terrainData.size = new Vector3(chunkSize, height, chunkSize);
terrainData.SetHeights(0, 0, GenerateHeights(chunkX, chunkY, chunkSize));
terrain.terrainData = terrainData;
GetComponent<TerrainCollider>().terrainData = terrainData;
}
float[,] GenerateHeights(int chunkX, int chunkY, int chunkSize) {
float[,] heights = new float[chunkSize, chunkSize];
for (int i = 0; i < chunkSize; i++)
{
for (int e = 0; e < chunkSize; e++)
{
heights[i, e] = CalculateHeight(i + chunkSize * chunkX, e + chunkSize * chunkY, chunkSize);
}
}
return heights;
}
float CalculateHeight(int x, int y, int chunkSize)
{
float xCord = (float)x / chunkSize * scale;
float yCord = (float)y / chunkSize * scale;
return Mathf.PerlinNoise(xCord, yCord);
}
}
ChunkData Script:
using UnityEngine;
public class ChunkData : MonoBehaviour
{
public int chunkX;
public int chunkY;
}
6710020–771166–ChunkData.cs (190 Bytes)
6710020–771169–SingleChunkGen.cs (1.27 KB)
6710020–771172–TerrainGen.cs (3.11 KB)