Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

In the new inputs system, correct way to get the touch position when a touch down event occurs?

Discussion in 'Input System' started by jrabek, Mar 11, 2021.

  1. jrabek

    jrabek

    Joined:
    Feb 4, 2020
    Posts:
    15
    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:

    Code (CSharp):
    1.  
    2.     public void OnTouchDown(InputValue value)
    3.     {
    4.         Vector2 pointerPosition = Touchscreen.current.position.ReadValue();
    5.         pointerInside = ConvertToLocalPoint(pointerPosition, out Vector2 localPoint);
    6.         pointerDown = value.Get<float>() == 1.0f;
    7.         OnPointerDownChange();
    8.     }
    9.  
    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!
     

    Attached Files:

  2. jrabek

    jrabek

    Joined:
    Feb 4, 2020
    Posts:
    15
    Forgot to note that I have tried the different values of "Pointer Behavior"
     

    Attached Files:

  3. jrabek

    jrabek

    Joined:
    Feb 4, 2020
    Posts:
    15
    Also weird is that the reverse is not true. If I build a webgl target and run on safari mobile I only see OnTouchDown get called.

    I also confirmed I do not have the touch simulation script enabled.
     
  4. Denizent

    Denizent

    Joined:
    Jun 22, 2022
    Posts:
    5
  5. PandaArcade

    PandaArcade

    Joined:
    Jan 2, 2017
    Posts:
    119