Search Unity

Touch Position Shifts across the screen for 1 frame, triggering "end" phase as well… Help???

Discussion in 'Scripting' started by infinitypbr, Jul 26, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Touch controls a joystick to move the character. every so often -- and not sure exactly why, unfortunately, the joystick "Skips" and loses the connection, only to re-gain it the next frame. However, this means the joystick goes away and then reappears under the finger -- if you were moving, you're now stopped. This only happens every once in a very long while, usually repeatable if I don't move on from the spot, but won't show up for hours of gameplay at times. Frame rate is over 30fps on iOS.

    I've been testing, and in the Xcode log I have the following: [FrameCount] #FingerID (Finger.Pos)

    [FRAMES BEFORE THIS ARE SIMILAR]
    [4949] #0 (215.0, 80.0)
    [4950] End joystickFingerID: 0
    Pos: (961.0, 44.0)|(961.0, 44.0)
    [4950] Check new position: (229.0, 69.0)|(961.0, 44.0)|732.4268

    [4951] #1 (229.0, 69.0)

    [FRAMES AFTER THIS ARE SIMILAR]

    If I can explain: Frame 4949, finger 0 (the joystick finger) is at 215,80. The next frame, 4950, in the "phase.ended" section the position of finger 0 is 961,44 -- clear on the other side of the screen. Keep in mind the finger didn't actually move.

    My script currently waits for 10 frames before actually destroying the joystick, so also in frame 4950, for Finger #1, the joystick finger is re-assigned. Frame 4951, now showing #1, shows pos 229,69, very close to the pos in 4949.

    Basically for one frame the system somehow reports that the touch position is not where it is, also reporting that it has ended the touch phase when it hasn't, only to pick up the SAME touch as a new ID during the next pass of the code (which cycles through all touches).

    I'm attaching my code below. Any help would be appreciated, as this is one annoying bug that I've been unable to pinpoint.

    Note: the bit at the end (line 221) with "Destroy (JoystickObject);" is usually included at line 168 in the ended phase, but since things weren't working I added that to test with.

    Code (csharp):
    1.  
    2. functionCheckTouch(){
    3. for (vari = 0; i < Input.touchCount; i++)
    4.  {
    5. if (Input.GetTouch(i).phase == TouchPhase.Began)
    6.  {
    7. //print ("Finger #" + i + " Began " + Input.GetTouch(i).fingerId);
    8. vartouchHitBegan : RaycastHit;
    9. vartouchRayBegan : Ray = guiCamera.ScreenPointToRay (Input.GetTouch(i).position);
    10. if (Physics.Raycast (touchRayBegan, touchHitBegan, 10000))
    11.  {
    12. if (touchHitBegan.collider.gameObject.name == "MiniMapPlus" || touchHitBegan.collider.gameObject.name == "MiniMapMinus")
    13.  {
    14. touchHitBegan.collider.gameObject.GetComponent(minimapZoom).ZoomCamera();
    15.  }
    16. elseif (touchHitBegan.collider.gameObject.tag == "guiButton")
    17.  {
    18. vartouchDownName = touchHitBegan.collider.gameObject.name;
    19. if (touchDownName == "CameraCircleRight")
    20.  {
    21. cameraRightFingerID = Input.GetTouch(i).fingerId;
    22. mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
    23.  }
    24. elseif (touchDownName == "CameraCircleLeft")
    25.  {
    26. cameraLeftFingerID = Input.GetTouch(i).fingerId;
    27. mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
    28.  }
    29. elseif (touchDownName == "CameraCircleDown")
    30.  {
    31. cameraDownFingerID = Input.GetTouch(i).fingerId;
    32. mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
    33.  }
    34. elseif (touchDownName == "CameraCircleUp")
    35.  {
    36. cameraUpFingerID = Input.GetTouch(i).fingerId;
    37. mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
    38.  }
    39. elseif (touchHitBegan.collider.gameObject.name == "MiniMapSpot")
    40.  {
    41. touchHitBegan.collider.gameObject.GetComponent(toggleMap).GotHit();
    42.  }
    43. elseif (touchHitBegan.collider.gameObject.name == "GUIButton1")
    44.  {
    45. if (GetComponent(joystick).isNotAttacking)
    46.  {
    47. attackFingerID = Input.GetTouch(i).fingerId;
    48. GetComponent(joystick).isNotAttacking = false;
    49. playerObject.GetComponent(playerScript).ButtonPressed(1);
    50.  }
    51.  }
    52. elseif (touchHitBegan.collider.gameObject.name == "GUIButton2")
    53.  {
    54. button2FingerID = Input.GetTouch(i).fingerId;
    55. playerObject.GetComponent(playerScript).ButtonPressed(2);
    56.  }
    57. elseif (touchHitBegan.collider.gameObject.name == "GUIButton3")
    58.  {
    59. button3FingerID = Input.GetTouch(i).fingerId;
    60. playerObject.GetComponent(playerScript).ButtonPressed(3);
    61.  }
    62. elseif (touchHitBegan.collider.gameObject.name == "GUIButton4")
    63.  {
    64. button4FingerID = Input.GetTouch(i).fingerId;
    65. playerObject.GetComponent(playerScript).ButtonPressed(4);
    66.  }
    67. elseif (touchHitBegan.collider.gameObject.name == "GUIButton5")
    68.  {
    69. button5FingerID = Input.GetTouch(i).fingerId;
    70. playerObject.GetComponent(playerScript).RightTriggerAction();
    71.  }
    72. elseif (touchHitBegan.collider.gameObject.name == "GUIButtonMenuOpen")
    73. GetComponent(joystick).OpenMenu();
    74.  }
    75.  }
    76. elseif (!controllerUp)
    77.  {
    78. varuseNew = true;
    79. if (endJoystick != 0)
    80.  {
    81. varposDif = Vector2.Distance(Input.GetTouch(i).position, endJoystickPos);
    82. print ("[" + Time.frameCount + "] Checknewposition: " + Input.GetTouch(i).position + "|" + endJoystickPos + "|" + posDif);
    83. //if (Input.GetTouch(i).position == endJoystickPos)
    84. //if (posDif <= 10)
    85. //{
    86. //print ("[" + Time.frameCount + "] DetectedSamePosition");
    87. controllerUp = true;
    88. joystickFingerID = Input.GetTouch(i).fingerId;
    89. useNew = false;
    90. endJoystick = 0;
    91. JoystickObject.renderer.enabled = true;
    92. //}
    93.  }
    94. if (useNew)
    95.  {
    96. joystickFingerID = Input.GetTouch(i).fingerId;
    97. controllerUp = true;
    98. varscreenPosLTouch = Input.GetTouch(i).position;
    99. varworldPosLTouch = guiCamera.ScreenToWorldPoint(screenPosLTouch);
    100. worldPosLTouch.z = 120.5;
    101. if (JoystickObject)
    102. Destroy (JoystickObject);
    103. JoystickObject = Instantiate(JoystickController, worldPosLTouch, Quaternion.identity);
    104. if (largeScreen)
    105. JoystickObject.transform.localScale = JoystickObject.transform.localScale / 2;
    106. JoystickObject.transform.parent = transform;
    107. joystickCenterX = JoystickObject.transform.position.x;
    108. joystickCenterY = JoystickObject.transform.position.y;
    109. print ("[" + Time.frameCount + "] StartjoystickFingerID: " + joystickFingerID);
    110.  }
    111.  }
    112.  }
    113. elseif (Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)
    114.  {
    115. varfoundStationary = false;
    116. if (cameraRightFingerID == Input.GetTouch(i).fingerId)
    117. mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
    118. elseif (cameraLeftFingerID == Input.GetTouch(i).fingerId)
    119. mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
    120. elseif (cameraDownFingerID == Input.GetTouch(i).fingerId)
    121. mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
    122. elseif (cameraUpFingerID == Input.GetTouch(i).fingerId)
    123. mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
    124. elseif (joystickFingerID == Input.GetTouch(i).fingerId)
    125.  {
    126. print ("[" + Time.frameCount + "] #" + joystickFingerID + "" + Input.GetTouch(i).position);
    127. //print ("Move/Stationary: " + joystickFingerID + " / " + Input.GetTouch(i).fingerId);
    128. foundStationary = true;
    129. varscreenPos2Touch = Input.GetTouch(i).position;
    130. varworldPos2Touch = guiCamera.ScreenToWorldPoint(screenPos2Touch);
    131. joystickCenterX = JoystickObject.transform.position.x;
    132. joystickCenterY = JoystickObject.transform.position.y;
    133. currentTouchX = worldPos2Touch.x;
    134. currentTouchY = worldPos2Touch.y;
    135. varcenterTouch = Vector2(joystickCenterX, joystickCenterY);
    136. varcurrentTouch = Vector2(currentTouchX, currentTouchY);
    137. vardstTouch = Vector2.Distance(centerTouch, currentTouch);
    138. if (largeScreen)
    139. dstTouch = dstTouch * 2;
    140. varhorizontalTouch = currentTouchX - joystickCenterX;
    141. varverticalTouch = currentTouchY - joystickCenterY;
    142. moveX = currentTouchX - joystickCenterX;
    143. moveY = currentTouchY - joystickCenterY;
    144. varangleTouch : float = Mathf.Atan2(moveX, moveY) * Mathf.Rad2Deg;
    145. if (!playerObject.GetComponent(playerScript).isDead)
    146.  {
    147. if (GetComponent(joystick).isNotAttacking)
    148.  {
    149. playerObject.GetComponent(playerScript).angle = angleTouch + mainCamera.GetComponent(SmoothFollow).angleOffsetX;
    150. playerObject.GetComponent(playerScript).moveSpeed = dstTouch;
    151.  }
    152.  }
    153.  }
    154.  }
    155. elseif (Input.GetTouch(i).phase == TouchPhase.Ended)
    156.  {
    157. //print ("Finger #" + Input.GetTouch(i).fingerId + " Ended");
    158. if (cameraRightFingerID == Input.GetTouch(i).fingerId)
    159. cameraRightFingerID = 9;
    160. elseif (cameraLeftFingerID == Input.GetTouch(i).fingerId)
    161. cameraLeftFingerID = 9;
    162. elseif (cameraUpFingerID == Input.GetTouch(i).fingerId)
    163. cameraUpFingerID = 9;
    164. elseif (cameraDownFingerID == Input.GetTouch(i).fingerId)
    165. cameraDownFingerID = 9;
    166. elseif (Input.GetTouch(i).fingerId == joystickFingerID)
    167.  {
    168. print ("[" + Time.frameCount + "] EndjoystickFingerID: " + joystickFingerID);
    169.  
    170. endJoystick = 5;
    171. endJoystickPos = Input.GetTouch(i).position;
    172. print ("Pos: " + Input.GetTouch(i).position + "|" + endJoystickPos);
    173. controllerUp = false;
    174. JoystickObject.renderer.enabled = false;
    175.  }
    176. elseif (Input.GetTouch(i).fingerId == button2FingerID)
    177.  {
    178. button2FingerID = 9;
    179. playerObject.GetComponent(playerScript).ButtonReleased(2);
    180.  }
    181. elseif (Input.GetTouch(i).fingerId == attackFingerID)
    182.  {
    183. attackFingerID = 9;
    184. playerObject.GetComponent(playerScript).ButtonReleased(1);
    185.  }
    186. elseif (Input.GetTouch(i).fingerId == button3FingerID)
    187.  {
    188. button3FingerID = 9;
    189. playerObject.GetComponent(playerScript).ButtonReleased(3);
    190.  }
    191. elseif (Input.GetTouch(i).fingerId == button4FingerID)
    192.  {
    193. button4FingerID = 9;
    194. playerObject.GetComponent(playerScript).ButtonReleased(4);
    195.  }
    196. elseif (Input.GetTouch(i).fingerId == button5FingerID)
    197.  {
    198. button5FingerID = 9;
    199. playerObject.GetComponent(playerScript).ButtonReleased(5);
    200.  }
    201.  }
    202.  }
    203.  
    204. if (endJoystick != 0)
    205.  {
    206. if (endJoystick == 1)
    207.  {
    208. joystickFingerID = 9;
    209. controllerUp = false;
    210. playerObject.GetComponent(playerScript).moveSpeed = 0;
    211. Destroy (JoystickObject);
    212. endJoystick = 0;
    213.  }
    214. elseif (endJoystick >= 2)
    215.  {
    216. //print ("EndJoystick2");
    217. endJoystick -= 1;
    218.  }
    219.  }
    220. }
    221.  
     
  2. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Here's more log output. The finger didn't move from the screen until the end.
    Code (csharp):
    1.  
    2. [7919] #0 (412.0, 151.0)
    3.  
    4. [7920] #0 (412.0, 151.0)
    5.  
    6. [7921] End joystickFingerID: 0  Pos: (991.0, 57.0)|(991.0, 57.0)
    7.  
    8. [7921] Check new position: (412.0, 158.0)|(991.0, 57.0)|587.7432
    9.  
    10. [7922] #2 (412.0, 158.0)
    11.  
    12. [7923] #2 (412.0, 158.0)
    13.  
    14. [7924] #2 (412.0, 158.0)
    15.  
    16. [7925] #2 (412.0, 158.0)
    17.  
    18. [7926] #2 (412.0, 158.0)
    19.  
    20. [7927] #2 (412.0, 158.0)
    21.  
    22. [7928] #2 (412.0, 158.0)
    23.  
    24. [7929] #2 (412.0, 158.0)
    25.  
    26. [7930] #2 (412.0, 158.0)
    27.  
    28. [7931] #2 (412.0, 158.0)
    29.  
    30. [7932] #2 (412.0, 158.0)
    31.  
    32. [7933] #2 (412.0, 158.0)
    33.  
    34. [7934] #2 (412.0, 158.0)
    35.  
    36. [7935] #2 (412.0, 158.0)
    37.  
    38. [7936] #2 (412.0, 158.0)
    39.  
    40. [7937] #2 (412.0, 158.0)
    41.  
    42. [7938] #2 (412.0, 158.0)
    43.  
    44. [7939] #2 (412.0, 158.0)
    45.  
    46. [7940] #2 (412.0, 158.0)
    47.  
    48. [7941] #2 (412.0, 158.0)
    49.  
    50. [7942] #2 (412.0, 158.0)
    51.  
    52. [7943] #2 (412.0, 158.0)
    53.  
    54. [7944] End joystickFingerID: 2  Pos: (409.0, 157.0)|(409.0, 157.0)
    55.  
    56. [7959] Start joystickFingerID: 0
    57.  
    58. [7960] End joystickFingerID: 0  Pos: (310.0, 101.0)|(310.0, 101.0)
    59.  
    60. [7961] Check new position: (310.0, 102.0)|(310.0, 101.0)|1.000023
    61.  
    62. [7962] #0 (310.0, 102.0)
    63.  
    64. [7963] #0 (310.0, 102.0)
    65.  
    66. [7964] End joystickFingerID: 0  Pos: (310.0, 102.0)|(310.0, 102.0)
    67.  
    68. [7972] Start joystickFingerID: 0
    69.  
    70. [7973] #0 (302.0, 50.0)
    71.  
    72. [7974] #0 (302.0, 50.0)
    73.  
    74. [7975] #0 (302.0, 50.0)
    75.  
    76. [7976] #0 (302.0, 50.0)
    77.  
    78. [7977] #0 (302.0, 50.0)
    79.  
    80. [7978] #0 (302.0, 50.0)
    81.  
    82. [7979] #0 (302.0, 50.0)
    83.  
    84. [7980] #0 (302.0, 50.0)
    85.  
    86. [7981] #0 (302.0, 50.0)
    87.  
    88. [7982] #0 (302.0, 50.0)
    89.  
    90. [7983] #0 (302.0, 50.0)
    91.  
    92. [7984] #0 (324.0, 36.0)
    93.  
    94. [7985] #0 (310.0, 39.0)
    95.  
    96. [7986] End joystickFingerID: 0  Pos: (306.0, 43.0)|(306.0, 43.0)
     
  3. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    UPDATE: I went to show my friend the problem, since it kept happening in this one spot for the 8 hours i was testing, and it didn't happen. He suggested that maybe it's a problem that only happens when the phone heats up, since this game uses the iPhone 5s to it's maximum capacity in graphics and CPU. Sure enough, I think that's the cause.