コード例 #1
0
ファイル: Flock.cs プロジェクト: simonchauvin/ggj2018
    void FixedUpdate()
    {
        //int updateStart = (int)(Random.value * (boids.Length - numberOfBoids / 5 - 1));
        barycenter = Vector3.zero;
        for (int i = 0; i < numberOfBoids; i++)
        {
            Boid boid = boids[i];
            if (boid != null && boid.thisRigidbody != null)
            {
                if (boid.leader)
                {
                    // Random walks
                    if (randomWalk)
                    {
                        leader.thisRigidbody.velocity += (leader.transform.position - target).normalized * Time.deltaTime;
                        if (timeSinceLastRandomWalk >= randomWalkTime)
                        {
                            checkLeaderWalk();
                        }
                        timeSinceLastRandomWalk += Time.deltaTime;
                    }
                    else if (returnWalk)
                    {
                        leader.thisRigidbody.velocity += (leader.transform.position - target).normalized * Time.deltaTime;
                        if (timeSinceLastReturn >= returnTime)
                        {
                            checkLeaderWalk();
                        }
                        timeSinceLastReturn += Time.deltaTime;
                    }
                    else
                    {
                        if (timeSinceLastCheck >= 5f)
                        {
                            checkLeaderWalk();
                            timeSinceLastReturn = 0f;
                        }
                        timeSinceLastCheck += Time.deltaTime;
                    }
                }
                else
                {
                    Vector3 following  = follow(boid) * followWeight * Time.deltaTime;
                    Vector3 alignment  = align(boid) * alignmentWeight * Time.deltaTime;
                    Vector3 cohesion   = cohere(boid) * cohesionWeight * Time.deltaTime;
                    Vector3 separation = separate(boid) * separationWeight * Time.deltaTime;
                    if (GameManager.instance.debugBoids)
                    {
                        boid.showFollowingDebug(following);
                        boid.showAlignmentDebug(alignment);
                        boid.showCohesionDebug(cohesion);
                        boid.showSeparationDebug(separation);
                    }

                    boid.thisRigidbody.velocity += (following + alignment + cohesion + separation);
                    //boid.thisRigidbody.AddForce(align(boid) * alignmentWeight);
                    //boid.thisRigidbody.AddForce(cohere(boid) * cohesionWeight);
                    //boid.thisRigidbody.AddForce(separate(boid) * separationWeight);

                    // Navfields
                    Navfield navfield = navfieldManager.getNavfield(boid.transform.position);
                    if (navfield != null)
                    {
                        boid.applyNavfieldBehavior(navfield);
                    }
                }

                //Guigui Fix
                float peurDuSol = Mathf.Pow(8.0f - Mathf.Clamp(boid.transform.position.y, 0.0f, 8.0f), 2.0f);

                float vely = boid.thisRigidbody.velocity.y + Time.fixedDeltaTime * peurDuSol * Mathf.Abs(Mathf.Min(0, boid.thisRigidbody.velocity.y));
                if (peurDuSol > 0 && vely < 5.0f)
                {
                    vely += 15.0f * Time.fixedDeltaTime;
                }
                float speed = boid.thisRigidbody.velocity.magnitude;
                boid.thisRigidbody.velocity = new Vector3(boid.thisRigidbody.velocity.x, vely, boid.thisRigidbody.velocity.z);

                if (speed > 10.0f)
                {
                    speed = 10.0f;
                }

                boid.thisRigidbody.velocity = boid.thisRigidbody.velocity.normalized * speed;

                barycenter += boid.transform.position;
            }
        }
        barycenter = barycenter / numberOfBoids;
    }
コード例 #2
0
 public void applyNavfieldBehavior(Navfield navfield)
 {
     thisRigidbody.velocity += navfield.getForce(transform.position) * Time.deltaTime;
 }