SHAI
SHAI

Reputation: 859

Destroy GetMouseButtonDown event and position or force mousebuttonup

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))
        {...}

Upvotes: 0

Views: 206

Answers (1)

Programmer
Programmer

Reputation: 125315

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.

Upvotes: 1

Related Questions