/// <summary> /// Find the average in the crowds location /// </summary> /// <returns></returns> Vector3 Cohesion() { Vector3 sum = Vector3.zero; int count = 0; foreach (var agent in _agentManager.Crowd) { //Don't include self in calculation if (agent == gameObject) { continue; } Agent other = agent.GetComponent <Agent>(); float distance = Vector3.Distance(_position, other.GetPosition()); //If the other agent is within distance then include their position for consideration when flocking if (!(distance < _agentManager.NeighbourDistance)) { continue; } sum += other.GetPosition(); count++; } // If there are no neighbors in the vicinity return a zero vector if (count <= 0) { return(Vector3.zero); } // Get average velocity sum /= count; return(Seek(sum)); }
Vector3 Seperation() { Vector3 sum = Vector3.zero; int count = 0; foreach (var agent in _agentManager.Crowd) { if (agent == gameObject) { continue; } Agent other = agent.GetComponent <Agent>(); float distance = Vector3.Distance(_position, other.GetPosition()); if (distance < _agentManager.NeighbourDistance) { Vector3 dVector = gameObject.GetComponent <Agent>().GetPosition() - other.GetPosition(); if (dVector.magnitude > 0) { sum += dVector.normalized / dVector.magnitude; } count++; } } if (count <= 0) { return(Vector3.zero); } return(sum); }