Ejemplo n.º 1
0
    //applying movement to unit
    /// <summary>
    /// Calculates the forces that steer unit's direction
    /// </summary>
    public void CalcSteeringForces()
    {
        //Create a new ultimate force that's zeroed
        Vector3 ultForce = Vector3.zero;

        //Move towards the given target
        ultForce += Seek(target.transform.position).normalized *seekWeight;

        //flocking
        //align
        ultForce += Alignment(fM.AverageDirection()).normalized *alignWeight;

        //cohere
        ultForce += Cohesion(fM.AveragePosition()).normalized *cohesionWeight;

        //separate
        foreach (GameObject flock in neighbors)
        {
            ultForce += Separation(flock).normalized *separationWeight;
        }

        RaycastHit hit;

        if (Physics.Raycast(new Ray(transform.position, transform.forward), out hit, 5.0f, 1 << 8))
        {
            ultForce += (transform.position - hit.point).normalized * obstacleAvoidWeight;
        }

        //apply the force to the units acceleration
        acceleration += ultForce / mass;
    }