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