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,

49681-joypadbase1.png

and

49682-joypadstick1.png

Please read the documentation:

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.

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.