// Cohesion public void Cohere(ref Vector3 v) { BoidsEntity boidsEntity = this.GetComponent <BoidsEntity>(); BoidsImpl closestImpl = this.GetClosestBoidImpl(); if (closestImpl == null) { return; } float distance = Vector3.Distance(this.transform.position, closestImpl.transform.position); if (distance > boidsEntity.cohesionDistance) { return; } if (distance > boidsEntity.alignmentDistance && distance > boidsEntity.separateDistance) { v += closestImpl.transform.position - this.transform.position; } if (this.followOnlyLeader && closestImpl != null) { this.followingLeader = true; } }
// Start is called before the first frame update void Start() { this.boidImpl = this.gameObject.GetComponent <BoidsImpl>(); DisolveSpriteAnimation animation = this.GetComponent <DisolveSpriteAnimation>(); this.initialColor = animation.GetColor(); }
private BoidsImpl GetClosestBoidImpl(bool findLeaderBoid = false) { BoidsImpl closestImpl = null; BoidsEntity boidsEntity = this.GetComponent <BoidsEntity>(); GameObject boidsContainer = boidsEntity.config.GetBoidsContainer(); if (boidsContainer == null) { return(closestImpl); } float closestDistance = -1.0f; // NOTE: Since this is first person boids, every BoidsEntity checks distance for every each other BoidsEntity. // To consider performance, this should be done in third person logic. BoidsImpl[] impls = boidsContainer.GetComponentsInChildren <BoidsImpl>(); Vector3 pos = this.transform.position; for (int i = 0; i < impls.Length; i++) { BoidsImpl impl = impls[i]; if (this.followOnlyLeader && !impl.isLeader) { continue; } // ignore self if (GameObject.ReferenceEquals(this.gameObject, impl.gameObject)) { continue; } float distance = Vector3.Distance(impl.transform.position, pos); if (closestDistance < 0 || distance < closestDistance) { closestDistance = distance; closestImpl = impl; } } return(closestImpl); }