- Home /
How to use touch.deltaPosition properly?
I use following code for measure touch delata position:
private Touch firstDetectedTouch;
public Vector3 dragDistance;
void Update() {
if (Input.touchCount > 0) {
firstDetectedTouch = Input.GetTouch(0);
if (Input.GetTouch(0).phase == TouchPhase.Moved) {
dragDistance = firstDetectedTouch.deltaPosition;
}
}
}
Then I sent dragDistance to my Player script and use it to move character as follow:
body2D.AddForce(speed * touchPhase.dragDistance * Time.deltaTime, ForceMode2D.Impulse);
On some devices(e.g Samsung Galaxy s3) it works as I expect. I can smooth control my character. But lately I had oportunity to test it on another device like Sony Xperia M2 and the result is terrible. To be honest it's unplayable. Sometimes character moves very slow other times it's rush as crazy. I don't know it's because I use this function wrong or missunderstand concept of delataPosition?
Answer by imaxus · Jun 06, 2016 at 02:26 PM
Hi,
with every physics activity and movement it's better to use FixedUpdate that Update. Because Update is called every frame, so on phones with good graphic chip it will be fast and on the others user experience will be poor. FixedUpdate is called every time the phisics is calculated (ex. 0.2s)
So, should I measure deltaPosition in FixedUpdate() also? This line of code:
body2D.AddForce(speed * touchPhase.dragDistance * Time.deltaTime, Force$$anonymous$$ode2D.Impulse);
is in Fixedpdate() in another script.
Edit : sorry i read this in bus, need to think a while on it.
If i were u i will try put everything in Fixed update, in first step in the same file to check if it work or not. If yes, then separate code to your two different scripts.
I tried both and don't work. But ins$$anonymous$$d of Touch.deltaPosition I create own method and it works perfect so far.
currentPosition.x = Input.GetTouch(0).position.x - pastPosition.x;
currentPosition.y = Input.GetTouch(0).position.y - pastPosition.y;
pastPosition = Input.GetTouch(0).position;
Thanks for your time and help.
Your answer
Follow this Question
Related Questions
Android controller 0 Answers
Table Tennis game using PC as screen and smartphone as Controller 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do you get input from a moga controller? 1 Answer
Unity android bluetooth controller disconnect doesn't make array empty 1 Answer