void Start() { for (int i = 0; i < flockSize; i++) { FlockSimple tFlockSimple = Instantiate(Prefab, transform.position, transform.rotation); tFlockSimple.transform.parent = transform; tFlockSimple.flockController = this; flockList.Add(tFlockSimple); } }
void Update() { //Calculate the Center and Velocity of the whole flock group Vector3 tCenter = Vector3.zero; Vector3 tVelocity = Vector3.zero; for (int i = 0; i < flockList.Count; i++) { FlockSimple tFlockSimple = flockList[i]; tCenter += tFlockSimple.transform.localPosition; tVelocity += tFlockSimple.rigidbody.velocity; } flockCenter = tCenter / flockSize; flockVelocity = tVelocity / flockSize; }
private Vector3 steer() { Vector3 tCenter = flockController.flockCenter - transform.localPosition; //cohesion Vector3 tVelocity = flockController.flockVelocity - rigidbody.velocity; //alignment Vector3 tFollow = flockController.Target.localPosition - transform.localPosition; //follow leader Vector3 separation = Vector3.zero; for (int i = 0; i < flockController.flockList.Count; i++) { FlockSimple tFlockSimple = flockController.flockList[0]; if (tFlockSimple != this) { Vector3 tRelativePos = transform.localPosition - tFlockSimple.transform.localPosition; separation += tRelativePos; //(relativePos.sqrMagnitude) } } //randomize Vector3 tRandomize = new Vector3((Random.value * 2) - 1, (Random.value * 2 - 1), (Random.value * 2) - 1); tRandomize.Normalize(); return(flockController.centerWeight * tCenter + flockController.velocityWeight * tVelocity + flockController.SeparationWeight * separation + flockController.followWeight * tFollow + flockController.randomizeWeight * tRandomize); }