//队伍对齐--目前用不到 /*private Vector3 Alignment(List<CUnitSteeringComp> neighbors) * { * //存储平均方向 * Vector3 averageHeading = Vector3.zero; * * //存储需要对齐的人的个数 * int neighborCount = 0; * for (int a = 0; a < neighbors.Count; a++) { * CUnitSteeringComp item = neighbors[a]; * if (item == this) continue; * if (!InSight(item.Position)) continue; * * averageHeading += item.Heading; * neighborCount++; * } * * if (neighborCount > 0) { * averageHeading /= neighborCount; * averageHeading -= Heading; * } * * return averageHeading; * }*/ //队伍聚合. 找到质心, 然后找过去 private Vector3 Cohesion(List <CUnitSteeringComp> neighbors) { //队伍的质心 Vector3 centerOfMass = Vector3.zero; Vector3 force = Vector3.zero; int neighborCount = 0; for (int a = 0; a < neighbors.Count; a++) { CUnitSteeringComp item = neighbors[a]; if (item == this) { continue; } if (!InSight(item.Position)) { continue; } centerOfMass += item.Position; neighborCount++; } if (neighborCount > 0) { centerOfMass /= neighborCount; force = Seek(centerOfMass); } return(force.normalized); }
//各个群体要求独立 private Vector3 Separation(List <CUnitSteeringComp> neighbors) { Vector3 force = Vector3.zero; for (int a = 0; a < neighbors.Count; a++) { CUnitSteeringComp item = neighbors[a]; if (item == this) { continue; } if (!TooClose(item.Position)) { continue; } Vector3 toAgent = Position - item.Position; //这个很慢啊. 所以对于neighbors还要进行筛选 //力的大小反比 与邻居的距离 force += toAgent.normalized; } return(force); }