Hello!
Ive been working on a feature for my game where a square shaped area around the player that contains chunks would be rendered, but outside of that, chunks will not be rendered. This is for lag reduction purposes and will make the game smoother looking.
Code:
void LateUpdate () {
UpdateRenderArea ();
}
int RoundNumberToNearest (float n, int r) {
float divided = n / r;
int rounded = Mathf.RoundToInt (divided);
int multiplied = rounded * r;
return multiplied;
}
void UpdateRenderArea () {
int playerX = RoundNumberToNearest (player.transform.position.x, chunkSize);
int playerY = RoundNumberToNearest (player.transform.position.y, chunkSize);
int playerXClamp = Mathf.Clamp (playerX / chunkSize - renderDistance, 0, chunkCountX - 1);
int playerYClamp = Mathf.Clamp (playerY / chunkSize - renderDistance, 0, chunkCountY - 1);
int playerXClamp2 = Mathf.Clamp (playerX / chunkSize + renderDistance, 0, chunkCountX - 1);
int playerYClamp2 = Mathf.Clamp (playerY / chunkSize + renderDistance, 0, chunkCountY - 1);
Debug.Log (playerXClamp + ", " + playerXClamp2 + ", " + playerYClamp + ", " + playerYClamp2);
bool [,] renderArea = new bool [chunkCountX, chunkCountY];
for (int x = playerXClamp; x < playerXClamp2 + 1; x ++)
{
for (int y = playerYClamp; y < playerYClamp2 + 1; y ++)
{
renderArea [x, y] = true;
}
}
for (int x = 0; x < chunkCountX; x ++)
{
for (int y = 0; y < chunkCountY; y ++)
{
chunks [x, y].SetActive (renderArea [x, y]);
}
}
}
The rest has been cut out, basically, though, chunks are put through a parenting system when blocks are made. all chunks are made, and then deactivated before one frame. the bottom left chunk, for example, would be in the array as “chunks [0, 0]” and it’s name is “chunk 1-1”.
However, when setting the render distance lower, it seems to favor the left side of the map more? it overall is just not working too.