Search Unity

Unity Remote notable delay while testing input touch ¿is it normal or is my code or my PC?

Discussion in 'Editor & General Support' started by pleasurehouse, May 11, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Today I installed Unity remote. I've spent the whole afternoon testing it. And I just realized that there is a delay of a second or so when I use the Input.touch ... I mean, when I remove my finger from the screen my character keeps running for a second. I don't know what's going on ... Is this behavior normal? Or is it that my code has a defect?


    Any idea what may be going on?
    Here is my code:



    Code (CSharp):
    1.  
    2. //----------------------------------------------------------------------------
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using UnityEngine.InputSystem.EnhancedTouch;
    8. using Touch = UnityEngine.Touch;
    9. //----------------------------------------------------------------------------
    10. public class CMTouch : MonoBehaviour//2D
    11. {
    12.     public static CMTouch instance;
    13.     //----------------------------------------------------------------------------
    14.     Event e;
    15.     //----------------------------------------------------------------------------
    16.     public Vector2 startPos;
    17.     public Vector2 direction;
    18.     //----------------------------------------------------------------------------
    19.     public bool isBeginning;
    20.     public bool isStationary;
    21.     public bool isMoving;
    22.     public bool isEnding;
    23.     public bool isDown;
    24.     public bool isRight;
    25.     public bool isLeft;
    26.     public bool isUp;
    27.     //----------------------------------------------------------------------------
    28.     private Vector2 mousePosition = new Vector2();
    29.     private delegate void TouchDelegate(Event e);
    30.     private Dictionary<EventType, TouchDelegate> touchEventMap = new Dictionary<EventType, TouchDelegate>();
    31.     //----------------------------------------------------------------------------
    32.     private delegate void DTouchPhaseDelegate(Touch touch);
    33.     private Dictionary<TouchPhase, DTouchPhaseDelegate> touchPhasetMap = new Dictionary<TouchPhase, DTouchPhaseDelegate>();
    34.     //----------------------------------------------------------------------------
    35.     private Dictionary<TouchType, DTouchPhaseDelegate> touchTypeMap = new Dictionary<TouchType, DTouchPhaseDelegate>();
    36.     //----------------------------------------------------------------------------
    37.    .
    38.     int fingerId;                        //The unique index for the touch.  
    39.     TouchPhase phase;          //Describes the phase of the touch.
    40.     TouchType type;               //A value that indicates whether a touch was of Direct, Indirect (or remote), or Stylus type
    41.     //----------------------------------------------------------------------------      
    42.     private void Awake()
    43.     {
    44.         instance = this;
    45.     }
    46.     //----------------------------------------------------------------------------
    47.     void Start()
    48.     {
    49.         cam = Camera.main;    
    50.  
    51.         touchPhasetMap.Add(TouchPhase.Began, Began);
    52.         touchPhasetMap.Add(TouchPhase.Moved, Moved);
    53.         touchPhasetMap.Add(TouchPhase.Stationary, Stationary);
    54.         touchPhasetMap.Add(TouchPhase.Ended, Ended);
    55.         touchPhasetMap.Add(TouchPhase.Canceled, Canceled);
    56.  
    57.         touchTypeMap.Add(TouchType.Direct, Direct);
    58.         touchTypeMap.Add(TouchType.Indirect, Indirect);
    59.         touchTypeMap.Add(TouchType.Stylus, Stylus);
    60.     }
    61.     //----------------------------------------------------------------------------
    62.     public bool IsLeft() => isLeft && isStationary && fingerId==0;
    63.     //----------------------------------------------------------------------------
    64.     public bool IsRight() =>  isRight && isStationary && fingerId==0;
    65.     //----------------------------------------------------------------------------
    66.     public bool IsDown()
    67.     {
    68.         return (direction.y < 0f  && isMoving) ||          
    69.                (isDown && fingerId==1 && tapCount==1);
    70.     }
    71.     //----------------------------------------------------------------------------
    72.     public bool IsUp()
    73.     {
    74.         return (direction.y > 0f && isMoving) ||      
    75.                (isUp && fingerId==1 && tapCount==1);  
    76.     }
    77.     //----------------------------------------------------------------------------
    78.     void SplitScreen(Touch theTouch)
    79.     {    
    80.         if (theTouch.position.x < Screen.width/2)
    81.         {
    82.             isLeft = true;
    83.             isRight = false;
    84.         }
    85.         else if (theTouch.position.x > Screen.width/2)
    86.         {
    87.             isRight = true;
    88.             isLeft = false;
    89.         }
    90.         if (theTouch.position.y < Screen.height/2)
    91.         {
    92.             isDown = true;
    93.             isUp = false;
    94.         }
    95.         else if (theTouch.position.y > Screen.height/2)
    96.         {
    97.             isUp = true;
    98.             isDown = false;
    99.         }
    100.      
    101.         if (theTouch.position.x==0f)
    102.         {
    103.             isLeft = false;
    104.             isRight = false;
    105.         }
    106.     }
    107.     //----------------------------------------------------------------------------
    108.     private void LateUpdate()
    109.     {
    110.         isLeft = false;
    111.         isRight = false;    
    112.         isDown = false;
    113.         isUp = false;
    114.     }
    115.     //----------------------------------------------------------------------------
    116.     void Update()
    117.     {
    118.         e = Event.current;
    119.         //touchEventMap[e.type](e);    
    120.      
    121.         foreach (var theTouch in Input.touches)
    122.         {  
    123.          
    124.             touchPhasetMap[theTouch.phase](theTouch);
    125.             touchTypeMap[theTouch.type](theTouch);
    126.  
    127.             SplitScreen(theTouch);
    128.  
    129.             tapCount = theTouch.tapCount;
    130.             fingerId = theTouch.fingerId;
    131.  
    132.             string message =    " direction-->" + direction +
    133.                                 " IsLeft()-->" + IsLeft() +
    134.                                 " IsRight()-->" + IsRight() +
    135.                                 " IsUp()-->" + IsUp() +
    136.                                 " IsDown()-->" + IsDown() +
    137.                                 " touch.position->" + theTouch.position +
    138.                                 " fingerId->" + fingerId +
    139.                                 " tapCount->" + tapCount;  
    140.      
    141.      
    142.             Debug.Log(this +"  "+ message );
    143.          
    144.         }  
    145.    
    146.     }
    147.  
    148.     //----------------------------------------------------------------------------
    149.    
    150.     void TouchDown(Event e)   {   }
    151.  
    152.     void TouchUp(Event e)  {   }
    153.  
    154.     void TouchMove(Event e)  {  }
    155.  
    156.     void TouchEnter(Event e)  {  }
    157.  
    158.     void TouchLeave(Event e)  {  }
    159.  
    160.     void TouchStationary(Event e) { }  
    161.     //----------------------------------------------------------------------------
    162.     void Began(Touch touch)//A finger touched the screen.
    163.     {
    164.         startPos = touch.position;
    165.         isBeginning = true;
    166.         isEnding = false;
    167.     }
    168.     //----------------------------------------------------------------------------
    169.     void Moved(Touch touch)//A finger moved on the screen.
    170.     {
    171.         direction = touch.position - startPos;
    172.  
    173.         if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
    174.         {
    175.             direction = new Vector2(direction.x, 0f);
    176.         }
    177.         else
    178.         {
    179.             direction = new Vector2(0f, direction.y);
    180.         }      
    181.      
    182.         isMoving = true;
    183.         isBeginning = false;
    184.     }
    185.  
    186.     //----------------------------------------------------------------------------
    187.     void Stationary(Touch touch)//    A finger is touching the screen but hasn't moved.
    188.     {      
    189.         isStationary = true;
    190.         isBeginning = false;
    191.         isMoving = false;
    192.         tapCount = -1;
    193.     }
    194.     //----------------------------------------------------------------------------
    195.     void Ended(Touch touch)//A finger was lifted from the screen. This is the final phase of a touch.
    196.     {
    197.         isEnding = true;
    198.  
    199.         isBeginning = false;
    200.         isStationary = false;
    201.         isMoving = false;
    202.  
    203.         isUp = false;
    204.         isDown = false;
    205.         isRight=false;
    206.         isLeft=false;
    207.      
    208.         tapCount = -1;
    209.         fingerId = -1;
    210.     }
    211.     //----------------------------------------------------------------------------
    212.     void Canceled(Touch touch)//The system cancelled tracking for the touch.
    213.     {
    214.         isEnding = false;
    215.  
    216.         isBeginning = false;
    217.         isStationary = false;
    218.         isMoving = false;
    219.  
    220.         isUp = false;
    221.         isDown = false;
    222.         isRight=false;
    223.         isLeft=false;
    224.      
    225.         tapCount = -1;
    226.         fingerId = -1;
    227.     }
    228.     //----------------------------------------------------------------------------
    229.     void Direct(Touch touch)  {     }
    230.     void Indirect(Touch touch)  { }
    231.     void Stylus(Touch touch)  { }
    232. }
    233. //---------------------------------------------------------------------------
    234.  
    235. -