Search Unity

[Solved-ish] Touch phase "Began" across multible frames until moved (EnhancedTouch)

Discussion in 'Input System' started by PatAndStormy, Apr 4, 2022.

  1. PatAndStormy

    PatAndStormy

    Joined:
    Jun 11, 2017
    Posts:
    5
    I have the following code, and I'm getting odd behavior regarding the Began phase. Sometimes it won't be called (goes straight to Moved), other times it will be repeatedly called until I move my mouse (using Touch simulation). Stationary will only be called after the first move. It sometimes behaves as expected, however. In another script (basically the same except for some movement stuff), the "Began" phase burst will be about equal to the times it skipped.

    My test code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem.EnhancedTouch;
    5. using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
    6.  
    7. public class TestInputs : MonoBehaviour
    8. {
    9.     private void Awake()
    10.     {
    11.         EnhancedTouchSupport.Enable();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (Touch.activeFingers.Count == 1)
    17.         {
    18.             Touch activeTouch = Touch.activeFingers[0].currentTouch;
    19.  
    20.             if (activeTouch.phase == UnityEngine.InputSystem.TouchPhase.Began)
    21.             {
    22.                 Debug.Log("Touch started: " + Time.frameCount);
    23.             }
    24.  
    25.             if (activeTouch.phase == UnityEngine.InputSystem.TouchPhase.Moved || activeTouch.phase == UnityEngine.InputSystem.TouchPhase.Stationary)
    26.             {
    27.                 Debug.Log("Touch moved/stationary: " + Time.frameCount);
    28.             }
    29.  
    30.             if (activeTouch.phase == UnityEngine.InputSystem.TouchPhase.Ended)
    31.             {
    32.                 Debug.Log("Touch ended: " + Time.frameCount);
    33.             }
    34.         }
    35.     }
    36. }
    Console output looks like this:

    Code (CSharp):
    1. Touch started: 226
    2.  
    3. Touch started: 227
    4.  
    5. Touch started: 228
    6.  
    7. Touch started: 229
    8.  
    9. Touch ended: 230
    10.  
    11.  
    I could just write my own "Began" check, but there's nothing to learn in doing that :). From my understanding EnhancedTouch is the one that should work with update() polling, but is there more I need to do to get that working?

    Thank you!

    Used this tutorial: https://forum.unity.com/threads/tut...with-input-systems-enhanced-touch-api.926753/
    --Edit in case anyone has this issue:
    I replaced
    Touch.activeFingers[0].currentTouch
    with just
    Touch.activeTouches[0]
    to get the expected behavior. If anyone whats going on here I'd appreciate it!
     
    Last edited: Apr 5, 2022
  2. PatAndStormy

    PatAndStormy

    Joined:
    Jun 11, 2017
    Posts:
    5
    So I added
    activeTouch.startTime
    to the began debug log, and they are all at the same startTime despite registering "Began" perpetually across frames (or until Ended or Moved).

    Code (CSharp):
    1. Touch started: 4 vs 3.64670062893674
    2. ...(many lines later)
    3. Touch started: 20 vs 3.64670062893674
    4.  
    5. Touch started: 21 vs 3.64670062893674
    6.  
    7. Touch started: 22 vs 3.64670062893674
    8.  
    It will do this forever btw, it doesn't appear to be a timing/polling issue where phase == Began for a few microseconds before it switches phase.

    -Update
    So I split the moved/stationary checks, and activeTouch.phase never is stationary even when it should be. It appears to be an issue where the touch.phase never actually changes unless the position is actually changing? I can't build to check on my device atm, but is this possibly to do with the touch simulation? Otherwise is there an issue about how the current touch is being polled? It should occur every Update() right?

    Console output where the phase is reporting as Moved even though there is no position changes:
    Code (CSharp):
    1. Touch moved 270 Pos: (1008.0, 743.0)
    2.  
    3. Touch moved 271 Pos: (1008.0, 743.0)
    4.  
    5. Touch moved 272 Pos: (1008.0, 743.0)
    6.  
    7. Touch moved 273 Pos: (1008.0, 743.0)
     
    Last edited: Apr 5, 2022
  3. PatAndStormy

    PatAndStormy

    Joined:
    Jun 11, 2017
    Posts:
    5
    So I replaced
    Touch.activeFingers[0].currentTouch
    with just Touch.activeTouches[0] and now everything is behaving as expected. I don't know why currentTouch isn't working (and never is stationary) for this especially given that the tutorial seems to indicate it should work fine. If anyone has an explanation I'd appreciate it!
     
  4. Lizby

    Lizby

    Joined:
    Dec 21, 2020
    Posts:
    3
    Thank you for this!! I thought I was losing it and this totally fixed it :)
     
    mcsimy likes this.