Пример #1
0
    public override SteeringOutput GetSteering()
    {
        // Create the structure to hold our output
        SteeringOutput steering = new SteeringOutput();

        // Get orientation to the target
        float rotation = target.transform.eulerAngles.z - character.transform.eulerAngles.z;

        // Map the result to the (-pi, pi) interval
        rotation = MapToRange(rotation);
        float rotationSize = Mathf.Abs(rotation);

        // Check if we are there, return no steering
        if (rotationSize < targetRadius)
        {
            steering.linear  = Vector3.zero;
            steering.angular = 0f;
            return(steering);
        }

        // This is the orientation that we want to have
        float targetRotation;

        // If we are outside the slow radius, the go to max speed
        if (rotationSize > slowRadius)
        {
            targetRotation = maxSpeed;
        }   // Otherwise calculate scaled orientation
        else
        {
            targetRotation = maxSpeed * rotationSize / slowRadius;
        }

        // The final target rotation combines speed (already in the variable) and direction
        targetRotation *= rotation / rotationSize;

        // Get characters current orientation
        IFollowable followable        = character.GetComponent <IFollowable>();
        float       characterRotation = followable.GetRotation();

        // Acceleration tries to get to the target orientation
        steering.angular  = targetRotation - characterRotation;
        steering.angular /= timeToTarget;

        // Check if the acceleration is too fast
        float angularAcceleration = Mathf.Abs(steering.angular);

        if (angularAcceleration > maxAcceleration)
        {
            steering.angular /= angularAcceleration;
            steering.angular  = steering.angular * maxAcceleration;
        }

        // Output the steering
        steering.linear = Vector3.zero;
        return(steering);
    }