- Home /
A problem with GetTouch(0) and GetTouch(1)
Guys I have created a TouchJoystick.cs but I have a problem in it. When I tap and hold on left side of the screen it says GetTouch(0) is in left side...ok so far so good, but when I simultaneously tap and hold on the right side of the screen it only sometimes says that GetTouch(1) is in the right side (which is what I want)... but sometimes It says GetTouch(0) is in the right side (or in other words though I was still holding down GetTouch(0) in the left side somehow it transfered it to the right side and made left side GetTouch(1) ) ... please help me.. here's the code.
And here are the two images needed for the script,
and
Answer by Bunny83 · Jul 09, 2015 at 01:03 AM
Here's an example:
public GameObject joypadBase1;
public GameObject joypadStick1;
public GameObject joypadBase2;
public GameObject joypadStick2;
public Camera camera;
private int joypadFingerID1 = -1;
private int joypadFingerID2 = -1;
void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
var t = Input.GetTouch(i);
var pos = t.position;
pos.z = 1.0f;
pos = camera.ScreenToWorldPoint(pos);
if (t.phase == TouchPhase.Began)
{
if (t.position.x < Screen.width*0.5f)
{
joypadFingerID1 = t.fingerId;
joypadBase1.transform.position = pos;
joypadStick1.transform.localPosition = Vector3.zero;
}
else
{
joypadFingerID2 = t.fingerId;
joypadBase2.transform.position = pos;
joypadStick2.transform.localPosition = Vector3.zero;
}
}
else if(t.phase == TouchPhase.Moved)
{
if (t.fingerId == joypadFingerID1 )
joypadStick1.transform.position = pos;
if (t.fingerId == joypadFingerID2 )
joypadStick2.transform.position = pos;
}
else if(t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled)
{
if (t.fingerId == joypadFingerID1 )
joypadFingerID1 = -1;
if (t.fingerId == joypadFingerID2 )
joypadFingerID2 = -1;
}
}
// This usually isn't necessary but is always good for safety
if (Input.touchCount == 0)
{
joypadFingerID1 = -1;
joypadFingerID2 = -1;
}
}
Note: This currently is ment to only work for one finger on each side. If you have more than two fingers on one side, always the last one counts. If you want it to keep the first you have to check "joypadFingerID 1 / 2" for being "-1" when the "Began" event happens. So you only assignn the fingerID when it doesn't have one yet.
@Bunny83 Wow thanks a lot... :) You have really saved my day. It worked just like I wanted and I hope to use this technique whenever I wanted to implement touch features :) I have just modified it a bit to suit my needs and here is the code :) using UnityEngine; using System.Collections;
public class TouchPlay5 : $$anonymous$$onoBehaviour {
public GameObject joypadBase1;
public GameObject joypadBase2;
public GameObject joypadStick1;
public GameObject joypadStick2;
public Camera camera;
private int joypadFingerID1 = -1;
private int joypadFingerID2 = -1;
void Update ()
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch (i);
Vector3 pos = t.position;
pos.z = 1.0f;
pos = camera.ScreenToWorldPoint(pos);
if(t.phase == TouchPhase.Began)
{
if(t.position.x < Screen.width/2)
{
if(joypadFingerID1 == -1)
{
Debug.Log("left side, ID: " + t.fingerId + " ,began.");
joypadFingerID1 = t.fingerId;
joypadBase1.transform.position = pos;
joypadStick1.transform.localPosition = Vector3.zero;
}
}
else
{
if(joypadFingerID2 == -1)
{
Debug.Log("right side, ID: " + t.fingerId + " ,began.");
joypadFingerID2 = t.fingerId;
joypadBase2.transform.position = pos;
joypadStick2.transform.localPosition = Vector3.zero;
}
}
}
else if(t.phase == TouchPhase.$$anonymous$$oved)
{
if(t.fingerId == joypadFingerID1)
{
joypadStick1.transform.position = pos;
}
if(t.fingerId == joypadFingerID2)
{
joypadStick2.transform.position = pos;
}
}
else if(t.phase == TouchPhase.Ended)
{
if(t.fingerId == joypadFingerID1)
{
joypadFingerID1 = -1;
joypadStick1.transform.localPosition = Vector3.zero;
}
if(t.fingerId == joypadFingerID2)
{
joypadFingerID2 = -1;
joypadStick2.transform.localPosition = Vector3.zero;
}
}
}
if(Input.touchCount == 0)
{
joypadFingerID1 = -1;
joypadFingerID2 = -1;
}
Debug.Log ("joystick 1 val: " + JoystickValues(joypadStick1.transform.localPosition) + " joystick 2 val: " + JoystickValues(joypadStick2.transform.localPosition));
}
public Vector2 JoystickValues(Vector3 joystickPosition)
{
Vector2 joystickPosReq = new Vector2(joystickPosition.x,joystickPosition.y);
return joystickPosReq;
}
}
Again thanks for your kind help:)
Answer by Graham-Dunnett · Jul 08, 2015 at 09:18 AM
Please read the documentation:
http://docs.unity3d.com/ScriptReference/Touch-fingerId.html
You'll see that the order of touches is guaranteed to change, which is why there is an id to make you easily track touches over time.
is it 100% Guarantee that the finger ID wont change? And also does it mean that I should never use the GetTouch Function?
@Vickylance: It is gueranteed that the fingerID doesn't change for a "tracked finger". A finger is tracked once the touch phase "Began" occured and the tracking ends once you get the phase "Cancelled" or "Ended".
Your problem is most likely that the left finger has interruptions. This can easily happen if you have your two touches on the same line (horizontally or vertically). If that happens the tracking of the left finger ended for a short time. If now the right finger just "starts" touching (so you get a Began) it will get the first finger ID since no touch was active at that moment.
You can use my $$anonymous$$obileStats app if you have an android device. If you're on iOS, i'm sorry, i don't have an iOS build ^^. But here's the one and only script that makes up the whole app. You're just missing the used GUISkin but basically it's jsut an empty scene with that script attached to the camera.
With that app you can see the index of the touch as well as the fingerID
edit
oops, I just realised that i actually use another script for drawing my bezier lines ^^. Here it is. That one isn't cleaned up. It just contains some utility methods.
@Bunny83 oh ok will try that app, andlet you know.... just one doubt you said,
Your problem is most likely that the left finger has interruptions. This can easily happen if you have your two touches on the same line (horizontally or vertically). If that happens the tracking of the left finger ended for a short time. If now the right finger just "starts" touching (so you get a Began) it will get the first finger ID since no touch was active at that moment.
Does it mean that the FingerID can even change if this problem occurs? And you also said,
It is gueranteed that the fingerID doesn't change for a "tracked finger". A finger is tracked once the touch phase "Began" occured and the tracking ends once you get the phase "Cancelled" or "Ended".
Is this not the case with Gettouch Function? Anyways now I am trying to convert all the Gettouch functions into fingerID functions which is a bit tough, will let you know if it all works.
@Vickylance:
As soon as a finger is no longer recognised by the touch hardware it is no longer tracked. If it "reappear" again the hardware has no relation to that finger anymore. It's a new finger to the system. As i said as long as you don't get a Cancelled or Ended for a certain fingerID it will always be the same ID.
GetTouch just takes an index to get "a touch" from the currently available ones.
Usually you use a for loop to loop through all touches and do your stuff there. For each logical control the user can control you should declare an integer variable that hold "-1" if it's not being used or the fingerID that is currently associated with your control. You assign the fingerID in "Began" and clear it in "Cancelled / Ended". It's actually pretty easy once you understand how the hardware works.
@Bunny83 Ok I get the difference between them but sorry to bother you again but could you please explain what you said here better
Usually you use a for loop to loop through all touches and do your stuff there. For each logical control the user can control you should declare an integer variable that hold "-1" if it's not being used or the fingerID that is currently associated with your control. You assign the fingerID in "Began" and clear it in "Cancelled / Ended". It's actually pretty easy once you understand how the hardware works.
I couldn't get it.. Again sorry to bother you again :(.
Your answer
![](https://koobas.hobune.stream/wayback/20220611230233im_/https://answers.unity.com/themes/thub/images/avi.jpg)