private void HandleHorizontalRotation()
    {
        Vector3 rotPointPos = rotationPoint.position;

        //// Check if the focus target is outside the "cam area"
        dirCamToRotPoint   = rotPointPos - transform.position;
        dirFocusToRotPoint = rotPointPos - focusPoint.position;


        Debug.DrawLine(rotPointPos, rotPointPos - (Quaternion.Euler(0, focusCamMaxAngle, 0) * dirCamToRotPoint));
        Debug.DrawLine(rotPointPos, rotPointPos - (Quaternion.Euler(0, -focusCamMaxAngle, 0) * dirCamToRotPoint));
        Debug.DrawLine(rotPointPos, transform.position, Color.blue);
        Debug.DrawLine(rotPointPos, focusPoint.position, Color.green);

        // Calculate Angle between the two points
        float focusToCamAngle = Vector3.Angle(Vector3.Scale(dirCamToRotPoint.normalized, zeroYVec), Vector3.Scale(dirFocusToRotPoint.normalized, zeroYVec));

        // Set a new desired position (/direction) between the camera & the rotation point // TODO Need to make sure it only sets a new desired camtorotdir once until neccessary again
        if (focusToCamAngle >= focusCamMaxAngle)
        {
            desiredCamToRotDir = dirFocusToRotPoint;
        }

        //// Check if Camera needs to be rotated
        float currentCamToDesiredRotPointAngle = Vector3.Angle(Vector3.Scale(dirCamToRotPoint.normalized, zeroYVec), Vector3.Scale(desiredCamToRotDir.normalized, zeroYVec));

        if (currentCamToDesiredRotPointAngle > .5f)
        {
            int   rotDir          = MathHelper.AngleDir(dirCamToRotPoint, desiredCamToRotDir, Vector3.up); // -1: Left, 1: Right
            float speedMultiplier = currentCamToDesiredRotPointAngle / 180;                                // The further away, the faster it turns
            transform.RotateAround(rotationPoint.position, Vector3.up, speedMultiplier * rotDir * maxRotationSpeed * Time.deltaTime);
        }
    }
Ejemplo n.º 2
0
    private void Chase(StateController controller, ActionData actionData)
    {
        if (controller.inputManager.currentTarget == null)
        {
            return;
        }

        if (!controller.inputManager.IsGrounded())
        {
            controller.inputManager.inputForwardJump = false;
            return;
        }
        else if (controller.inputManager.inputForwardJump)
        {
            return;
        }

        if (controller.inputManager.IsGrounded())
        {
            Vector3 myDir = controller.transform.forward;
            myDir.y = 0;
            Vector3 targetDirection =
                (controller.inputManager.currentTarget.transform.position - controller.transform.position);
            targetDirection.y = 0;

            // Calculate the angle between self.forward & the direction towards the target
            float angle = Vector3.Angle(myDir.normalized, targetDirection.normalized); //Quaternion.Angle(/*myRotation*/controller.transform.rotation,targetRotation);

            if (angle > 5)                                                             // TODO HACK: make 5 to a variable
            {
                // Check if target is on the left or right side
                int side = MathHelper.AngleDir(controller.transform.forward, targetDirection, controller.transform.up);

                if (side == -1)
                {
                    controller.inputManager.inputTurnLeft  = true;
                    controller.inputManager.inputTurnRight = false;
                }
                else if (side == 1)
                {
                    controller.inputManager.inputTurnRight = true;
                    controller.inputManager.inputTurnLeft  = false;
                }
            }
            else
            {
                controller.inputManager.inputTurnLeft    = false;
                controller.inputManager.inputTurnRight   = false;
                controller.inputManager.inputForwardJump = true;
            }
            //controller.transform.LookAt(controller.inputManager.currentTarget.transform);
        }
    }