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

        // Get direction to the target
        IFollowable followable        = character.GetComponent <IFollowable>();
        Vector3     characterVelocity = followable.GetVelocity();

        Vector3 direction;

        if (target == null)
        {
            direction = characterVelocity.normalized;
        }
        else
        {
            direction = target.transform.position - character.transform.position;
        }
        float distance = direction.magnitude;

        // Velocity that we want to have
        Vector3 targetVelocity;

        // The target velocity combines speed and direction
        targetVelocity = direction;
        targetVelocity.Normalize();
        targetVelocity *= maxSpeed;

        // Acceleration tries to get to the target velocity
        steering.linear  = targetVelocity - characterVelocity;
        steering.linear /= timeToTarget;

        // Check if the acceleration is too fast
        if (steering.linear.magnitude > maxAcceleration)
        {
            steering.linear.Normalize();
            steering.linear = steering.linear * maxAcceleration;
        }

        // Output the steering
        steering.angular = 0;
        return(steering);
    }
Пример #2
0
    public override SteeringOutput GetSteering()
    {
        // Create the structure to hold our output
        SteeringOutput steering = new SteeringOutput();

        // Get direction to the target
        Vector3 direction = target.transform.position - character.transform.position;
        float   distance  = direction.magnitude;

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

        // This is the speed that we want to have
        float targetSpeed;

        // If we are outside the slow radius, the go to max speed
        if (distance > slowRadius)
        {
            targetSpeed = maxSpeed;
        }   // Otherwise calculate scaled speed
        else
        {
            //Debug.Log("Pingo " + character.name);
            targetSpeed = maxSpeed * (distance / slowRadius);
        }

        // Velocity that we want to have
        Vector3 targetVelocity;

        // The target velocity combines speed and direction
        targetVelocity = direction;
        targetVelocity.Normalize();
        targetVelocity *= targetSpeed;

        Vector3 characterVelocity = Vector3.zero;

        // Get characters current speed
        IFollowable followable = character.GetComponent <IFollowable>();

        characterVelocity = followable.GetVelocity();

        /*if (character.GetComponent<AIMovement>() != null)
         * {
         *  AIMovement aiMovement = character.GetComponent<AIMovement>();
         *  characterVelocity = aiMovement.GetVelocity();
         * } else if (character.GetComponent<FishController>() != null)
         * {
         *  FishController fishController = character.GetComponent<FishController>();
         *  characterVelocity = fishController.GetVelocity();
         * } else
         * {
         *  throw new System.Exception("The is no component of the game object " + character.name + " which has a velocity parameter (ie. AIMovement or FishController)");
         * }*/

        // Acceleration tries to get to the target velocity
        steering.linear  = targetVelocity - characterVelocity;
        steering.linear /= timeToTarget;

        // Check if the acceleration is too fast
        if (steering.linear.magnitude > maxAcceleration)
        {
            steering.linear.Normalize();
            steering.linear = steering.linear * maxAcceleration;
        }

        // Output the steering
        steering.angular = 0;
        return(steering);
    }