// Use this for initialization
    void Start()
    {
        int max = 25;

        for (int i = 0; i < max; i++)
        {
            GameObject obj = null;
            obj = Instantiate(cube);

            SeparationVehicle c = obj.AddComponent <SeparationVehicle> ();
            vehicles.Add(c);
            obj.transform.parent = this.gameObject.transform;
        }
        cube.SetActive(false);
    }
Пример #2
0
    public Vector3 Separate(List <SeparationVehicle> list)
    {
        float   desiredseparation = r * 2;
        Vector3 sum   = Vector3.zero;
        int     count = 0;

        // For every boid in the system, check if it's too close
        for (int i = 0; i < list.Count; i++)
        {
            SeparationVehicle other = list[i];
            float             d     = Vector3.Distance(this.gameObject.transform.localPosition, other.transform.localPosition);
            // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
            if ((d > 0) && (d < desiredseparation))
            {
                // Calculate vector pointing away from neighbor
                Vector3 diff = this.gameObject.transform.localPosition - other.transform.localPosition;
                diff.Normalize();
                diff /= d;                        // Weight by distance
                sum  += diff;
                count++;                          // Keep track of how many
            }
        }
        // Average -- divide by how many
        if (count > 0)
        {
            // Our desired vector is moving away maximum speed
            //Vector3.Magnitude
            sum /= count;

            sum.Normalize();
            sum *= maxspeed;
            // Implement Reynolds: Steering = Desired - Velocity
            sum = sum - velocity;
            sum = Vector3.ClampMagnitude(sum, maxforce);
        }
        return(sum);
    }