- Home /
This question was
closed Sep 13, 2020 at 04:45 AM by
Rhyusaky for the following reason:
The question is answered, right answer was accepted
Question by
Rhyusaky · Sep 10, 2020 at 02:26 AM ·
programmingtilemapprocedural generationchunksstrange
Big problem on chunk generation (Unity Tilemaps)
Why does this chunk generator, although it works virtually (in the sense that it generates chunks correctly, as seen in the gizmo img#0/img#1 visualization), generate these strange patterns in the tilemap?
img#0
img#1
The code for chunk generation here:
Dictionary<Vector2Int, Chunk> chunks = new Dictionary<Vector2Int, Chunk>(32);
public IEnumerator UpdateMap()
{
while(true)
{
var cellSize = grid.cellSize;
var ppos = playerObj.transform.position;
ppos.x = ppos.x / (chunkSize.x * cellSize.x);
ppos.y = ppos.y / (chunkSize.y * cellSize.y);
var player = new Vector2Int(Mathf.RoundToInt(ppos.x), Mathf.RoundToInt(ppos.y));
Debug.Log(player);
string chunkReg = "*CHUNKS*";
for (int x = player.x - chunkArea.x; x <= player.x + chunkArea.x; x++)
for (int y = player.y - chunkArea.y; y <= player.y + chunkArea.y; y++)
{
var pos = new Vector2Int(x, y);
if (!mapBounds.Contains(pos) || chunks.ContainsKey(pos))
continue;
chunkReg += $"\n {pos}";
//var chunk = new Chunk(pos.x * chunkSize.x, pos.y * chunkSize.y, chunkSize.x, chunkSize.y);
var chunk = new Chunk(pos.x, pos.y, 1, 1);
chunk.Generate();
chunk.Draw();
chunks[pos] = chunk;
qtree.Insert(new Point(pos, chunk));
yield return null;
}
chunkReg += "\n END";
Debug.Log(chunkReg);
yield return null;
}
}
The code for the current chunk filling in tilemap:
public void SetBlock(RectInt bounds, TileBase tile)
{
int xPos = bounds.x;
int yPos = bounds.y;
int w = xPos + bounds.width;
int h = yPos + bounds.height;
for (int x = xPos; x < w; x++)
for (int y = xPos; y < h; y++)
{
map.SetTile(new Vector3Int(x, y, 0), tile);
}
}
public void DrawChunk(Vector2Int position, bool draw)
{
var t = draw ? tile : null;
//SetBlock(new RectInt(position.x * chunkSize.x, position.y * chunkSize.y, chunkSize.x, chunkSize.y), t);
SetBlock(new RectInt(position.x * chunkSize.x, position.y * chunkSize.y, chunkSize.x, chunkSize.y), t);
}
This problem doesn't seem to make any sense to me, do you understand? So ... if anyone knows any way to help me with this, I will be very grateful
Comment
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help fixing procedural generation bug 1 Answer
How to assign individual data to a tiles? (Unity3D Tilemap) 0 Answers
Smooth chunk saving and loading 0 Answers