- Home /
Drag an object to touch position(Problem)
I am using the below code to move the object to touch postion. I am developing for ios. Below is the code
using UnityEngine;
using System.Collections;
public class dragtheobjectontouch : MonoBehaviour {
public GUIText debugtext;
public GameObject dragobject;
public Vector2 realworldposition;
void Awake()
{
Application.targetFrameRate=60;
}
void Update () {
if(Input.touchCount>0)
{
foreach(Touch touch in Input.touches)
{
if(touch.phase==TouchPhase.Moved)
{
debugtext.text=touch.position.ToString();
realworldposition=Camera.main.ScreenToWorldPoint(touch.position);
dragobject.transform.position=new Vector3(realworldposition.x,realworldposition.y,dragobject.transform.position.z);
}
}
}
}
}
The Above code works perfectly but the problem is when i start moving the finger on ipad very fast the object lags behind the finger as if it is following it. Ofcourse it comes to the perfect position of touch when i stop moving the finger on the screen, but it really lags when the finger moves very quickly on the screen.
Answer by robertbu · Feb 11, 2013 at 08:36 PM
I found the question interesting, so I poked around a bit. If you Google some combination of "iOS/iPhone/iPad Touch/Input Lag/Lagging" you will find a bunch of hits not just for Unity but for iOS apps in general. This seems to be a general problem with touch devices (iOS and Android). The lag seems to be a hardware issue with the device and not something that can be solved by the apps.
The fact that your object eventually catches up to your finger is a strong indication this is a hardware issue. I did read in one place where a developer felt he had mitigated the issue somewhat by increasing the frame rate. There are downsides to increasing the FPS, but if you want, apparently it can be set like this:
Application.targetFrameRate = 60;
I don't have time to run the test at the moment, so if you test it, please post your results back to the list.
Here is one page that talks about hardware lag:
I already thought of increasing the framerate, so as you can see in the above code i have already increased the framerate to 60 before posting the question. Just check the awake function in my above posted code. But the Problem was not solved by increasing the framerate to 60. I did read the article from the link you posted. You might be right, it might be due to hardware lag.
If you do find out any solution, do post it here as that would be really helpfull to me and lot of others. And also one more thing the person who wrote this article is right about it. As even in the home screen of ipad4 when i move the icon with touch very quickly it lags behind the finger. So this is definitely the hardware issue and i don't think we can do anything about this. Thanks for your help and especially the link to the article.