My intent is to move a sprite across the screen with my finger making sure that the finger always remains at the same distance from the sprite and that the sprite moves precisely following the movement of the finger.
–
I had problems testing my code on different devices, while on my device (Xiaomi Mi5) it works perfectly, on others (Huawei P8 lite 2017 and Samsung Galaxy S3 neo) the sprite movement is much faster than that of the finger.
–
My code:
public class MoveBall : MonoBehaviour
{
private Vector3 transition;
private Touch touch;
void Update()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
transition = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0);
transform.position = transform.position + transition;
}
}
}
–
I did a lot of research and the problem seems to be the difference in dpi between the screens, but I couldn’t find anything useful about it.
–
So, if the problem is this: Is there a formula that works that somehow associates deltaPosition with dpi?
Meanwhile I’m trying to calculate the dpi of the device screens but the results of Screen.dpi is always the same (I’m testing in editor mode and Screen.dpi always returns 96 on different devices).
I suppose its Canvas Scaler issue. It scales your canvas on different devices, so you have “same” resolution on canvas, but in real resolution is different.
Thereby, your touch return real position, while your sprite has scaled position.
Select Canvas object and check Canvas Scaler. Set next parameters
The problem was to normalize the size of the view on which I am working on unity with compared to the proportions and dimensions of the device screens.
Code:
private void Start()
{
// "Normalize" the difference between the size of the Camera.orthograpich and the screen of the device
normailize = new Vector2(CameraSizePortrait().x / Screen.width, CameraSizePortrait().y / Screen.height);
lastPosition = transform.position;
}
//Calculate the deltaPosition manually and normalize it
void Update()
{
if (Input.touchCount == 1)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
delta = touch.position - lastPosition;
transform.Translate(delta.x * normailize.x, delta.y * normailize.y, 0);
}
}
}
private void LateUpdate()
{
lastPosition = touch.position;
}
// Returns the size of the Camera.orthographic in proportion to the screen of the device.
private Vector2 CameraSizePortrait()
{
if (-4.99f < Mathf.Abs(Screen.height/16 - Screen.width/9) && Mathf.Abs(Screen.height/16 - Screen.width/9) < 4.99f)
{
Debug.Log("16:9");
return new Vector2(Camera.main.orthographicSize * 2 / 16 * 9, Camera.main.orthographicSize * 2);
}
else if (-4.99f < Mathf.Abs(Screen.height/18 - Screen.width/9) && Mathf.Abs(Screen.height/18 - Screen.width/9) < 4.99f)
{
Debug.Log("18:9");
return new Vector2(Camera.main.orthographicSize * 2 / 18 * 9, Camera.main.orthographicSize * 2);
}
else if (-4.99f < Mathf.Abs(Screen.height/19 - Screen.width / 9) && Mathf.Abs(Screen.height/19 - Screen.width/9) < 4.99f)
{
Debug.Log("19:9");
return new Vector2(Camera.main.orthographicSize * 2 / 19 * 9, Camera.main.orthographicSize * 2);
}
else Debug.Log("Screen proportions not recognized"); return new Vector2(Screen.width, Screen.height);
}
I hope it can be useful to someone else with the same problem.`