// Loop through all chunks in the scene and check their distance, // if not in radius, destroy them public void RemoveChunks(Vector3Int chunkPosition, int radiusInChunks) { foreach (KeyValuePair <Vector3Int, Chunk> pair in chunks) { Vector3 chunkPos = pair.Value.chunk.transform.position; Vector2 heading; heading.x = chunkPosition.x + _chunkSizeHalf - chunkPos.x; heading.y = chunkPosition.z + _chunkSizeHalf - chunkPos.z; float distanceSquared = heading.x * heading.x + heading.y * heading.y; float distance = Mathf.Sqrt(distanceSquared); if (distance > radiusInChunks + 2) { if (!pair.Value.IsEmpty()) { _accelerationStructure.RemoveInstance(pair.Value.chunk.GetComponent <Renderer>()); } pair.Value.DisposeBlocks(); Destroy(pair.Value.chunk); _toRemove.Add(pair.Key); } } foreach (Vector3Int key in _toRemove) { chunks.Remove(key); } _toRemove.Clear(); }