/** * Updates the boids according to certain rules. * */ void Update() { // Boid handle Boid b; float angle; // Behaviour update loop for (int i = 0; i < numActiveBoids; i++) { b = boids[i]; // Update position from game object location b._pos = new Vector2(boidGameObject[i].transform.position.x, boidGameObject[i].transform.position.y); // Store velocities generated from different rules velocitiesFromRules[(int)BehaviouralRules.seekGoal] = adherences[(int)BehaviouralRules.seekGoal] == 0 || goalObj == null ? Vector2.zero : Behaviours.Rule_SeekGoal(b, goalObj.transform.position); velocitiesFromRules[(int)BehaviouralRules.avoidObstacle] = adherences[(int)BehaviouralRules.avoidObstacle] == 0 || obstacleObj == null ? Vector2.zero : Behaviours.Rule_AvoidObstacle(b, obstacleObj.transform.position); velocitiesFromRules[(int)BehaviouralRules.steerToMean] = adherences[(int)BehaviouralRules.steerToMean] == 0 ? Vector2.zero : Behaviours.Rule_SteerToMean(b, boids, numActiveBoids); velocitiesFromRules[(int)BehaviouralRules.avoidCrowding] = adherences[(int)BehaviouralRules.avoidCrowding] == 0 ? Vector2.zero : Behaviours.Rule_AvoidCrowding(b, boids, numActiveBoids); for (int j = 0; j < (int)BehaviouralRules.SIZE; j++) { b._velocity += (velocitiesFromRules[j] * adherences[j]) * Time.deltaTime * 100; } LimitVelocity(ref b._velocity); boidGameObject[i].gameObject.transform.Translate(b._velocity); // Keeps boids within the screen StayWithinLimits(ref b); // Get angle from velocity angle = Mathf.Atan2(b._velocity.y, b._velocity.x) * Mathf.Rad2Deg - 90; Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward); // Rotate sprite according to velocity, with smoothing float smoothing = .2f; // 1 - No smoothing, 0 - Infinite b._sprite.transform.rotation = Quaternion.Slerp(b._sprite.transform.rotation, rotation, smoothing); } }