Пример #1
0
    void Flock()
    {
        float   speed       = velocity.magnitude;
        Vector3 avgVelocity = Vector3.zero;
        Vector3 avgPosition = Vector3.zero;
        float   count       = 0;
        float   f           = 0.0f;
        float   dirVector   = 0.0f;
        Vector3 myPosition  = transformComponent.position;
        Vector3 forceV;
        Vector3 toAvg;
        Vector3 wantedVel;

        for (int i = 0; i < objects.Length; i++)
        {
            Transform transform = objects[i];
            if (transform != transformComponent)
            {
                Vector3 otherPosition = transform.position;
                // Average position to calculate cohesion
                avgPosition += otherPosition;
                count++;
                //Directional vector from other flock to this flock
                forceV = myPosition - otherPosition;
                //Magnitude of that directional vector(Length)
                dirVector = forceV.magnitude;
                //Add push value if the magnitude, the length of the
                //vector, is less than followRadius to the leader
                if (dirVector < followRadius)
                {
                    //calculate the velocity, the speed of the object, based
                    //on the avoidance distance between flocks if the
                    //current magnitude is less than the specified
                    //avoidance radius
                    if (dirVector < avoidanceRadius)
                    {
                        f = 1.0f - (dirVector / avoidanceRadius);
                        if (dirVector > 0)
                        {
                            avgVelocity +=
                                (forceV / dirVector) * f * avoidanceForce;
                        }
                    }
                    //just keep the current distance with the leader
                    f = dirVector / followRadius;
                    ZebraFlock tempOtherFlock = otherFlocks[i];
                    //we normalize the tempOtherFlock velocity vector to get
                    //the direction of movement, then we set a new velocity
                    avgVelocity += tempOtherFlock.normalizedVelocity * f *
                                   followVelocity;
                }
            }
        }

        if (count > 0)
        {
            //Calculate the average flock velocity(Alignment)
            avgVelocity /= count;
            //Calculate Center value of the flock(Cohesion)
            toAvg = (avgPosition / count) - myPosition;
        }
        else
        {
            toAvg = Vector3.zero;
        }
        //Directional Vector to the leader
        forceV    = origin.position - myPosition;
        dirVector = forceV.magnitude;
        f         = dirVector / toOriginRange;
        //Calculate the velocity of the flock to the leader
        if (dirVector > 0)         //if this boid is not at the center of the flock
        {
            originPush = (forceV / dirVector) * f;
        }

        if (speed < minSpeed && speed > 0)
        {
            velocity = (velocity / speed) * minSpeed;
            //Debug.Log("speed "+speed);
        }

        wantedVel = velocity;
        //Calculate final velocity
        wantedVel  -= wantedVel * Time.deltaTime;
        wantedVel  += randomPush * Time.deltaTime;
        wantedVel  += originPush * Time.deltaTime;
        wantedVel  += avgVelocity * Time.deltaTime;
        wantedVel  += toAvg.normalized * gravity * Time.deltaTime;
        wantedVel.y = 0;
        //Final Velocity to rotate the flock into
        velocity = Vector3.RotateTowards(velocity, wantedVel,
                                         turnSpeed * Time.deltaTime, 100.00f);
        transformComponent.rotation =
            Quaternion.LookRotation(velocity);
        //Move the flock based on the calculated velocity
        transformComponent.Translate(velocity * Time.deltaTime,
                                     Space.World);
        //normalise the velocity
        normalizedVelocity = velocity.normalized;
    }
Пример #2
0
 // Use this for initialization
 void Start()
 {
     anim = GetComponent <Animator>();
     a    = GameObject.FindGameObjectWithTag("LeaderZebra");
     fc   = GetComponent <ZebraFlock>();
 }