tl;dr
I need to be able to get the current touch position when a touch down occurs and not just the fact the screen was touched.
Context
Version: Unity 2020.2.7f1
Game: I am building a drawing game that will run on mobile and desktop.
Gameplay: Whenever the player presses the left mouse button and moves the mouse on desktop, or touches down and drags on mobile a line should be drawn.
In the Input Actions I have defined (see screenshot)
- PointerMove (works the same for mouse and touch)
- LeftMouseButton
- TouchDown
The problem is that on desktop if a left click happens then OnTouchDown and OnLeftMouseButton both get called.
I need to separate the handlers because it seems the only way I’ve found to get the touch position when a touch down occurs is to do this:
public void OnTouchDown(InputValue value)
{
Vector2 pointerPosition = Touchscreen.current.position.ReadValue();
pointerInside = ConvertToLocalPoint(pointerPosition, out Vector2 localPoint);
pointerDown = value.Get<float>() == 1.0f;
OnPointerDownChange();
}
The issue is with this approach is that if hitting the left moues button causes it to be called as well, the touchscreen position seems to be Vector3.zero which causes the pointerInside
to be incorrectly calculated.
It is important to know where the touch occurs because there is a limited area to draw that is the canvas on screen.
Thanks in advance to anyone who has any tips or links!