Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why is the orbital camera twitching from the phone?

Discussion in 'Scripting' started by Urtay, Nov 29, 2023.

  1. Urtay

    Urtay

    Joined:
    Jan 19, 2022
    Posts:
    4
    I'm creating a game for webgl, the goal is to create an orbital camera for a phone, I managed to do it for computers, but I can't do it for a phone. I've been trying to do this for a few days now, and I'm still not getting it. I need your help. At the moment I have written a script, and there is 1 error in it that does not allow me to use it. Where I have the error: step 1. I press my finger on any element of the user interface and keep my finger pressed on it. Step 2. I press the other finger in the free zone and swipe my finger across the screen while still holding it. Step 3. I raise the first finger that was above the user interface and still hold the finger that was in the free zone. Stage 4. The camera jerks sharply 1 time. How to avoid this camera jump?

    my script


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using TMPro;
    5. using UnityEngine;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class OrbitCamera : MonoBehaviour
    9. {
    10.  
    11.     public Transform target;
    12.     public float distance = 30;
    13.     public float sensitivityX = 4.0f;
    14.     public float sensitivityY = 1.0f;
    15.     public float minYAngle = -80.0f;
    16.     public float maxYAngle = 80.0f;
    17.  
    18.     private float rotationX = 0.0f;
    19.     private float rotationY = 0.0f;
    20.  
    21.     public bool CanRotateCamera;
    22.     public float smoothSpeed = 5f;
    23.     private Dictionary<int, bool> fingerIdToFirstTouchOverUI = new Dictionary<int, bool>();
    24.  
    25.     private Touch _touchcurrent;
    26.  
    27.     private void Start()
    28.     {
    29.         CanRotateCamera = true;
    30.         if (Application.isMobilePlatform)
    31.         {
    32.             Input.multiTouchEnabled = true;
    33.             sensitivityY = 7f;
    34.             sensitivityX = 7f;
    35.         }
    36.         else
    37.         {
    38.             Cursor.lockState = CursorLockMode.Locked;
    39.             Cursor.visible = false;
    40.         }
    41.     }
    42.  
    43.     private void LateUpdate()
    44.     {
    45.         if (Application.isMobilePlatform)
    46.         {
    47.             HandleMobileInput();
    48.         }
    49.         else
    50.         {
    51.             HandlePCInput();
    52.         }
    53.     }
    54.  
    55.     private void HandleMobileInput()
    56.     {
    57.         if (CanRotateCamera)
    58.         {
    59.             bool isAnyTouchMoving = false;
    60.  
    61.             for (int i = 0; i < Input.touchCount; i++)
    62.             {
    63.                 Touch touch = Input.GetTouch(i);
    64.  
    65.                 if (touch.phase == TouchPhase.Began)
    66.                 {
    67.                     fingerIdToFirstTouchOverUI[touch.fingerId] = IsTouchOverUI(touch.position);
    68.                     if (fingerIdToFirstTouchOverUI[touch.fingerId])
    69.                     {
    70.                         continue;
    71.                     }
    72.                 }
    73.  
    74.                 if (touch.phase == TouchPhase.Moved && !fingerIdToFirstTouchOverUI[touch.fingerId])
    75.                 {
    76.                     rotationY -= touch.deltaPosition.x * sensitivityX * Time.deltaTime;
    77.                     rotationX += touch.deltaPosition.y * sensitivityY * Time.deltaTime;
    78.                     rotationX = Mathf.Clamp(rotationX, minYAngle, maxYAngle);
    79.                     isAnyTouchMoving = true;
    80.                 }
    81.             }
    82.  
    83.             if (isAnyTouchMoving)
    84.             {
    85.                 transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
    86.             }
    87.  
    88.             Vector3 newPosition = target.position - transform.forward * distance;
    89.             newPosition.y += 7.0f;
    90.             transform.position = newPosition;
    91.         }
    92.     }
    93.  
    94.  
    95.     private void HandlePCInput()
    96.     {
    97.         if (CanRotateCamera)
    98.         {
    99.             float mouseX = Input.GetAxis("Mouse X");
    100.             float mouseY = Input.GetAxis("Mouse Y");
    101.  
    102.             rotationY += mouseX * sensitivityX;
    103.             rotationY = Mathf.Repeat(rotationY, 360);
    104.  
    105.             rotationX -= mouseY * sensitivityY;
    106.             rotationX = Mathf.Clamp(rotationX, minYAngle, maxYAngle);
    107.  
    108.             distance -= Input.GetAxis("Mouse ScrollWheel");
    109.             distance = Mathf.Clamp(distance, 8.0f, 30.0f);
    110.  
    111.             transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
    112.  
    113.             Vector3 newPosition = target.position - transform.forward * distance;
    114.             newPosition.y += 7.0f;
    115.             transform.position = newPosition;
    116.         }
    117.     }
    118.  
    119.  
    120.  
    121.  
    122.  
    123.     private bool IsTouchOverUI(Vector2 touchPosition)
    124.     {
    125.         PointerEventData eventData = new PointerEventData(EventSystem.current);
    126.         eventData.position = touchPosition;
    127.  
    128.         List<RaycastResult> results = new List<RaycastResult>();
    129.         EventSystem.current.RaycastAll(eventData, results);
    130.  
    131.         return results.Any(result => result.gameObject.layer == LayerMask.NameToLayer("UI"));
    132.     }
    133. }

    I have tried a large number of different options, but for many days in a row I have not been able to do anything, I am disappointed and go around in circles, I really need the help of people who understand how to implement it. I just want an orbital camera that will not interfere with the UI and it will all work in webgl from the phone.
     
  2. Urtay

    Urtay

    Joined:
    Jan 19, 2022
    Posts:
    4
    I've been searching for a solution for over a week now. I hope someone can help us
     
  3. Urtay

    Urtay

    Joined:
    Jan 19, 2022
    Posts:
    4
    Code (CSharp):
    1. if (touch.phase == TouchPhase.Moved && !fingerIdToFirstTouchOverUI[touch.fingerId])
    2. {
    3.     // check for large delta and skip this time
    4.     // experiment with the value so that it doesn't miss certain fast swipes
    5.     if (touch.deltaPosition.magnitude > 100)
    6.     {
    7.         continue; // skip to next for loop
    8.     }
    9.  
    10.     // delta looks good, handle normally
    11.     rotationY -= touch.deltaPosition.x * sensitivityX * Time.deltaTime;
    12.     rotationX += touch.deltaPosition.y * sensitivityY * Time.deltaTime;
    13.     rotationX = Mathf.Clamp(rotationX, minYAngle, maxYAngle);
    14.     isAnyTouchMoving = true;
    15. }