- Home /
Render area not working correctly
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.
After 2 straight hours of bug fixing, I must go to bed. Goodnight, Ill answer any comments in the morning.
Just to be clear: your terrain is in the xy plane, not the xz plane?
Hi, when I have this kind of problems, I use a lot of Debug.DrawLine to figure out visualy what I'm doing wrong.
Answer by zach-r-d · Jul 15, 2015 at 07:53 PM
I'm fairly certain the problem can be fixed by doing these two things:
Replace the definitions of the variables in UpdateRenderArea to the following:
float playerX = player.transform.position.x/chunkSize + .5f; // +.5f calculates from center of chunk rather than corner float playerY = player.transform.position.y/chunkSize + .5f; int playerXClamp = Mathf.Clamp (Mathf.FloorToInt(playerX - renderDistance), 0, chunkCountX - 1); int playerYClamp = Mathf.Clamp (Mathf.FloorToInt(playerY - renderDistance), 0, chunkCountY - 1); int playerXClamp2 = Mathf.Clamp (Mathf.FloorToInt(playerX + renderDistance), 0, chunkCountX - 1); int playerYClamp2 = Mathf.Clamp (Mathf.FloorToInt(playerY + renderDistance), 0, chunkCountY - 1);
Use <= instead of < for the condition of each of the for loops.