예제 #1
0
    void ApplyFlockRules()
    {
        Vector3 vCenter           = Vector3.zero;
        Vector3 vAvoid            = Vector3.zero;
        float   flockSpeed        = 0.01f;
        int     neighborGroupSize = 0;

        float currentNeighborDistance = 0;

        foreach (GameObject fish in manager.fishArray)
        {
            if (fish != this.gameObject)
            {
                currentNeighborDistance = Vector3.Distance(fish.transform.position, this.transform.position);
                if (currentNeighborDistance <= manager.neighborDistance)
                {
                    vCenter += fish.transform.position; neighborGroupSize++;
                    if (currentNeighborDistance < manager.avoidDistance)
                    {
                        vAvoid += (this.transform.position - fish.transform.position);
                    }
                    flockSpeed += fish.GetComponent <FlockController_Fish>().speed;
                }
            }
        }

        if (neighborGroupSize > 0)
        {
            vCenter     = vCenter / neighborGroupSize + (manager.GetFlockTarget().position - this.transform.position);
            flockSpeed /= neighborGroupSize;

            Vector3 direction = (vCenter + vAvoid) - transform.position;

            if (direction != Vector3.zero)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), manager.rotationSpeed * Time.deltaTime);
            }
        }
        else
        {
            vCenter = manager.GetFlockTarget().position - this.transform.position;
        }
    }