@yraiv

Почему тройное нажатие на экран ломает игра?

С двумя нажатиями +- ок всё, но если нажать сразу 3 пальцами на экран, то всё ломается и экран больше не поворачивается, почему так происходит?
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;

public class OrbitCamera : MonoBehaviour
{
    public Transform target; //  (игрок)
    public float distance = 30; // Расстояние от к до и
    public float sensitivityX = 4.0f; // Чувствительность горизонтального вращения
    public float sensitivityY = 1.0f; // Чувствительность вертикального вращения
    public float minYAngle = -80.0f; // Минимальный угол по вертикали
    public float maxYAngle = 80.0f; // Максимальный угол по вертикали

    private float rotationX = 0.0f;
    private float rotationY = 0.0f;

    public bool CanRotateCamera;
    public float smoothSpeed = 5f;
    private Dictionary<int, bool> fingerIdToFirstTouchOverUI = new Dictionary<int, bool>();
    private Dictionary<int, float> fingerIdToRotationX = new Dictionary<int, float>();
    private Dictionary<int, float> fingerIdToRotationY = new Dictionary<int, float>();



    private void Start()
    {
        CanRotateCamera = true;
        if (Application.isMobilePlatform)
        {
            Input.multiTouchEnabled = true;
            sensitivityY = 7f;
            sensitivityX = 7f;
        }
        else
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    private void LateUpdate()
    {
        if (Application.isMobilePlatform)
        {
            HandleMobileInput();
        }
        else
        {
            HandlePCInput();
        }
    }

    private void HandleMobileInput()
    {
        if (CanRotateCamera)
        {
            bool isAnyTouchMoving = false;

            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.GetTouch(i);

                if (touch.phase == TouchPhase.Began)
                {
                    fingerIdToFirstTouchOverUI[touch.fingerId] = IsTouchOverUI(touch.position);
                    if (fingerIdToFirstTouchOverUI[touch.fingerId])
                    {
                        continue;
                    }
                    fingerIdToRotationX[touch.fingerId] = rotationX;
                    fingerIdToRotationY[touch.fingerId] = rotationY;
                }

                if (fingerIdToFirstTouchOverUI[touch.fingerId])
                {
                    continue;
                }

                if (touch.phase == TouchPhase.Moved)
                {
                    fingerIdToRotationY[touch.fingerId] -= touch.deltaPosition.x * sensitivityX * Time.deltaTime;
                    fingerIdToRotationX[touch.fingerId] += touch.deltaPosition.y * sensitivityY * Time.deltaTime;
                    fingerIdToRotationX[touch.fingerId] = Mathf.Clamp(fingerIdToRotationX[touch.fingerId], minYAngle, maxYAngle);
                    isAnyTouchMoving = true;
                }
            }

            if (isAnyTouchMoving)
            {
                var validRotationY = fingerIdToRotationY
                    .Where(pair => !fingerIdToFirstTouchOverUI.ContainsKey(pair.Key) || !fingerIdToFirstTouchOverUI[pair.Key])
                    .Select(pair => pair.Value)
                    .ToList();

                var validRotationX = fingerIdToRotationX
                    .Where(pair => !fingerIdToFirstTouchOverUI.ContainsKey(pair.Key) || !fingerIdToFirstTouchOverUI[pair.Key])
                    .Select(pair => pair.Value)
                    .ToList();

                if (validRotationY.Count > 0 && validRotationX.Count > 0)
                {
                    rotationY = validRotationY.Last();
                    rotationX = validRotationX.Last();
                    transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
                }
            }

            Vector3 newPosition = target.position - transform.forward * distance;
            newPosition.y += 7.0f;
            transform.position = newPosition;
        }
    }
    

    private void HandlePCInput()
    {
        if (CanRotateCamera)
        {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");

            rotationY += mouseX * sensitivityX;
            rotationY = Mathf.Repeat(rotationY, 360);

            rotationX -= mouseY * sensitivityY;
            rotationX = Mathf.Clamp(rotationX, minYAngle, maxYAngle);

            distance -= Input.GetAxis("Mouse ScrollWheel");
            distance = Mathf.Clamp(distance, 8.0f, 30.0f);

            transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);

            Vector3 newPosition = target.position - transform.forward * distance;
            newPosition.y += 7.0f;
            transform.position = newPosition;
        }
    }


  

    
      private bool IsTouchOverUI(Vector2 touchPosition)
       {
           PointerEventData eventData = new PointerEventData(EventSystem.current);
           eventData.position = touchPosition;

           List<RaycastResult> results = new List<RaycastResult>();
           EventSystem.current.RaycastAll(eventData, results);

           return results.Any(result => result.gameObject.layer == LayerMask.NameToLayer("UI"));
       }
}
  • Вопрос задан
  • 43 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы