예제 #1
0
    /* Called by the physics engine. Update loop.
     */
    void FixedUpdate()
    {
        // Note: this doesn't conflict with adding boids at runtime because everything executes in one frame
        foreach (GameObject vehicle in boids)
        {
            Boid      boid = vehicle.GetComponent <Boid>();
            Rigidbody body = boid.GetComponent <Rigidbody>();

            if (alignmentEnabled)
            {
                body.AddForce(boid.Align() * alignmentWeight);
            }

            if (separationEnabled)
            {
                body.AddForce(boid.Separate() * separationWeight);
            }

            if (cohesionEnabled)
            {
                body.AddForce(boid.Cohere() * cohesionWeight);
            }

            if (seekEnabled)
            {
                Ray        ray = camera.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                // If the camera is pointing somewhere on the floor
                if (Physics.Raycast(ray, out hit))
                {
                    body.AddForce(boid.Seek(hit.point) * seekWeight);
                }
            }

            if (wanderEnabled)
            {
                body.AddForce(boid.Wander() * wanderWeight);
            }
        }
    }