void AccumulateForce()
    {
        Vector3 accumulatedForce = Vector3.zero;

        for (int i = 0; i < activeMovementBehaviors.Count; i++)
        {
            AutonomousMovementBehavior behavior = activeMovementBehaviors[i];

            Vector3 resultantForce = behavior.CalculateForce(this);

            float magnitudeOfResultantForce = resultantForce.magnitude;
            float remainingForce            = maximumSteeringForce - accumulatedForce.magnitude;

            if (magnitudeOfResultantForce <= remainingForce)
            {
                accumulatedForce += resultantForce;
            }
            else
            {
                resultantForce    = Vector3.ClampMagnitude(resultantForce, remainingForce);
                accumulatedForce += resultantForce;
                break;
            }
        }

        accumulatedForce.y = 0f;

        float accumulatedForceSqrMag = accumulatedForce.sqrMagnitude;

        entityRigidbody.AddForce(accumulatedForce, ForceMode.VelocityChange);
        entityRigidbody.velocity = Vector3.ClampMagnitude(entityRigidbody.velocity, MaxSpeed);
    }
Esempio n. 2
0
    void AccumulateForce()
    {
        if (activeMovementBehaviors.Count == 0)
        {
            currentVelocity = Vector3.zero;
            return;
        }
        Vector3 accumulatedForce = Vector3.zero;

        for (int i = 0; i < activeMovementBehaviors.Count; i++)
        {
            AutonomousMovementBehavior behavior = activeMovementBehaviors[i];

            Vector3 resultantForce = behavior.CalculateForce(this);

            float magnitudeOfResultantForce = resultantForce.magnitude;
            float remainingForce            = maximumSteeringForce - accumulatedForce.magnitude;

            if (magnitudeOfResultantForce <= remainingForce)
            {
                accumulatedForce += resultantForce;
            }
            else
            {
                resultantForce    = Vector3.ClampMagnitude(resultantForce, remainingForce);
                accumulatedForce += resultantForce;
                break;
            }
        }

        accumulatedForce.y = 0f;

        Vector3 acceleration = accumulatedForce / entityRigidbody.mass;

        currentVelocity += acceleration * Time.deltaTime;
        currentVelocity  = Vector3.ClampMagnitude(currentVelocity, maxSpeed);

        Vector3 projectedPosition = transform.position + (currentVelocity * Time.deltaTime);

        projectedPosition.y = 20f;

        RaycastHit hit;

        if (Physics.Raycast(projectedPosition, Vector3.down, out hit, float.MaxValue, terrainLayerMask))
        {
            projectedPosition.y = hit.point.y + (transform.lossyScale.y);
            Vector3 toPosition = projectedPosition - transform.position;
            toPosition          = Vector3.ClampMagnitude(toPosition, maxSpeed);
            transform.position += toPosition;
        }
    }