Why is Unity giving me the same touch twice?

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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Assets
{
    public class TestTouch : MonoBehaviour
    {
        public class t
        {
            public Touch touch;
            public int frame;
        }

        private t _last;

        void Update()
        {
            if (Input.touchCount < 1)
            {
                return;
            }
            var touch = Input.GetTouch(0);
            var frame = Time.frameCount;
            if (_last != null)
            {
                if (touch.position == _last.touch.position && touch.phase == TouchPhase.Moved && _last.touch.phase == TouchPhase.Moved)
                {
                    Debug.Log("Pause");
                }
            }
            _last = new t {touch = touch, frame = frame};
        }
    }
}

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?

UnityEngine.Touch is a struct, you always get new ones. You use Touch.fingerId to keep track of them.

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”.

bump

bump

bump

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?

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.

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 :eyes:

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.