- Home /
different devices -> Different touch speed
Hello,
I'm developing a vertical scrolling shooter for Android on Unity 3.x. I'm facing an issue with the touch movement speed of my main character, the touch speed is different on the different devices i try it on.
Here is my working code for the touch movementes (it WORKS, but at DIFFERENT SPEED on DIFFERENT DEVICES):
void Update () {
//**********************************************************************************
//||||||||||||||||||||||||||||| MOVEMENT |||||||||||||||||||||||||||||||||||||||||||
//**********************************************************************************
if (Input.touchCount > 0) // TOUCHSCREEN CONTROLS
{
// Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
touchDeltaPosition.x *= touchSpeed;
touchDeltaPosition.y *= touchSpeed;
// Move object according to new positions
transform.Translate(touchDeltaPosition);
pos = transform.position;
transform.position = pos;
}
//**********************************************************************************
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//**********************************************************************************
}
You can check it better at http://pastebin.com/zJC7gX4J .
I'm convinced i have to use the Time.deltaTime somewhere here but i tried several options like: transform.Translate(touchDeltaPosition * Time.deltaTime);
or
transform.position = pos * Time.deltaTime;
or (already looking for ANY kind of solution)
transform.Translate(touchDeltaPosition * Input.GetTouch(0).deltaTime);
or
transform.position = pos * touchDeltaTime;
And none of them worked for me.
Any ideas are appreciated,
Thanks!
Answer by Graham-Dunnett · Apr 17, 2013 at 10:02 PM
Presumably the devices that you are testing on have different pixel resolutions or DPI. So, the touch position moves more pixels on one device than the other, so your deltaPosition
is different. You might want to do some maths that involves a percentage of the screen height and width. That way, if the player moves a finger say 25% of the screen width you don't care if that's 400 pixels or 64 pixels.
O$$anonymous$$, thanks for the answer, it seems like a nice idea so i gave it a try, but it's not working, the problem is still there(the movement speed of the object is different in different devices - i've tested on a motorola milestone 3, motorola smart plus and samsung galaxy S2).
Here is the code i used, maybe you can spot some error in my logic:
void Update() { if (Input.touchCount > 0) // TOUCHSCREEN CONTROLS { // Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Getting the percentage of the screen that it moved (result is in decimal format like 0.3 for 30%)
touchDeltaPositionWindowPercentage.x = (touchDeltaPosition.x * 100 / globalValues.windowWidth) / 100;
touchDeltaPositionWindowPercentage.y = (touchDeltaPosition.y * 100 / globalValues.windowHeight) / 100;
// Changing the screen percentage to real value
touchDeltaPosition.x = globalValues.windowWidth * touchDeltaPositionWindowPercentage.x * touchSpeed;
touchDeltaPosition.y = globalValues.windowHeight * touchDeltaPositionWindowPercentage.y * touchSpeed;
// $$anonymous$$ove object according to new positions
transform.Translate(touchDeltaPosition * Time.deltaTime);
pos = transform.position;
transform.position = pos;
}
}
you can see it better at: http://pastebin.com/uZ9AZAD3
Answer by SolarFly · Jun 23, 2014 at 10:35 PM
Touch.deltaPosition is just hardware delta. Touchscreen has query frequency. Sum of delta positions on update is a half of delta between positions of TouchPhase.Began and TouchPhase.End (on my Nexus 5). Just save start and end positions and calculate your own delta in current touch position.
Answer by jamius19 · Nov 12, 2015 at 04:27 PM
Use this script! :)
It works almost perfectly across devices! :)
Vector2 worldStartPoint;
Camera cam;
public float speed;
void Awake ()
{
cam = Camera.main;
}
void Update ()
{
// only work with one touch
if (Input.touchCount == 1) {
Touch currentTouch = Input.GetTouch (0);
if (currentTouch.phase == TouchPhase.Began) {
worldStartPoint = cam.ScreenToWorldPoint (currentTouch.position);
} else if (currentTouch.phase == TouchPhase.Moved) {
Vector2 temp = cam.ScreenToWorldPoint (currentTouch.position);
Vector2 worldDelta = temp - worldStartPoint;
//print (worldDelta);
cam.transform.Translate (-worldDelta.x * Time.deltaTime * speed, -worldDelta.y * Time.deltaTime * speed, 0);
}
}
}