结合上一篇文章讲述群组行为的算法,这篇是替换实现,更加清晰。
FlockController的行为很简单,它在运行时生成boid的行为,更新群组的中心及群组的平均速度
public class FlockController : MonoBehaviour {
public float minVelocity = 1;
public float maxVeclocity = 8;
public int flockSize = 20;
public float centerWeight = 1;
public float velocityWeight = 1;
public float separationWeight = 1;
public float followWeight = 1;
public float randomizeWeight = 1;
public Flock perfab;
public Transform target;
public Vector3 flockCenter;
public Vector3 flockVelocity;
public ArrayList flockList = new ArrayList();
void Start () {
for(int i = 0; i < flockSize; i++)
{
Flock flock = Instantiate(perfab,transform.position,transform.rotation) as Flock;
flock.transform.parent = transform;
flock.controller = this;
flockList.Add(floc