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

Bug Touch position is changing every frame on Android without finger movement

Discussion in 'Scripting' started by marknasr22, Dec 31, 2022.

  1. marknasr22

    marknasr22

    Joined:
    Sep 11, 2022
    Posts:
    2
    On Android Input.GetTouch(0).position gives a different x float number every frame even though my finger is not moving.
    I am using Samsung S9+ for testing.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Have you considered switching to decaf?

    I haven't seen this... but I have seen the opposite, where there is a 1/8" or so "gap" as you start to drag on Android.

    It may be necessary for you to do your own "latching" of it. Pseudocode would look something like:

    Code (csharp):
    1. private Vector2 lastTouch;
    2. private const float MinimumMotion = 5.0f;  // pixels
    and then something like:

    Code (csharp):
    1. // read touch into Vector2
    2.  
    3. // calculate distance between this touch and lastTouch
    4.  
    5. // if distance > MinimumMotion then store this touch into lastTouch
    6.  
    7. /return lastTouch;  // as if it was the real one
     
    marknasr22 likes this.
  3. marknasr22

    marknasr22

    Joined:
    Sep 11, 2022
    Posts:
    2
    Thank you for the reply.

    What does decaf stand for?

    Yes, I thought of that idea and it does work better, but it can cause unwanted behavior when touch is ignored because the x difference seems to be random, especially on different devices.
    The best I I could think of is to use the Screen.width in the check as well, but I am wondering if there is a reliable way to get exact touch positions.

    The new Input system seems to have the same exact issue as well.
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Kurt is suggesting that you're shaking: too much coffee :p

    Given how dense phone displays are nowadays, I wouldn't be surprised if, even with your finger seemingly held still, you still got some jitter.

    If you can get the physical size of the display, you should be able to work out how many pixels make up an inch, and then use that to implement a deadzone.
     
    marknasr22 and Kurt-Dekker like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Agreed... but I am equally surprised the OS in this case isn't quelling this small motion for you.

    In 100% of the devices I have used, the OS (Android or iOS) applies all kinds of quelling to stabilize touches, filtering over time and position and distance moved from origin.

    What Chemical said above... I was implying that your finger was vibrating from too much coffee. :) The phrase dates back to these 1980s commercials for Sanka:

     
    marknasr22 likes this.