0

When I run my app on Android, the first finger touch calls Input.GetMouseButtonDown(0) and the second touch calls Input.GetMouseButtonDown(1). I want to override GetMouseButtonDown(0) in some cases- so the second finger (1) touch will become the first (0) and I don't know how to do it. Either this or how to force mouseButtonUp on the first finger touch- I want to remove this first "click" from the system so it'll not use Input.mousePosition in a case of 2 touches.

Why? I'm creating a paint app when the user can draw lines. There is an area when the user can paint (in a rectangle) and an area where he shouldn't, I know how to detect when pressed on unwanted area. But sometimes the palm of my hand creates unwanted first touch Input.GetMouseButtonDown(0) on the unwanted area (without Input.GetMouseButtonUp(0)) and when I start to draw a line Input.mousePosition gets the average of both touches, so I just want a way to remove a "down" touch/click from the system. Or another way to solve my problem.

Here is my code:

 if (Input.touchCount == 0)
    { screenPoint = new Vector3(0, 0, 0); 
     currentTouch = 4;  //currentTouch is for GetMouseButtonUp(currentTouch)  }

    for (int i=0; i< Input.touchCount; i++)
    {
      touch = Input.GetTouch(i);
      screenPointTemp = touch.position;
      screenPointTemp3 = new Vector3(screenPointTemp.x, screenPointTemp.y, zCam);

       //if the touch is in a "good" zone-
      if (Camera.main.ScreenToWorldPoint(screenPointTemp3).z > BottomNod.transform.position.z - nodeScale) 
      {
          screenPoint = touch.position;
          currentTouch = i;
       }

        }
    }

if (Input.GetMouseButtonUp(currentTouch))
        {...}
SHAI
  • 789
  • 3
  • 10
  • 43

1 Answers1

1

When working on mobile devices to detect a touch on the screen without clicking on any object, you should be using Input.touchCount with Input.GetTouch or Input.touches. Although I highly recommend Input.GetTouch since that doesn't even allocate temporary variables like Input.touches. To get the postion of the touch use, Input.GetTouch(index).position.

Each of these functions returns Touch so you can use Touch.fingerId to detect/keep track of how many touches you want at the-same time. You can also use the index that is passed in to Input.GetTouch to keep track of the touch. That's totally up to you.

This detects every touch down, move and up on mobile devices:

for (int i = 0; i < Input.touchCount; ++i)
{
    //Touch Down
    if (Input.GetTouch(i).phase == TouchPhase.Began)
    {

    }

    //Touch Moved
    if (Input.GetTouch(i).phase == TouchPhase.Moved)
    {

    }

    //Touch Up
    if (Input.GetTouch(i).phase == TouchPhase.Ended)
    {

    }
}

Limit to one touch only(Use index 0):

if (Input.touchCount == 1)
{
    //Touch Down
    if (Input.GetTouch(0).phase == TouchPhase.Began)
    {

    }

    //Touch Moved
    if (Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        //Draw?
    }

    //Touch Up
    if (Input.GetTouch(0).phase == TouchPhase.Ended)
    {

    }
}

Like I said, you can limit this with fingerId. The implementation depends on what exactly that you want.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hey thanks, I shouldn't limit to one touch because if I do- when the user touches the first touch (by a mistake) with his palm on an unwanted zone without noticing, he won't understand why when he touches the screen (second touch) nothing will happen... I added my code to my question, this was my solution but I still have problems- when the first touch is wrong and second is right, when I release the first touch (GetMouseButtonUp(CurrentTouch))== true, and I don't want it to be true. – SHAI Jul 10 '17 at 01:16
  • Also when I use- Input.GetTouch I can't test it on unity with my mouse – SHAI Jul 10 '17 at 01:23
  • You need to use [Unity Remote](https://stackoverflow.com/questions/39107153/how-to-speed-up-the-build-and-run-process-in-unity-for-mobile-devices-ios-androi/39107871#39107871) to test it or use Unity macro (`#if UNITY_EDITOR`) to separate the `GetMouseButtonUp` code. – Programmer Jul 10 '17 at 01:26
  • *"when the user touches the first touch (by a mistake) with his palm on an unwanted zone without noticing, he won't understand why when he touches the screen (second touch) nothing will happen"* In that case, the first touch should be considered as valid and be used but while the first touch is still on, the rest of the touches should be ignored? – Programmer Jul 10 '17 at 01:35
  • In this specific case, the first touch which is a mistake should be ignored- and the second one should be the valid one. Your answer wasn't exactly the solution but I replaced my GetMouseButtonUp with your TouchPhase.Ended and it solved my problem so thanks. – SHAI Jul 10 '17 at 02:00
  • Yeah. It wasn't made to me direct answer. It was made to show you which API you should be using. Glad I was able to help. – Programmer Jul 10 '17 at 02:05