Check if you are hitting UI and if so ignore the touch
e.g.
public Vector2 lastTouchPosition;
// Optional some random initialization in case you directly hit the button
private void Start ()
{
lastTouchPosition = new Vector2 (Random.Range(0f, Screen.width, Random.Range(0f, Screen.height));
}
void Update ()
{
if(Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
if(!EventSystem.current.IsPointerOverGameObject())
{
lastTouchPosition = touch.position;
}
}
}
By default the Eventsystem takes all UI elements into account that have Raycast Target
enabled (default for all UI elements like Image
, Text
, TMP_Text
etc)
You you are using the new InputSystem refer to this post