public Vector2 UpdateBoid(List <Boid> allBoids) { path = boid.Path; if (path == null) { return(Vector2.zero); } var positions = path.positions.Select(p => p.position).ToList(); var worldRecord = float.PositiveInfinity; var pos = rBody.position + rBody.velocity.normalized * pathPredict; Vector3 normal = Vector3.zero; Vector3 target = Vector3.zero; for (int i = 0; i < positions.Count; i++) { var a = positions[i]; var b = positions[(i + 1) % positions.Count]; // wrap around var normalPoint = GetNormalPoint(pos, a, b); var dir = b - a; if (normalPoint.x < Mathf.Min(a.x, b.x) || normalPoint.x > Mathf.Max(a.x, b.x) || normalPoint.y < Mathf.Min(a.y, b.y) || normalPoint.y > Mathf.Max(a.y, b.y)) { normalPoint = b; a = positions[(i + 1) % positions.Count]; b = positions[(i + 2) % positions.Count]; dir = b - a; } var d = Vector3.Distance(pos, normalPoint); if (d < worldRecord) { worldRecord = d; normal = normalPoint; dir = dir.normalized * pathPredict; target = normal; target += dir; } } if (worldRecord > path.radius) { return(TargetBoid.Steer(target, rBody, 0, boid) * boid.PathMultiplier); } return(Vector2.zero); }
private void Awake() { rBody = GetComponent <Rigidbody2D>(); boid = GetComponent <Boid>().AddBehaviour(this); path = boid.Path; }