// Update is called once per frame
        void FixedUpdate()
        {
            RightJoystickInput = RightJoystick.GetInputDirection();
            float xMovementRightJoystick = RightJoystickInput.x; // The horizontal movement from joystick 02
            float zMovementRightJoystick = RightJoystickInput.y; // The vertical movement from joystick 02

            //== RIGHT ROTATION BEGIN
            //TargetObject.transform.Translate(xMovementRightJoystick * touchSensitivity, zMovementRightJoystick * touchSensitivity, 0);

            float tempAngle = Mathf.Atan2(zMovementRightJoystick, xMovementRightJoystick);

            xMovementRightJoystick *= Mathf.Abs(Mathf.Cos(tempAngle));
            zMovementRightJoystick *= Mathf.Abs(Mathf.Sin(tempAngle));

            // calculate the player's direction based on angle

            RightJoystickInput = new Vector3(xMovementRightJoystick, 0, zMovementRightJoystick);
            RightJoystickInput = transform.TransformDirection(RightJoystickInput);

            // rotate the player to face the direction of input
            Vector3 temp = transform.position;

            temp.x += xMovementRightJoystick;
            temp.z += zMovementRightJoystick;
            Vector3 lookDirection = temp - transform.position;

            if (lookDirection != Vector3.zero)
            {
                TargetObject.transform.localRotation = Quaternion.Slerp(TargetObject.transform.localRotation, Quaternion.LookRotation(lookDirection), rotationSpeed * Time.deltaTime);
            }

            //== RIGHT ROTATION END
        }
