private void Start() { Bounds bounds = CameraUtility.GetCameraBounds(Camera.main); float min_separation = BoidPrefab.GetComponent <Agent>().separationRadius * 2; int max_attempts = 40; int boids_spawned = 0; List <Vector2> placedSoFar = new List <Vector2>(); for (int i = 0; i < NumBoids; i++) { int current_attempts = 0; bool foundSpot = false; float initial_x = 0; float initial_y = 0; while (!foundSpot && current_attempts < max_attempts) { initial_x = Random.Range(bounds.min.x, bounds.max.x); initial_y = Random.Range(bounds.min.y, bounds.max.y); Vector2 current = new Vector2(initial_x, initial_y); foundSpot = true; foreach (Vector2 toCheck in placedSoFar) { if (Vector2.Distance(toCheck, current) < min_separation) { foundSpot = false; break; } } current_attempts++; } if (foundSpot) { Quaternion intial_rotation = Quaternion.Euler(0, 0, Random.Range(0.0f, 360.0f)); GameObject go = Instantiate(BoidPrefab, new Vector3(initial_x, initial_y, 0), intial_rotation); placedSoFar.Add(new Vector2(initial_x, initial_y)); boids_spawned++; } } Debug.Log("Boids spawned " + boids_spawned); }
private bool OOBY() { Bounds bounds = CameraUtility.GetCameraBounds(Camera.main); return(transform.position.y < bounds.min.y - offScreenPadding || transform.position.y > bounds.max.y + offScreenPadding); }