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

Removing touch drag lag for android

Discussion in 'Scripting' started by SprayNpraY, Feb 28, 2020.

  1. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    Hi googling the name of this post I;ve seen there are already plenty of questions and other posts related but most I've seen are years old and haven't fully addressed my problem.

    Basically I'm in the process of prototyping a 2D game that involves on a mobile device (currently testing on android) dragging around the screen a small block to avoid colliding with obstacles. The issue I have is that when dragging/swiping on the screen to move the main character/block its a second or so behind where my finger is, even when I'm not dragging fast which causes it to feel very unresponsive and a bit clunky.

    Have any fixes been released for this with later versions of unity? This is my current movement code if this is related to the issue.

    Code (CSharp):
    1. #if UNITY_ANDROID
    2.  
    3.         if (Input.touchCount > 0)
    4.         {
    5.             Touch touch = Input.GetTouch(0);
    6.  
    7.             if (touch.phase == TouchPhase.Began)
    8.             {
    9.                 screenPoint = Camera.main.ScreenToWorldPoint(touch.position);
    10.             }
    11.             else if (touch.phase == TouchPhase.Moved)
    12.             {
    13.                 Vector3 deltaPosition = Camera.main.ScreenToWorldPoint(touch.position) - screenPoint;
    14.                 screenPoint = Camera.main.ScreenToWorldPoint(touch.position);
    15.                 transform.position += deltaPosition;
    16.             }
    17.         }
    18. #endif
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    When is your code running? Update()?

    You should note that Unity by default caps framerate on mobile devices to 30 fps for the sake of battery conservation. Input might seem more responsive if you uncap the framerate. (Though maybe consider uncapping it only while the user is touching the screen or something like that.)
     
  3. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    Thanks for the response. Yes it's running inside of update, are you sure this is a frame rate issue? as even though I haven't fully understood it in other posts that I've seen I think it maybe something related to the responsiveness to certain android devices when being touched but I could be wrong.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    I'm not remotely sure. I'm just throwing out ideas for things you could try.
     
  5. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    Thanks for the suggestion, I didn't realize as this is a project I started years ago that I've recently come back to that I have already tried similar to your suggestion and made it so the fps starts at 60 which I think is the cap for mobile devices but also had a button that can increase it in game and display the FPS which I put to over 200 and unfortunately it didn't fix the issue.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public static int target = 60;
    4.     public Text frameRateText;
    5.  
    6.     void Start()
    7.     {
    8.         QualitySettings.vSyncCount = 0;
    9.         Application.targetFrameRate = 60;
    10.  
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (target != Application.targetFrameRate)
    16.         {
    17.             Application.targetFrameRate = target;
    18.         }
    19.         frameRateText.text = Application.targetFrameRate.ToString();
    20.     }
    21.  
    22.     public void IncreaseFrameRate()
    23.     {
    24.         target++;
    25.     }
    26.     public void DecreaseFrameRate()
    27.     {
    28.         target--;
    29.     }
    30. }
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Did you actually achieve a framerate of 200 or did you just set the target to 200? The actual frame rate can be lower than the target if there are performance issues.
     
  7. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    I basically created a button that can be pressed whilst play testing on my mobile that I can increase and decrease the frame rate and it displays it as the top of the screen. I don't believe it has anything to do with performance as Ive also tested it with very minimal in the scene I think its something to do with androids hardware but I can't find a solution to it.
     
  8. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    You can enable show touches in your android device under the developer options. This makes your touches visible on your touch screen making it possible to compare your objects location with the actual touch information processed by your device. Then you can also see how far behind is your device's touch info from your finger. You can not make your object go faster then the device's touch information. Your object can most likely be couple of frames behind of your device's touch position. So yeah part of the lag is due to your device's hardware and processing speed. If it lags behind more, then this can be caused by something in your app.
     
    SprayNpraY likes this.