A little background:
I am making a game similar to Minecraft with a three-dimensional array of cubes. This array is a 50 x 50 x 10 and whenever I turn Camera Clip Plane to only show cubes close to the player, the game tends to lag a lot. I have made one solution, but every 10 seconds, a very large lag occurs, but the game otherwise runs smooth. This was my fix:
IEnumerator setVisibleRange(List<List<List<GameObject>>> blockList) {
canScanRange = false;
for(int r = 0; r < blockList.Count; r++) {
for(int c = 0; c < blockList[r].Count; c++) {
for(int d = 0; d < blockList[r]
.Count; d++) {
if(blockList[r][c][d] is GameObject) {
float dist = Vector3.Distance(blockList[r][c][d].transform.position, GameObject.Find("Player").transform.position);
if(dist > 50) {
blockList[r][c][d].SetActive(false);
} else {
blockList[r][c][d].SetActive(true);
}
}
}
}
}
yield return new WaitForSeconds(10);
canScanRange = true;
}
My 'fix' has to run though EVERY element in the array which makes the lag happen.
Is there a way for this lag to go away?