// Update is called once per frame void Update() { GameObject[] boids = GameObject.FindGameObjectsWithTag ("Boid"); averageVelocity = Vector3.zero; int groupCount = 0; foreach(GameObject boid in boids) { Vector3 v = boid.rigidbody.velocity; // don't count yourself as part of the group if(ReferenceEquals(boid,this.gameObject)) continue; // don't count boids to far away as part of the group if(Vector3.Distance(this.transform.position,boid.transform.position) > groupRadius) continue; averageVelocity += v; groupCount++; } if (groupCount == 0 || groupCount == 1) return; averageVelocity /= groupCount; Vector3 velToSend = averageVelocity * damping; // send the change as a force to the post-updater behavStateDel bD = (o) => rigidbody.AddForce (velToSend); // send the direct velocity change to the post-updater //behavioralDelegate bD = () => rigidbody.velocity += velToSend; BehavioralEvent bE = new BehavioralEvent(bD,behavioralPriority); postUpdater.addEvent (bE); }
// Update is called once per frame void Update() { GameObject[] boids = GameObject.FindGameObjectsWithTag ("Boid"); foreach(GameObject other in boids) { if(other == this.gameObject) continue; Vector3 otherPos = other.transform.position; behavStateDel bD = (o) => rigidbody.AddExplosionForce(forceVal,otherPos,minDistance); BehavioralEvent bE = new BehavioralEvent(bD,behavioralPriority); postUpdater.addEvent(bE); } }
// Update is called once per frame void Update() { GameObject[] boids = GameObject.FindGameObjectsWithTag ("Boid"); centerOfMass = Vector3.zero; int groupCount = 0; foreach(GameObject boid in boids) { if(Vector3.Distance(this.transform.position,boid.transform.position) > groupRadius) continue; centerOfMass += boid.transform.position; groupCount++; } if (groupCount == 0 || groupCount == 1) { return; } centerOfMass /= (float)groupCount; float dist = Vector3.Distance (centerOfMass, this.transform.position); BehavioralEvent bE = null; if (dist > targetRadius) bE = new BehavioralEvent ((o) => rigidbody.AddExplosionForce (-forceVal, centerOfMass, dist + 0.1f), behavioralPriority); if (bE != null) postUpdater.addEvent (bE); }
public void addEvent(BehavioralEvent bE) { toRun.Add (bE); }