We remove the coordinates on the X axis from the touchpad in Unity

foreach(var th in Input.touches) { float coordx = th.position.x } 

coord x jerks hard when you hold your finger. Values ​​somehow gives the right, but a lot of high-frequency noise. How to filter, smooth?

    2 answers 2

    I found a touchpad device and their system of clearing interference. They just take three (there are variations, but the standard is three) the coordinates of the finger next to and average them. Thus, interference is minimized, and the necessary information about the location of the finger is saved.

    Method code - takes the coordinates of the finger, gives the average values

     float FilterX(float rawX) { if (lowFilterX.Count > 3) { if (save == 0) { filterCoordX = 0; } else { filterCoordX = save; } for (int i = 0; i < lowFilterX.Count; i++) { filterCoordX = (lowFilterX[1] + lowFilterX[2] + lowFilterX[3]) / 3; } save = filterCoordX; lowFilterX.Add(rawX); lowFilterX.RemoveAt(0); return filterCoordX; } else { lowFilterX.Add(rawX); return rawX; } } 

      The question is not clear and your intentions, too. So suppose that it is necessary to scroll the scene.

      1. You need to make sure that pressing one. Otherwise, as if the player will be moving five sausages in different directions, nothing can be determined.

         if (Input.touchCount == 1) 

      https://docs.unity3d.com/ScriptReference/Input-touchCount.html

      1. Find offset

         Input.touches[0].deltaPosition 

      https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html

      1. Shift the camera by deltaPosition multiplying it by some "X" (faster or slower)
      • I regret that I could not convey to you what I need. It is not that. I'll post the answer now. - Dmitrii