Does anyone know how to disable or set to zero the dead zone which is between:
TouchPhase.Began->TouchPhase.Moved and TouchPhase.Stationary->TouchPhase.Moved?
I am referring to Touch touch = Input.GetTouch().
While coding pure Java or native C++ on Android there is no problem with input. You can have smooth one pixel precision while processing input.
But in Unity there is somthing like 5 pixels dead zone (drag threshold). It looks like this:
TouchPhase.Began touch position x = 100
TouchPhase.Moved touch position x = 105 (or more)
TouchPhase.Moved touch position x = 106 (+1 pixel)
TouchPhase.Moved touch position x = 107 (+1 pixel)
...
TouchPhase.Stationary touch position x = 200
TouchPhase.Stationary touch position x = 200
TouchPhase.Stationary touch position x = 200
TouchPhase.Moved touch position x = 205 (or more)
TouchPhase.Moved touch position x = 206 (+1 pixel)
TouchPhase.Moved touch position x = 207 (+1 pixel)
...
So after phase switches to “moved” you can capture input every pixel, but before that, there is some drag threshold wich has to moved over before you get movement.
Does anyone know how to get rid of this?
I realize this thread is a bit old but I also stumpled upon another version of this issue while I was developing some digital and analog sticks for mobile today. It is not exactly the same problem, but I think the background is the same since the default threshold value for validating a drag operation seems to kick in also for the EventSystem (if you have UI-based components). To me, it seems like a threshold in the ~5 px range is used also there.
So, the solution which worked for me was to let the script implement the IInitializePotentialDragHandler and modify a flag in the event object passed in the callback, e.g.
public class NoDragThresholdClass : MonoBehaviour, IInitializePotentialDragHandler, IPointerDownHandler, IPointerUpHandler, IDragHandler
,,,
// Other drag interface implementations
,,,
public void OnInitializePotentialDrag(PointerEventData eventData)
{
// Disable the drag theshold to trigger drag instantly
// which let us manage filtering and clipping ourselves
eventData.useDragThreshold = false;
}
....
Hope this helps someone that also struggly with this. Did you (mid@finger) find any solution working for any touch event? (Besides the proposed filtering solution). I haven’t tried implementing this interface on a class that handles raw touches. Might do that later.
This (pixelDragThreshold) should be what you’re looking for. I’m not sure whether or not it can be set in the editor itself, you can set it via script.
You can also set it for specific event as far as i know with this.
Edit: Sorry, updated the link.
To smooth the input you have to add for example last 10 values and divide by 10. To change the weight of those values you could do v10.1 + v20.2 + v30.3… + v101.0 where vX is the input value. To get rid of the threshold problem, create some float variable T, set it to 0.0 and increase it by some value like 0.1 when you get any input up to max 1.0 and decrease it to 0.0 by 0.1 when there is no input. For example:
drag start
first input offset 5, T=0.0, output offset 0.0 (here you got rid of the threshold value of 5) or averaged value of last 10
next input offset 1, T=0.1, output offset 0.1 or averaged value of last 10
next input offset 1, T=0.2, output offset 0.2 or averaged value of last 10
next input offset 1, T=0.3, output offset 0.3 or averaged value of last 10
next input offset 1, T=0.4, output offset 0.4 or averaged value of last 10
...
next input offset 1, T=1.0, output offset 1.0 or averaged value of last 10
drag stopped or paused
next input offset 0, T=0.9, output offset 0.0 or averaged value of last 10
next input offset 0, T=0.8, output offset 0.0 or averaged value of last 10
...
next input offset 0, T=0.0, output offset 0.0 or averaged value of last 10
Input offset is what you you get from unity,
output offset is a modified value without threshold which can be additionally averaged.