Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

turn with touch

Discussion in 'Scripting' started by vova1227, Apr 16, 2021.

  1. vova1227

    vova1227

    Joined:
    Feb 17, 2021
    Posts:
    45
    when I swipe to turn and press any button with the second finger, then this press is counted in the rotation. Help please, I do not know how to separate 2 different clicks. I tried to do this using the ONDRAG method in PointerID But then I don't know how to stop the camera rotation without lifting my finger from the touchscreen.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4. using System;
    5.  
    6. public class TouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    7. {
    8.     public Vector2 TouchDist;
    9.     public Vector2 PointerOld;
    10.     int PointerId;
    11.     public bool Pressed;
    12.  
    13.     public void OnPointerDown(PointerEventData eventData)
    14.     {
    15.         Pressed = true;
    16.         PointerId = eventData.pointerId;
    17.         PointerOld = eventData.position;
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         if (Pressed)
    23.         {
    24.             if (PointerId > 0 && PointerId < Input.touches.Length)
    25.             {
    26.                 TouchDist = Input.touches[PointerId].position - PointerOld;
    27.                 PointerOld = Input.touches[PointerId].position;
    28.             }
    29.             else
    30.             {
    31.                 TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;
    32.                 PointerOld = Input.mousePosition;
    33.             }
    34.         }
    35.         else
    36.         {
    37.             TouchDist = new Vector2();
    38.         }
    39.     }
    40.  
    41.     public void OnPointerUp(PointerEventData eventData)
    42.     {
    43.         Pressed = false;
    44.     }
    45. }
    46.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    You don't say what you're doing exactly but you're welcome to look at some multi-touch multi-axis input examples in my proximity_buttons package. For instance, the
    DemoCameraRotatedControls
    scene has movement on one side of the phone screen, camera on the other.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  3. vova1227

    vova1227

    Joined:
    Feb 17, 2021
    Posts:
    45
    I calculate how far the finger is moved from the place of the first touch, and if it stops moving, then I reset the variable TouchDist(She is responsible for turning the player in another script. )I want to be able to press the shot button without lifting the first finger off the screen. That is, simultaneously turn and shoot. But then, when turning, the second press on the display is taken into account. It seems like a trifle, but mobile control is already uncomfortable, I want to make it as convenient as possible
     
  4. vova1227

    vova1227

    Joined:
    Feb 17, 2021
    Posts:
    45
    problem solved, sign was missing, =

    PointerId >= 0 && PointerId