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

Why is Unity giving me the same touch twice?

Discussion in 'Scripting' started by Deleted User, Dec 22, 2016.

  1. Deleted User

    Deleted User

    Guest

    I'm polling the device for touch input and it seems Unity will sometimes give me back the same touch on the next frame. Here's a test:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6.  
    7. namespace Assets
    8. {
    9.     public class TestTouch : MonoBehaviour
    10.     {
    11.         public class t
    12.         {
    13.             public Touch touch;
    14.             public int frame;
    15.         }
    16.  
    17.         private t _last;
    18.  
    19.         void Update()
    20.         {
    21.             if (Input.touchCount < 1)
    22.             {
    23.                 return;
    24.             }
    25.             var touch = Input.GetTouch(0);
    26.             var frame = Time.frameCount;
    27.             if (_last != null)
    28.             {
    29.                 if (touch.position == _last.touch.position && touch.phase == TouchPhase.Moved && _last.touch.phase == TouchPhase.Moved)
    30.                 {
    31.                     Debug.Log("Pause");
    32.                 }
    33.             }
    34.             _last = new t {touch = touch, frame = frame};
    35.         }
    36.     }
    37. }
    38.  
    If you run this in Debug mode and put a breakpoint at "Debug.Log("Pause");", you will see that the touches are the same but only the frame is different.

    Why is that and how can I fix it?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    UnityEngine.Touch is a struct, you always get new ones. You use Touch.fingerId to keep track of them.
     
  3. Deleted User

    Deleted User

    Guest

    Yes, I'm aware. If you run the example, you will see the fingerId is the same.

    My problem is that the touch is the exact same the next frame and still has the phase Moved. Because if the Touch did indeed move, then it shouldn't have the same position as the previous touch. If it does have the same position as the previous frame's Touch, then it shouldn't have the TouchPhase "Moved".
     
  4. Deleted User

    Deleted User

    Guest

  5. Deleted User

    Deleted User

    Guest

  6. Deleted User

    Deleted User

    Guest

  7. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    I'm not sure when the TouchPhase is set to Moved, but i would imagine it can happen with small changes.


    Anyway, i think it would be best if you would write why/how you need touches? Is it for some sort of replay feature?
     
  8. Deleted User

    Deleted User

    Guest

    The problem is that I have a LineRenderer object that I make bigger and move to show the user's touch trail but when a duplicate touch is made, it becomes strange.

    You can see it's not because of a small change because the position is the same.
     
  9. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Well Touch is basically the finger down. And as long as that finger, that "created" the touch (which you get from GetTouch(0)), is still pressing, the Id of GetTouch(0) will be the same.
    If you get a change in phase, that says it moved, and the position is saying to you it hasn't. Why not just ignore it that time?

    If you press with 2 fingers, then you GetTouch(0) and GetTouch(1).

    So you should rely on fingerId as @ThermalFusion said and the position change.
    GetTouch returns (not sure though) a new struct everytime. So there is no such thing as duplicate. Yes, the finger is still touching, it has the same id and the position is the same from previous or not.

    So either you check with previous touches, for the position change. Or adjust the LineRederer to not become strange.


    Hope it helps, but probably wont o_O
     
  10. Deleted User

    Deleted User

    Guest

    Yes for now that's what I'm doing (ignoring it) but I was wondering if there was a better way.

    But again, as I said before, this is only for a single touch so the touch ID stays the same and the position doesn't change.

    I'm not sure how one would adjust the line renderer to not become strange but false touches can affect everything.