// Checks if there are any nearby entityClusters, which the GameObject can be pooled with public static bool ReconsiderEntityCluster(Transform t) { if (t == null) { return(false); } // Remove from old cluster EntityCluster oldCluster = t.GetComponent <Entity>().cluster; if (oldCluster != null) { if (oldCluster.Count == 1) { entityClusters.Remove(oldCluster); } oldCluster.RemoveEntity(t); } // Get all nearby entityClusters var nearClusters = new System.Collections.Generic.List <EntityCluster>(); float minimumDistance = t.GetComponent <Entity>().obstacleSize + minPassDist; for (var i = 0; i < entityClusters.Count; i++) { // potential loop EntityCluster cluster = entityClusters[i]; if (cluster == null) { entityClusters.Remove(cluster); i--; continue; } // Test if near enough if (cluster.GetClosestPassingDistance(t.transform.position) < minimumDistance) { nearClusters.Add(cluster); // Add cluster connections foreach (Transform companion in cluster) { float distance = (companion.position - t.position).magnitude - companion.GetComponent <Entity>().obstacleSize; if (distance < minimumDistance) { if (t.GetComponent <Moving>() != null && !t.GetComponent <Moving>().clusteredEntities.Contains(companion)) { t.GetComponent <Moving>().clusteredEntities.Add(companion); } if (companion.GetComponent <Moving>() != null && !companion.GetComponent <Moving>().clusteredEntities.Contains(t)) { companion.GetComponent <Moving>().clusteredEntities.Add(t); } } } } } if (nearClusters.Count == 0) { // Create new cluster System.Collections.Generic.List <Transform> clusterList = new System.Collections.Generic.List <Transform> { t }; EntityCluster cluster = new EntityCluster(clusterList); entityClusters.Add(cluster); return(true); } else if (nearClusters.Count == 1) { // Add to cluster nearClusters[0].Add(t); if (oldCluster != null) { return(oldCluster == nearClusters[0]); } else { return(true); } } else { // Merge existing entityClusters and add object MergeEntityClusters(nearClusters); nearClusters[0].Add(t); return(true); } }