我在C#中为我的团结项目创建了一个植绒算法。我试图跟随this example,但问题是把所有东西放在一起,而我却没有推断出来。以下代码是我希望拥有领导者跟随植绒系统的一部分。
我已经添加了以下/寻求转向基本的三个转向子程序(对齐,内聚,分离),但是现在这些boids刚刚聚集在领导者的顶部并且遮住了他。我希望他们能够跟随领导者,但也要保持彼此分离,而不是仅仅相互堆积,就像他们不同步一样。
当用户选择一组boids,然后选择他们应该聚集到的位置时,boid最接近的是分配的leader并被分配到目标的A *路径。然后其他boids调用flocking而不是路径代码。
void Update()
{
if (flock.Count > 0 &&
flockLeader != gameObject)
{
Flocking();
}
else
{
Movement();
}
}
private void Flocking()
{
Vector3 vector = Vector3.zero, alignment = Alignment(), cohesion = Cohesion(), separation = Separation(), following = Following();
int weightAlignment = 1, weightCohesion = 1, weightSeparation = 1, weightFollowing = 1;
NeighbourhoodWatch(); // determine neighbour boids (exclude self)
vector += (separation * weightSeparation);
vector += (alignment * weightAlignment);
vector += (cohesion * weightCohesion);
vector += (following * weightFollowing);
vector.Normalize();
if (!vector.Equals(Vector3.zero))
{
if (Vector3.Angle(vector, transform.forward) >= 1f)
{
transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, vector, rateOfTurn * Time.deltaTime, 0.0f));
}
else // rotate and move
{
transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, vector, rateOfTurn * Time.deltaTime, 0.0f));
transform.Translate(Vector3.forward * (speed * Time.deltaTime));
}
}
}
private Vector3 Alignment()
{
Vector3 vector = Vector3.zero;
if (neighbourhood.Count > 0)
{
foreach (GameObject boid in neighbourhood)
{
vector += boid.transform.forward;
}
vector /= neighbourhood.Count;
}
return vector;
}
private Vector3 Cohesion()
{
Vector3 vector = Vector3.zero;
if (neighbourhood.Count > 0)
{
foreach (GameObject boid in neighbourhood)
{
vector += boid.transform.position; // acquire centre of mass
}
vector /= neighbourhood.Count;
vector = vector - transform.position; // direction to centre of mass
}
return vector;
}
private Vector3 Separation()
{
Vector3 vector = Vector3.zero;
if (neighbourhood.Count > 0)
{
int spaceInvaders = 0;
foreach (GameObject boid in neighbourhood)
{
float proximity = Vector3.Distance(boid.transform.position, transform.position);
if (proximity <= footprint)
{
vector += boid.transform.position - transform.position;
spaceInvaders++;
}
}
if (spaceInvaders > 0)
{
vector /= spaceInvaders;
vector *= -1; // negation (invert direction)
}
}
return vector;
}
private Vector3 Following()
{
Vector3 vector = Vector3.zero;
vector = flockLeader.transform.position - transform.position;
return vector;
}
答案 0 :(得分:0)
我有一些时间尝试类似的东西,如果你想让你的单位与你的领导者有距离,只需取领导速度并使用反向向量,反向向量将是你的单位目的地,也使用向量.scale如果你想调整自己的分离距离。