Problem is not reproducible or outdated
[MOBILE 2D] Make touch.deltaposition consistent across all devices
So, I have this method that moves an object relative to finger move through the x axis. It works fine on my 720p phone, however when I try it on a 2k phone like the samsung s6, it moves way, way faster. I'm pretty sure it does not have to do with the performance, since I'm using Time.deltatime.
if (touch.phase == TouchPhase.Moved && gameStarted)
{
transform.Translate(new Vector3((touch.deltaPosition.x * velocityConstant * Time.deltaTime), 0, 0));
}
My assumption is that touch.deltaposition is given on pixel coordinates, even though the official Unity documentation isn't very clear on that.
How could deltaposition be consistent across all devices, so that it considers world coordinates instead of screen resolution/pixels?
PS: I already tried tinkering with ScreenToWorldSpace, without any success
Answer by ArturoSR · Aug 02, 2016 at 03:21 AM
Hi, you must normalize what ever the value you are using as reference, i.e.:
Vector2 screen;
Vector2 screenNormalized;
Vector2 touchPosition;
void Start () {
screen = new Vector2 (Screen.width, Screen.height);
}
void Update () {
touchPosition = Touch.position;
screenNormalized = new Vector2 (touchPosition.x / screen.x, touchPosition.y / screen.y);
}
Making this you have a normalized pair of values (0 to 1 in both values), this helps a lot if you need to calculate delta movement using the screen of any device.
Just the answer I was looking for. Clear and concise. Thanks a lot! :)
Follow this Question
Related Questions
When The Screen Is Touched Jump 0 Answers
How to optimize touch input script 0 Answers
touch.position values clearification 0 Answers