Why does Input.GetMouseButton(0) work for touch devices when the touch count is less than 0, in unity3D?

Viewed 1005

I have read the Unity3D documentation related to mouse input and touch input but was not able to find any information that would solve this doubt of mine. I have also gone through several YouTube videos where they use Input.GetMouseButton() but they too didn't provide any information that would help me with this problem. Take a look at this snippet, where the else-if executes when I drag on the screen, and not the if statement.

public bool isDragging = true;
if(isDragging)
{
  if(Input.touches.Length < 0)
      swipeDelta = Input.GetTouch(0).position - startTouch;
  else if (Input.GetMouseButton(0))
    { 
          swipeDelta = (Vector2)Input.mousePosition - startTouch;
    }
}
1 Answers

Note: getMouseButton -> true as long as the button is pressed/held.

getMouseButtonDown -> true on the Frame the button was pressed. (talking about Update() frames. In FixedUpdate it could be true multiple times depending on fps.)

TouchPhase.Began = Input.GetMouseButtonDown(0)

TouchPhase.Ended = Input.GetMouseButtonUp(0)

Input.touchCount > 0 or Input.touchCount == 1 = Input.GetMouseButton(0)

So of course, when you hold down 1 Finger, the else-if is executed.

Related