Search Unity

Question How to use Gyroscope and Finger dragging together?

Discussion in 'Scripting' started by totames, Mar 1, 2023.

  1. totames

    totames

    Joined:
    Jul 30, 2021
    Posts:
    3
    Well hi, I am tyring to make a 3D Environment with a skybox. This is the code of the camera controller. First, I created the variables to use. Then I check if a gyroscope is available in system, if so, this code enables it. In the update method, I checked if a finger is touching the screen. Based on this, _checkTouch changes to true or false. if it is false and _gyro is not null, the application uses the gyroscope. If a finger is touching the screen, then I use transform.eulerAngles to rotate the camera. Gyroscope uses transform.Rotatearound().
    So, the problem arises here. I rotate the camera with my finger, then use gyroscope to rotate it. After I use gyroscope and rotate around, If I rotate the camera by touching with my finger, the rotated camera snaps back to where i left my finger. How can I solve this problem? Is there any better way to implement this?


    Code (CSharp):
    1. public float horizontalSpeed = 10f;
    2. public float verticalSpeed = 10f;
    3.  
    4. public float yaw;
    5. public float pitch;
    6. public Animator animatorObj;
    7.  
    8. private Gyroscope _gyro;
    9.  
    10. private bool _checkTouch = true;
    11. public float x;
    12. public float y;
    13. public float gyroSensitivity = 20f;
    14.  
    15. private Vector3 _previousPosition;
    16. private float _distanceMoved = 0f;
    17.  
    18.  
    19. public Vector3 currentEulerAngles;
    20.  
    21.  
    22. private void Start()
    23. {
    24.     //Check if gyroscope is available
    25.     if (SystemInfo.supportsGyroscope)
    26.     {
    27.         _gyro = Input.gyro;
    28.         _gyro.enabled = true;
    29.     }
    30. }
    31.  
    32. void Update()
    33. {
    34.     //check if the camera is rotated with finger
    35.     if (Input.touchCount > 0)
    36.     {
    37.         _checkTouch = true;
    38.     }
    39.     else
    40.     {
    41.         _checkTouch = false;
    42.     }
    43.  
    44.     //Finger Dragging
    45.     yaw -= horizontalSpeed * Input.GetAxis("Mouse X");
    46.     pitch += verticalSpeed * Input.GetAxis("Mouse Y");
    47.  
    48.     x = Input.gyro.rotationRate.x;
    49.     y = Input.gyro.rotationRate.y;
    50.  
    51.     if (_checkTouch)
    52.     {
    53.         currentEulerAngles = new Vector3(pitch, yaw, 0.0f);
    54.         transform.eulerAngles = currentEulerAngles;
    55.     }
    56.     else if(_gyro != null && _checkTouch == false) // If gyroscope is available
    57.     {
    58.         Debug.Log("gyro");
    59.         transform.RotateAround(transform.position, transform.right, -x * Time.deltaTime * gyroSensitivity);
    60.         transform.RotateAround(transform.position, Vector3.up, -y * Time.deltaTime * gyroSensitivity);
    61.     }
    62.     else
    63.     {
    64.         Debug.Log("no gyro");
    65.     }
    66.  
    67.     //Enables rotation after the animation ends
    68.     if (animatorObj.GetCurrentAnimatorStateInfo(0).IsName("CameraAnim") &&
    69.         animatorObj.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f)
    70.     {
    71.         Debug.Log("Done");
    72.         animatorObj.applyRootMotion = true;
    73.     }
    74. }