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.
functionCheckTouch(){
for (vari = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
//print ("Finger #" + i + " Began " + Input.GetTouch(i).fingerId);
vartouchHitBegan : RaycastHit;
vartouchRayBegan : Ray = guiCamera.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (touchRayBegan, touchHitBegan, 10000))
{
if (touchHitBegan.collider.gameObject.name == "MiniMapPlus" || touchHitBegan.collider.gameObject.name == "MiniMapMinus")
{
touchHitBegan.collider.gameObject.GetComponent(minimapZoom).ZoomCamera();
}
elseif (touchHitBegan.collider.gameObject.tag == "guiButton")
{
vartouchDownName = touchHitBegan.collider.gameObject.name;
if (touchDownName == "CameraCircleRight")
{
cameraRightFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
}
elseif (touchDownName == "CameraCircleLeft")
{
cameraLeftFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
}
elseif (touchDownName == "CameraCircleDown")
{
cameraDownFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
}
elseif (touchDownName == "CameraCircleUp")
{
cameraUpFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
}
elseif (touchHitBegan.collider.gameObject.name == "MiniMapSpot")
{
touchHitBegan.collider.gameObject.GetComponent(toggleMap).GotHit();
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButton1")
{
if (GetComponent(joystick).isNotAttacking)
{
attackFingerID = Input.GetTouch(i).fingerId;
GetComponent(joystick).isNotAttacking = false;
playerObject.GetComponent(playerScript).ButtonPressed(1);
}
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButton2")
{
button2FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(2);
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButton3")
{
button3FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(3);
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButton4")
{
button4FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(4);
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButton5")
{
button5FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).RightTriggerAction();
}
elseif (touchHitBegan.collider.gameObject.name == "GUIButtonMenuOpen")
GetComponent(joystick).OpenMenu();
}
}
elseif (!controllerUp)
{
varuseNew = true;
if (endJoystick != 0)
{
varposDif = Vector2.Distance(Input.GetTouch(i).position, endJoystickPos);
print ("[" + Time.frameCount + "] Checknewposition: " + Input.GetTouch(i).position + "|" + endJoystickPos + "|" + posDif);
//if (Input.GetTouch(i).position == endJoystickPos)
//if (posDif <= 10)
//{
//print ("[" + Time.frameCount + "] DetectedSamePosition");
controllerUp = true;
joystickFingerID = Input.GetTouch(i).fingerId;
useNew = false;
endJoystick = 0;
JoystickObject.renderer.enabled = true;
//}
}
if (useNew)
{
joystickFingerID = Input.GetTouch(i).fingerId;
controllerUp = true;
varscreenPosLTouch = Input.GetTouch(i).position;
varworldPosLTouch = guiCamera.ScreenToWorldPoint(screenPosLTouch);
worldPosLTouch.z = 120.5;
if (JoystickObject)
Destroy (JoystickObject);
JoystickObject = Instantiate(JoystickController, worldPosLTouch, Quaternion.identity);
if (largeScreen)
JoystickObject.transform.localScale = JoystickObject.transform.localScale / 2;
JoystickObject.transform.parent = transform;
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
print ("[" + Time.frameCount + "] StartjoystickFingerID: " + joystickFingerID);
}
}
}
elseif (Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)
{
varfoundStationary = false;
if (cameraRightFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
elseif (cameraLeftFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
elseif (cameraDownFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
elseif (cameraUpFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
elseif (joystickFingerID == Input.GetTouch(i).fingerId)
{
print ("[" + Time.frameCount + "] #" + joystickFingerID + "" + Input.GetTouch(i).position);
//print ("Move/Stationary: " + joystickFingerID + " / " + Input.GetTouch(i).fingerId);
foundStationary = true;
varscreenPos2Touch = Input.GetTouch(i).position;
varworldPos2Touch = guiCamera.ScreenToWorldPoint(screenPos2Touch);
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
currentTouchX = worldPos2Touch.x;
currentTouchY = worldPos2Touch.y;
varcenterTouch = Vector2(joystickCenterX, joystickCenterY);
varcurrentTouch = Vector2(currentTouchX, currentTouchY);
vardstTouch = Vector2.Distance(centerTouch, currentTouch);
if (largeScreen)
dstTouch = dstTouch * 2;
varhorizontalTouch = currentTouchX - joystickCenterX;
varverticalTouch = currentTouchY - joystickCenterY;
moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
varangleTouch : float = Mathf.Atan2(moveX, moveY) * Mathf.Rad2Deg;
if (!playerObject.GetComponent(playerScript).isDead)
{
if (GetComponent(joystick).isNotAttacking)
{
playerObject.GetComponent(playerScript).angle = angleTouch + mainCamera.GetComponent(SmoothFollow).angleOffsetX;
playerObject.GetComponent(playerScript).moveSpeed = dstTouch;
}
}
}
}
elseif (Input.GetTouch(i).phase == TouchPhase.Ended)
{
//print ("Finger #" + Input.GetTouch(i).fingerId + " Ended");
if (cameraRightFingerID == Input.GetTouch(i).fingerId)
cameraRightFingerID = 9;
elseif (cameraLeftFingerID == Input.GetTouch(i).fingerId)
cameraLeftFingerID = 9;
elseif (cameraUpFingerID == Input.GetTouch(i).fingerId)
cameraUpFingerID = 9;
elseif (cameraDownFingerID == Input.GetTouch(i).fingerId)
cameraDownFingerID = 9;
elseif (Input.GetTouch(i).fingerId == joystickFingerID)
{
print ("[" + Time.frameCount + "] EndjoystickFingerID: " + joystickFingerID);
endJoystick = 5;
endJoystickPos = Input.GetTouch(i).position;
print ("Pos: " + Input.GetTouch(i).position + "|" + endJoystickPos);
controllerUp = false;
JoystickObject.renderer.enabled = false;
}
elseif (Input.GetTouch(i).fingerId == button2FingerID)
{
button2FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(2);
}
elseif (Input.GetTouch(i).fingerId == attackFingerID)
{
attackFingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(1);
}
elseif (Input.GetTouch(i).fingerId == button3FingerID)
{
button3FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(3);
}
elseif (Input.GetTouch(i).fingerId == button4FingerID)
{
button4FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(4);
}
elseif (Input.GetTouch(i).fingerId == button5FingerID)
{
button5FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(5);
}
}
}
if (endJoystick != 0)
{
if (endJoystick == 1)
{
joystickFingerID = 9;
controllerUp = false;
playerObject.GetComponent(playerScript).moveSpeed = 0;
Destroy (JoystickObject);
endJoystick = 0;
}
elseif (endJoystick >= 2)
{
//print ("EndJoystick2");
endJoystick -= 1;
}
}
}