Exemplo n.º 2
0
        void FixedUpdate()
        {
            // get input from both joysticks
            if (isFlipDirection)
            {
                leftJoystickInput  = -1 * leftJoystick.GetInputDirection();
                rightJoystickInput = -1 * rightJoystick.GetInputDirection();
            }
            else
            {
                leftJoystickInput  = leftJoystick.GetInputDirection();
                rightJoystickInput = rightJoystick.GetInputDirection();
            }

            float xMovementLeftJoystick = leftJoystickInput.x;   // The horizontal movement from joystick 01
            float zMovementLeftJoystick = leftJoystickInput.y;   // The vertical movement from joystick 01

            float xMovementRightJoystick = rightJoystickInput.x; // The horizontal movement from joystick 02
            float zMovementRightJoystick = rightJoystickInput.y; // The vertical movement from joystick 02

            // if there is no input on the left joystick
            if (leftJoystickInput == Vector3.zero)
            {
            }
            // if there is no input on the right joystick
            if (rightJoystickInput == Vector3.zero)
            {
            }

            //****************************************************************************** (1) if there is only input from the left joystick
            if (leftJoystickInput != Vector3.zero)
            {
                if (isLeftActive)
                {
                    //== LEFT MOVEMENT BEGIN
                    //TargetObject.transform.Translate(xMovementLeftJoystick * touchSensitivity, zMovementLeftJoystick * touchSensitivity, 0);

                    float tempAngle = Mathf.Atan2(zMovementLeftJoystick, xMovementLeftJoystick);
                    xMovementLeftJoystick *= Mathf.Abs(Mathf.Cos(tempAngle));
                    zMovementLeftJoystick *= Mathf.Abs(Mathf.Sin(tempAngle));

                    // calculate the player's direction based on angle

                    leftJoystickInput  = new Vector3(xMovementLeftJoystick, 0, zMovementLeftJoystick);
                    leftJoystickInput  = transform.TransformDirection(leftJoystickInput);
                    leftJoystickInput *= moveSpeed;

                    // rotate the player to face the direction of input
                    Vector3 temp = transform.position;
                    temp.x += xMovementLeftJoystick;
                    temp.z += zMovementLeftJoystick;
                    Vector3 lookDirection = temp - transform.position;
                    if (lookDirection != Vector3.zero)
                    {
                        TargetController.transform.localRotation = Quaternion.Slerp(TargetController.transform.localRotation, Quaternion.LookRotation(lookDirection), rotationSpeed * Time.deltaTime);
                    }


                    moveDirection    = transform.forward * moveSpeed * 100 * Time.deltaTime;
                    moveDirection.y -= gravity * Time.deltaTime;
                    charController.Move(moveDirection * Time.deltaTime);
                }


                //== LEFT MOVEMENT END
            }
            else
            {
                moveDirection    = Vector3.zero;
                moveDirection.y -= gravity * Time.deltaTime;
                charController.Move(moveDirection * Time.deltaTime);
            }

            //****************************************************************************** (2) if there is only input from the right joystick
            if (rightJoystickInput != Vector3.zero)
            {
                if (isRightActive)
                {
                    //== RIGHT MOVEMENT BEGIN
                    //TargetObject.transform.Translate(xMovementLeftJoystick * touchSensitivity, zMovementLeftJoystick * touchSensitivity, 0);
                    float tempAngle = Mathf.Atan2(zMovementRightJoystick, xMovementRightJoystick);
                    xMovementRightJoystick *= Mathf.Abs(Mathf.Cos(tempAngle));
                    zMovementRightJoystick *= Mathf.Abs(Mathf.Sin(tempAngle));

                    // calculate the player's direction based on angle

                    rightJoystickInput  = new Vector3(xMovementRightJoystick, 0, zMovementRightJoystick);
                    rightJoystickInput  = transform.TransformDirection(rightJoystickInput);
                    rightJoystickInput *= moveSpeed;

                    // rotate the player to face the direction of input
                    Vector3 temp = transform.position;
                    temp.x += xMovementRightJoystick;
                    temp.z += zMovementRightJoystick;
                    Vector3 lookDirection = temp - transform.position;
                    if (lookDirection != Vector3.zero)
                    {
                        TargetObject.transform.localRotation = Quaternion.Slerp(TargetObject.transform.localRotation, Quaternion.LookRotation(lookDirection), rotationSpeed * Time.deltaTime);
                    }
                }
            }

            //****************************************************************************** (3) if there is input from both joysticks (Left And Right)
            if (leftJoystickInput != Vector3.zero && rightJoystickInput != Vector3.zero)
            {
                /*******
                *  // calculate the player's direction based on angle
                *  float tempAngleInputRightJoystick = Mathf.Atan2(zMovementRightJoystick, xMovementRightJoystick);
                *  xMovementRightJoystick *= Mathf.Abs(Mathf.Cos(tempAngleInputRightJoystick));
                *  zMovementRightJoystick *= Mathf.Abs(Mathf.Sin(tempAngleInputRightJoystick));
                *
                *  // rotate the player to face the direction of input
                *  Vector3 temp = transform.position;
                *  temp.x += xMovementRightJoystick;
                *  temp.z += zMovementRightJoystick;
                *  Vector3 lookDirection = temp - transform.position;
                *  if (lookDirection != Vector3.zero)
                *  {
                *   TargetController.localRotation = Quaternion.Slerp(TargetController.localRotation, Quaternion.LookRotation(lookDirection) * Quaternion.Euler(0, 45f, 0), rotationSpeed * Time.deltaTime);
                *  }
                *
                *  // calculate the player's direction based on angle
                *  float tempAngleLeftJoystick = Mathf.Atan2(zMovementLeftJoystick, xMovementLeftJoystick);
                *  xMovementLeftJoystick *= Mathf.Abs(Mathf.Cos(tempAngleLeftJoystick));
                *  zMovementLeftJoystick *= Mathf.Abs(Mathf.Sin(tempAngleLeftJoystick));
                *
                *  leftJoystickInput = new Vector3(xMovementLeftJoystick, 0, zMovementLeftJoystick);
                *  leftJoystickInput = transform.TransformDirection(leftJoystickInput);
                *  leftJoystickInput *= moveSpeed;
                *
                *  // move the player
                *  transform.Translate(leftJoystickInput * Time.fixedDeltaTime);
                *******/
            }
        }