예제 #1
0
    void ApplyRules()
    {
        GameObject[] gos;
        gos = glovalflock2.allFish;

        Vector3 vcentre = Vector3.zero;
        Vector3 vavoid  = Vector3.zero;

        float gSpeed = 5.0f;

        Vector3 goalpos = glovalflock2.goalpos;

        float dist;

        int groupsize = 0;

        foreach (GameObject go in gos)
        {
            if (go != this.gameObject)
            {
                dist = Vector3.Distance(go.transform.position, this.transform.position);
                if (dist <= neighborDistance)
                {
                    vcentre += go.transform.position;
                    groupsize++;

                    if (dist < 2.0f)
                    {
                        vavoid = vavoid + (this.transform.position - go.transform.position);
                    }
                    flock2 anotherFlock = go.GetComponent <flock2>();
                    gSpeed = gSpeed + anotherFlock.speed;
                }
            }
        }

        if (groupsize > 0)
        {
            vcentre = vcentre / groupsize + (goalpos - this.transform.position);
            speed   = gSpeed / groupsize;

            Vector3 direction = (vcentre + vavoid) - transform.position;
            if (direction != Vector3.zero)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationspeed * Time.deltaTime);
            }
        }
    }
예제 #2
0
    void ApplyRules()
    {
        GameObject[] gos;
        gos = globalFlock2.allFish2;

        Vector3 g_centre = Vector3.zero;
        Vector3 g_avoid  = Vector3.zero;
        float   g_speed  = 0.1f;

        Vector3 goalPos = globalFlock2.goalPos;

        float dist;
        int   groupSize = 0;           //团队规模数量

        foreach (GameObject go in gos) //计算自己与每条鱼的距离
        {
            if (go != this.gameObject && go != null)
            {
                dist = Vector3.Distance(go.transform.position, this.transform.position);
                if (dist <= neighbourDistance)         //如果是在邻居范围就加入团队
                {
                    g_centre += go.transform.position; //计算总的团队中心
                    groupSize++;

                    if (dist < 1.0f)//如果太近就计算出防撞向量
                    {
                        g_avoid = g_avoid + (this.transform.position - go.transform.position);
                    }

                    flock2 antherFlock = go.GetComponent <flock2>();
                    g_speed = g_speed + antherFlock.speed;//计算团队总速度
                }
            }
        }

        if (groupSize > 0)
        {
            g_centre = g_centre / groupSize + (goalPos - this.transform.position);
            speed    = g_speed / groupSize;

            Vector3 direction = (g_centre + g_avoid) - transform.position;
            if (direction != globalFlock2.center2.position)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
            }
            //使鱼平滑转向
        }
    }