- Home /
Input.GetTouch seems to lose the finger and replace it with the next # -- Video & code example.
Hey everyone. I'm using the code below to work the movement of the player on touch devices. It almost always works, and for weeks at a time there'll be no issues. but then all of a sudden, and a bunch today, it starts "skipping" (best word I can think of), where the ID # of the finger is lost and quickly replaced by a subsequent #, even though there is only one finger on the screen.
In the video the last line shows [Finger ID Number] | [Number of Fingers]. ID# 9 is the "no finger" ID, so it should read "9 | 0" or "0 | 1" at all times, since there is only one finger. (sometimes I've seen, but I didn't catch it for the video, the screen display that there are 2 or 3 fingers, even though only one is touching the screen). The last 5 seconds or so of the video shows the joystick circle (covered by my thumb some of the time). The finger doesn't come off the screen, but you can see the circle jump around as the script thinks the finger is off the screen then on in the next frame as a new finger ID.
Any ideas why this may be happening? I'm including the code, which seems solid to me and works almost all the time.
Thanks!
VIDEO: http://www.sfbaystudios.com/joystickissue.mov
CODE:
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
var foundHitTouchBegan : boolean = false;
var touchHitBegan : RaycastHit;
var touchRayBegan : Ray = guiCamera.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (touchRayBegan, touchHitBegan, 10000))
{
if (touchHitBegan.collider.gameObject.name == "Mini Map Plus" || touchHitBegan.collider.gameObject.name == "Mini Map Minus")
{
foundHitTouchBegan = true;
touchHitBegan.collider.gameObject.GetComponent(minimapZoom).ZoomCamera();
}
if (touchHitBegan.collider.gameObject.tag == "guiButton" && !foundHitTouchBegan)
{
var touchDownName = touchHitBegan.collider.gameObject.name;
if (touchDownName == "Camera Circle Right")
{
cameraRightFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
}
else if (touchDownName == "Camera Circle Left")
{
cameraLeftFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
}
else if (touchDownName == "Camera Circle Down")
{
cameraDownFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
}
else if (touchDownName == "Camera Circle Up")
{
cameraUpFingerID = Input.GetTouch(i).fingerId;
mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
}
else if (touchHitBegan.collider.gameObject.name == "MiniMapSpot")
{
touchHitBegan.collider.gameObject.GetComponent(toggleMap).GotHit();
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button 1")
{
if (isNotAttacking)
{
attackFingerID = Input.GetTouch(i).fingerId;
isNotAttacking = false;
playerObject.GetComponent(playerScript).ButtonPressed(1);
}
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button 2")
{
button2FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(2);
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button 3")
{
button3FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(3);
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button 4")
{
button4FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).ButtonPressed(4);
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button 5")
{
button5FingerID = Input.GetTouch(i).fingerId;
playerObject.GetComponent(playerScript).RightTriggerAction();
}
else if (touchHitBegan.collider.gameObject.name == "GUI Button Menu Open")
{
menuOpen = 5;
menuOpenButton.SetActive(false);
menuCloseButton.SetActive(true);
guiMenu.SetActive(true);
if (miniMap)
miniMap.SetActive(false);
titleController.GetComponent(titleControl).paused = true;
gameOn = false;
unpauseGame = false;
playerCamera.camera.enabled = false;
guiMenu.GetComponent(guiMenuMain).LoadMenu();
}
foundHitTouchBegan = true;
}
}
if (!foundHitTouchBegan && !controllerUp)
{
joystickFingerID = Input.GetTouch(i).fingerId;
controllerUp = true;
var screenPosLTouch = Input.GetTouch(i).position;
var worldPosLTouch = guiCamera.ScreenToWorldPoint(screenPosLTouch);
worldPosLTouch.z = 120.5;
JoystickObject = Instantiate(JoystickController, worldPosLTouch, Quaternion.identity);
var JoystickTransformTouch = JoystickObject.transform;
JoystickTransformTouch.parent = transform;
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
}
}
else if (Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)
{
var foundStationary = false;
if (cameraRightFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetX -= 5;
else if (cameraLeftFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetX += 5;
else if (cameraDownFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetY -= 0.5;
else if (cameraUpFingerID == Input.GetTouch(i).fingerId)
mainCamera.GetComponent(SmoothFollow).angleOffsetY += 0.5;
else if (joystickFingerID == Input.GetTouch(i).fingerId)
{
foundStationary = true;
var screenPos2Touch = Input.GetTouch(i).position;
var worldPos2Touch = guiCamera.ScreenToWorldPoint(screenPos2Touch);
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
currentTouchX = worldPos2Touch.x;
currentTouchY = worldPos2Touch.y;
var centerTouch = Vector2(joystickCenterX, joystickCenterY);
var currentTouch = Vector2(currentTouchX, currentTouchY);
var dstTouch = Vector2.Distance(centerTouch, currentTouch);
var horizontalTouch = currentTouchX - joystickCenterX;
var verticalTouch = currentTouchY - joystickCenterY;
if (dstTouch > 2)
{
var vectTouch : Vector2 = centerTouch - currentTouch;
vectTouch = vectTouch.normalized;
vectTouch *= (dstTouch-2);
moveX = (currentTouchX - joystickCenterX) + vectTouch.x;
moveY = (currentTouchY - joystickCenterY) + vectTouch.y;
}
else
{
moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
}
var angleTouch : float = Mathf.Atan2(moveX, moveY) * Mathf.Rad2Deg;
if (!playerObject.GetComponent(playerScript).isDead)
{
if (isNotAttacking && isNotDefending)
{
playerObject.GetComponent(playerScript).angle = angleTouch + mainCamera.GetComponent(SmoothFollow).angleOffsetX;
playerObject.GetComponent(playerScript).moveSpeed = dstTouch;
}
}
}
}
else if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
//print ("Touch " + Input.GetTouch(i).fingerId + " Phase Ended");
if (cameraRightFingerID == Input.GetTouch(i).fingerId)
cameraRightFingerID = 9;
else if (cameraLeftFingerID == Input.GetTouch(i).fingerId)
cameraLeftFingerID = 9;
else if (cameraUpFingerID == Input.GetTouch(i).fingerId)
cameraUpFingerID = 9;
else if (cameraDownFingerID == Input.GetTouch(i).fingerId)
cameraDownFingerID = 9;
else if (Input.GetTouch(i).fingerId == joystickFingerID)
{
joystickFingerID = 9;
controllerUp = false;
playerObject.GetComponent(playerScript).moveSpeed = 0;
Destroy (JoystickObject);
}
else if (Input.GetTouch(i).fingerId == button2FingerID)
{
button2FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(2);
}
else if (Input.GetTouch(i).fingerId == attackFingerID)
{
attackFingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(1);
}
else if (Input.GetTouch(i).fingerId == button3FingerID)
{
button3FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(3);
}
else if (Input.GetTouch(i).fingerId == button4FingerID)
{
button4FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(4);
}
else if (Input.GetTouch(i).fingerId == button5FingerID)
{
button5FingerID = 9;
playerObject.GetComponent(playerScript).ButtonReleased(5);
}
}
}
Your answer
Follow this Question
Related Questions
Need Clarification on some Touch Struct Variables 1 Answer
¿Unity messing up fingerIDs? [Android] 1 Answer
Asynchronous Multi-Touch help - keeping track of touches and corresponding gameObjects (Android) 1 Answer
multiTouch problem when fingers close together (Bug?) 1 Answer
iPhone Multitouch Problem 1 Answer