Removing touch drag lag for android

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.

#if UNITY_ANDROID

        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began)
            {
                screenPoint = Camera.main.ScreenToWorldPoint(touch.position);
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Vector3 deltaPosition = Camera.main.ScreenToWorldPoint(touch.position) - screenPoint;
                screenPoint = Camera.main.ScreenToWorldPoint(touch.position);
                transform.position += deltaPosition;
            }
        }
#endif

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

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.

I’m not remotely sure. I’m just throwing out ideas for things you could try.

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.

public class GameManager : MonoBehaviour
{
    public static int target = 60;
    public Text frameRateText;

    void Start()
    {
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 60;

    }

    void Update()
    {
        if (target != Application.targetFrameRate)
        {
            Application.targetFrameRate = target;
        }
        frameRateText.text = Application.targetFrameRate.ToString();
    }

    public void IncreaseFrameRate()
    {
        target++;
    }
    public void DecreaseFrameRate()
    {
        target--;
    }
}

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.

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.

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.

1 Like