Exemplo n.º 1
0
    // Update is called once per frame
    void Update( )
    {
        new Thread(() =>
        {
            //run through all boids.
            for (int i = _boids.Count - 1; i >= 0; --i)
            {
                Boid b = _boids[i];
                //get the boids current velocity.
                Vector3 velocity = b.velocity;

                //add the influences of neighboring boids to the velocity.
                velocity += _alignment.getResult(_boids, i);
                velocity += _cohesion.getResult(_boids, i);
                velocity += _separation.getResult(_boids, i);

                //normalize the velocity and make sure that the boids new velocity is updated.
                velocity.Normalize( );
                b.velocity = velocity;

                b.lookat = b.position + velocity;
            }
        }).Start( );

        for (int i = _boids.Count - 1; i >= 0; --i)
        {
            //update the boids position in the mainthread.
            _boids[i].transform.position += _boids[i].velocity * Time.deltaTime * speed;
        }
    }