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;
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (pType == PathType.AStar)
        {
            target.transform.position      = aStarChar.transform.position;
            currentNode.position           = aStarChar.transform.position;
            debugSphere.transform.position = aStarChar.transform.position;
        }
        else
        {
            float distance = Vector3.Distance(new Vector3(currentNode.position.x, 0, currentNode.position.z), fM.AveragePosition());

            if (distance < currentNode.radius)
            {
                NextNode();
            }
            else if (pType == PathType.Random)
            {
                randomNodeTime += Time.deltaTime;

                if (randomNodeTime >= defaultRandomNodeTime)
                {
                    NextNode();
                    randomNodeTime = 0.0f;
                }
            }
        }
    }