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. Dismiss Notice

Bug First position of touch contact is 0,0

Discussion in 'Input System' started by samyam_, Jan 15, 2021.

  1. samyam_

    samyam_

    Joined:
    Jun 29, 2017
    Posts:
    10
    I have these actions, mainly the primary touch position and contact

    upload_2021-1-14_20-4-23.png
    Code:
    Code (CSharp):
    1.  
    2.     private void Start() {
    3.         // Subscribe to input action touch events
    4.         touchControls.Touch.TouchPress.performed += ctx => StartTouch(ctx);
    5.         touchControls.Touch.TouchPress.canceled += ctx => EndTouch(ctx);
    6.     }
    7.  
    8.     private void StartTouch(InputAction.CallbackContext context) {
    9.         Debug.Log("Touch started " + touchControls.Touch.TouchPosition.ReadValue<Vector2>());
    10.         if (OnStartTouch != null) OnStartTouch(touchControls.Touch.TouchPosition.ReadValue<Vector2>(), (float)context.startTime);
    11.     }
    12.  
    The first value printed is always 0, 0 for the screen position. This has happened to someone else I know as well. The other values are fine after the first one. The input debugger also shows the correct position. Seems like a bug to me. Any way around this? Thanks!
     
  2. tnyLe

    tnyLe

    Joined:
    Dec 19, 2018
    Posts:
    8
    Ditto!
     
  3. Ishimine

    Ishimine

    Joined:
    Mar 15, 2015
    Posts:
    5
    Im using the same code as samyam_ and in my case y have one and one. First is (0,0), second is OK, third is (0,0), etc.
     

    Attached Files:

  4. Dawdlebird

    Dawdlebird

    Joined:
    Apr 22, 2013
    Posts:
    85
    I'm using the "touch contact?" as well, which works fine on most phones, but apparently returns 0,0 on some...
     
  5. Chimoru

    Chimoru

    Joined:
    May 22, 2015
    Posts:
    4
    This is happening to me as well, has anybody discovered a workaround or resolution to this?
     
  6. GelanorS

    GelanorS

    Joined:
    Apr 23, 2021
    Posts:
    1
  7. danpos2506

    danpos2506

    Joined:
    Feb 29, 2020
    Posts:
    14
    Funnily enough I was following a tutorial by the OP and im still having this issue. @samyam_ did you ever find a fix for this?
     
  8. samyam_

    samyam_

    Joined:
    Jun 29, 2017
    Posts:
    10
    No I didn't unfortunately for this case, but you can use the EnhancedTouch API as a workaround to get the position
     
  9. vinimaykaul

    vinimaykaul

    Joined:
    Jun 16, 2020
    Posts:
    6
    Having the same issue, not just on some phones, but all phones I tried and also with simulated touch on Unity Editor. The first touch is always (0,0)

    I don't know what to do... :(
     
  10. vinimaykaul

    vinimaykaul

    Joined:
    Jun 16, 2020
    Posts:
    6
    Have a found a workaround though:
    I tried using a delay of 0.1f seconds using Coroutine to read the first value after 0.1 seconds. I did this Only for first touch value after loading a scene, using a bool variable, since the problem exists only in the first touch value calculated in the first frame after touch.

    Then I modified that 0.1f seconds to Time.deltaTime to wait only one iteration of Update. I realized that giving a fixed time may or may not work on different devices so am using Time.deltaTime.

    This still could cause issues I reckon, but should work most of the times
     
    Last edited: Sep 2, 2021
  11. FlyingMongoose

    FlyingMongoose

    Joined:
    Feb 2, 2014
    Posts:
    1

    So... I ran into this myself...using your guide Samyam, and by the way, thank you it's awesome!
    Anyway, I found out that this is specifically related to PRIMARY TOUCH. Seriously. I swapped my TouchPress Binding to Touch#0/Touch Contact?
    And it fixed the issue for me.

    I'm using 2021.2.7f1
     
    Last edited: Dec 31, 2021
    Ohilo, nobluff67, RuanAzevedo and 2 others like this.
  12. alatalo

    alatalo

    Joined:
    Jun 18, 2018
    Posts:
    9
    Confirmed! After changing from Primary Touch to Touch #0 the first touch position started to work beautifully. Thank you! Seems like something Unity should be fixing.
     
    Ohilo, RuanAzevedo, ns_summer and 3 others like this.
  13. TeacherMax

    TeacherMax

    Joined:
    Jan 27, 2019
    Posts:
    9
    UNconfirmed by me :(

    I changed to Touch#0/Touch Contact? [Touchscreen]
    and still get (0,0) as first point.

    I'll try @samyam_ suggestion above & see if that fixes it.

    (On a related note, & I know IU should make a separate post...) I have my own action to alter the coordinates of my touch screen and it only shows up in the drop down after some convoluted sequence of events I havent managed to nail down!)
     
  14. ccfoo242

    ccfoo242

    Joined:
    Oct 9, 2014
    Posts:
    85
  15. Mir1983

    Mir1983

    Joined:
    Jun 14, 2021
    Posts:
    2
    Using 2022.1.18.f1 and same issue. Rawposition returns 0,0 i am using old input system
     
    finz_devteam1 likes this.
  16. Ivan_Yeh

    Ivan_Yeh

    Joined:
    Dec 14, 2012
    Posts:
    3
    Checking "Initial State Check" on the passthrough for Touch #0/Position fixed for me. Not sure if it is good practice, but I suspect it has something to do with initialization done on the first touch. So I figured checking this box would force it to initialize.

    unity-issue-first-touch.png

    Hope this helps.
     
  17. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    114
    Original post is a bit old, but in this thread are a lot of error-prone answers.

    You don't need the enhanced touch API or stuff like that. You can do it by correctly setting up an action:

    Create an action and then click "Add Binding with one modifier".
    Set it up like shown here:
    upload_2022-11-2_9-0-58.png

    Modifier is the thing that will trigger the action.
    Binding is the value you'll get in the code, so you can just do a
    ReadValue<Vector2>
    to get the touch position.
     
    AndBje, Ohilo, nobluff67 and 8 others like this.
  18. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    366

    Thanks, works perfectly with any touch!
     
    rcfearn likes this.
  19. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    69
    By setting up my actions using this method I was able to reliably detect swipes with no errant input (like 0, 0 on the first swipe detection....)

    In case someone else ends up here via Google like I did:

    Set up your action like ManuelRauber says
    In the started event, save the starting position/time
    In the performed event, save the current position/time
    In the canceled event, use the last current position/time and that will be your ending position and time
    ???
    Profit! :D

    The resulting positions are screen co-ordinates, so if you want, use Camera.main.ScreenToWorldPoint(position) for interacting with game objects.
     
    ManuelRauber likes this.
  20. Mese

    Mese

    Joined:
    Dec 13, 2015
    Posts:
    37
    Setting up like this makes position to be 0,0 when called from the canceled event.

    As JonTheCoder said, you have to register the "current" position every "performed" event.

    REALLY weird.

    [Edit]

    WAIT "performed" is not triggering every frame, it does it in the beginning.
    This logic is not working.

    You have to ask for the last positions every frame on your own.
    I added a "OnTouch" on my InputManager wrapper that gets called every frame between "started" and "canceled".
     
    Last edited: Feb 7, 2023
  21. Tauntastic

    Tauntastic

    Joined:
    Sep 24, 2020
    Posts:
    15
    Touchscreen and Mouse are control schemes and both are driven by the mouse only anyway.
    upload_2023-3-14_17-15-28.png
    Tested further, seems like Touchscreen Simulation causes the first touch to be (0,0). Using just Touchscreen + GameView has no first touch (0,0)
     
    Last edited: Mar 14, 2023
  22. Hopp246

    Hopp246

    Joined:
    Jun 29, 2022
    Posts:
    1
    Thanks a lot. It is working perfectly now.
     
    ManuelRauber likes this.
  23. rcfearn

    rcfearn

    Joined:
    Sep 25, 2017
    Posts:
    1
    I don't think you've set it up correctly... I've just implemented this using the suggestions including tracking the last position with .performed and it appears to be working as expected.