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 void Update () { //*********************************************************** - Pastebin.com .

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!

found any solution?

3 Answers

3

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.

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.

Use this script! :slight_smile:

It works almost perfectly across devices! :slight_smile:

      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);
			}
		}
	}