/// <summary> /// Author: Ziqi Li /// Function to get a random target for a flock of birds /// </summary> /// <param name="list"></param> /// <returns></returns> Vector3 GetRandTarget(FlocksList list) { Vector3 randTarget; Vector3 avgPosition = GetFlockAvgPosition(list); Vector3 avgVelocity = GetFlockAvgVelocity(list).normalized; avgVelocity.y *= MotionMultiplier_Y; // decrease the impact of vertical movement // find a random target in the moving direction of this flock Vector3 centerPos = FlyingAreaCenter.transform.position; // ignore z-axis Vector3 avgPos_xy = new Vector3(avgPosition.x, avgPosition.y, 0); centerPos.z = 0; // if this flock is out of flying range, set the center of flying area as target if (Vector3.Distance(avgPos_xy, centerPos) > MaxRangeFromCenter) { Vector2 rand = Random.insideUnitCircle * RandTargetRadius; randTarget = new Vector3(centerPos.x + rand.x, centerPos.y + rand.y, avgPosition.z); } else // randomly select a forward point { Vector3 randCircleCenter = avgPosition + avgVelocity * RandTargetMagnitude; Vector2 rand = Random.insideUnitCircle * RandTargetRadius; randTarget = new Vector3(randCircleCenter.x + rand.x, randCircleCenter.y + rand.y, avgPosition.z); } return(randTarget); }
/// <summary> /// Author: Ziqi Li /// Function to get the average velocity of a flock of units /// </summary> /// <param name="list"></param> Vector3 GetFlockAvgVelocity(FlocksList list) { Vector3 avgVelocity = Vector3.zero; foreach (GameObject unit in list.Birds) { avgVelocity += unit.GetComponent <Rigidbody>().velocity; } return(avgVelocity / list.Birds.Count); }
/// <summary> /// Author: Ziqi Li /// Function to get the geometric center of a flock of units /// </summary> /// <param name="list"></param> Vector3 GetFlockAvgPosition(FlocksList list) { Vector3 avgPosition = Vector3.zero; foreach (GameObject unit in list.Birds) { avgPosition += unit.transform.position; } return(avgPosition / list.Birds.Count); }