private void Update() { if (isInitialized && MustUpdate()) { // Round agent pos to valid chunk pos. Vector3Int roundedPos = Vector3Int.Scale(Vector3Int.RoundToInt( Vector3.Scale(agent.localPosition, invChunkSize)), chunkSize); HashSet <Vector3Int> tmp = new HashSet <Vector3Int>(coords); coords.Clear(); foreach (Bounds b in bounds.GetBounds(roundedPos)) { Vector3Int v = Vector3Int.RoundToInt(b.center); if (!useCamFrustum || GeometryUtility.TestPlanesAABB(planes, b)) { coords.Add(v); if (!tmp.Contains(v)) { int priority = -(v - roundedPos).sqrMagnitude; chunks.Add(chunkPool.Activate(v, transform, priority)); allChunksActive = false; } } } tmp.ExceptWith(coords); foreach (Vector3Int v in tmp) { chunks.Remove(chunkPool.Deactivate(v, transform)); } } }