// Update is called once per frame
        void Update()
        {
            if (isEnabled)
            {
                leftJoystickInput = leftJoystick.GetInputDirection();

                for (int i = 0; i < AnimationState3D.Length; i++)
                {
                    if (leftJoystickInput != Vector3.zero)
                    {
                        if (AnimationState3D[i].ParameterType == CParameterType.Float)
                        {
                            float dummyvalue = float.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetFloat(AnimationState3D[i].ParameterName, dummyvalue);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Int)
                        {
                            int dummyvalue = int.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetInteger(AnimationState3D[i].ParameterName, dummyvalue);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Bool)
                        {
                            bool dummyvalue = bool.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetBool(AnimationState3D[i].ParameterName, dummyvalue);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Trigger)
                        {
                            TargetAnimator.SetTrigger(AnimationState3D[i].ParameterName);
                        }
                    }
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (isEnabled)
            {
                leftJoystickInput = leftJoystick.GetInputDirection();

                for (int i = 0; i < AnimationState3D.Length; i++)
                {
                    if (leftJoystickInput != Vector3.zero)
                    {
                        if (AnimationState3D[i].ParameterType == CParameterType.Float)
                        {
                            float dummyvalue = float.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetFloat(AnimationState3D[i].ParameterName, dummyvalue);
                            if (AnimationState3D[i].ForceAnimation)
                            {
                                if (!TargetAnimator.GetCurrentAnimatorStateInfo(0).IsName(AnimationState3D[i].StateNext))
                                {
                                    TargetAnimator.Play(AnimationState3D[i].StateNext);
                                }
                            }
                            ExecuteSound(i);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Int)
                        {
                            int dummyvalue = int.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetInteger(AnimationState3D[i].ParameterName, dummyvalue);
                            if (AnimationState3D[i].ForceAnimation)
                            {
                                if (!TargetAnimator.GetCurrentAnimatorStateInfo(0).IsName(AnimationState3D[i].StateNext))
                                {
                                    TargetAnimator.Play(AnimationState3D[i].StateNext);
                                }
                            }
                            ExecuteSound(i);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Bool)
                        {
                            bool dummyvalue = bool.Parse(AnimationState3D[i].PositiveValue);
                            TargetAnimator.SetBool(AnimationState3D[i].ParameterName, dummyvalue);
                            if (AnimationState3D[i].ForceAnimation)
                            {
                                if (!TargetAnimator.GetCurrentAnimatorStateInfo(0).IsName(AnimationState3D[i].StateNext))
                                {
                                    TargetAnimator.Play(AnimationState3D[i].StateNext);
                                }
                            }
                            ExecuteSound(i);
                        }
                        if (AnimationState3D[i].ParameterType == CParameterType.Trigger)
                        {
                            TargetAnimator.SetTrigger(AnimationState3D[i].ParameterName);
                            if (AnimationState3D[i].ForceAnimation)
                            {
                                if (!TargetAnimator.GetCurrentAnimatorStateInfo(0).IsName(AnimationState3D[i].StateNext))
                                {
                                    TargetAnimator.Play(AnimationState3D[i].StateNext);
                                }
                            }
                            ExecuteSound(i);
                        }
                    }
                }
            }
        }
        void FixedUpdate()
        {
            // get input from both joysticks
            if (isFlipDirection)
            {
                leftJoystickInput = -1 * leftJoystick.GetInputDirection();
            }
            else
            {
                leftJoystickInput = leftJoystick.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)
            {
                //== 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);
                }

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

                //== LEFT MOVEMENT END
            }

            //****************************************************************************** (2) if there is only input from the right joystick
            if (leftJoystickInput == Vector3.zero && rightJoystickInput != Vector3.zero)
            {
                //TargetObject.transform.Rotate(-zMovementRightJoystick * rotateSensitivity, xMovementRightJoystick * rotateSensitivity, 0);

                /***
                 * if (Input.touchCount > 0)
                 * {
                 *  if (Input.touchCount > 0)
                 *  {
                 *      Vector3 touchPos = Input.GetTouch(0).position;
                 *      Vector3 newtouchPos = Camera3D.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, Camera3D.GetComponent<Camera>().nearClipPlane));
                 *
                 *      Vector3 direction = newtouchPos - TargetObject.transform.position;
                 *
                 *      Quaternion toRotation = Quaternion.LookRotation(TargetObject.transform.forward, direction);
                 *      TargetObject.transform.rotation = Quaternion.Lerp(TargetObject.transform.rotation, toRotation, 10f * Time.deltaTime);
                 *
                 *      //TargetObject.transform.LookAt(newtouchPos);
                 *  }
                 * }
                 ***/



                /****calculate the player's direction based on angle
                 * float tempAngle = Mathf.Atan2(zMovementRightJoystick, xMovementRightJoystick);
                 * xMovementRightJoystick *= Mathf.Abs(Mathf.Cos(tempAngle));
                 * zMovementRightJoystick *= Mathf.Abs(Mathf.Sin(tempAngle));
                 *
                 * // rotate the player to face the direction of input
                 * Vector3 temp = transform.position;
                 * temp.x += xMovementRightJoystick;
                 * temp.y += yMovementRightJoystick;
                 * temp.z += zMovementRightJoystick;
                 * Vector3 lookDirection = temp - transform.position;
                 * if (lookDirection != Vector3.zero)
                 * {
                 *  TargetObject.localRotation = Quaternion.Slerp(TargetObject.localRotation, Quaternion.LookRotation(lookDirection) * Quaternion.Euler(0, 45f, 0), 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);
                *******/
            }
        }
Exemplo n.º 4
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);
                *******/
            }
        }