Пример #1
0
    public Vector2 UpdateBoid(List <Boid> allBoids)
    {
        if (hasPath)
        {
            return(Vector2.zero);
        }

        var sum   = Vector2.zero;
        var count = 0;

        foreach (var boid in allBoids)
        {
            if (boid.gameObject == gameObject)
            {
                continue;
            }
            var diff     = rBody.position - boid.RBody.position;
            var distance = diff.magnitude;

            if (distance > 0 && distance < boid.AlignCircle)
            {
                sum   += boid.RBody.position;
                count += 1;
            }
        }
        if (count > 0)
        {
            sum /= count;
            return(TargetBoid.Steer(sum, rBody, 10, boid) * boid.CohesionMultiplier);
        }

        return(Vector2.zero);
    }
Пример #2
0
    public Vector2 UpdateBoid(List <Boid> allBoids)
    {
        var pointInFuture = rBody.position + rBody.velocity.normalized * distanceForward;
        var randomCircle  = Random.insideUnitCircle;

        var target = pointInFuture + randomCircle * radius;

        return(TargetBoid.Steer(target, rBody, 10, boid));
    }
Пример #3
0
    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);
    }