- Home /
Different speed on different phones
This is the code I have for rotating a cube around itself and it works great on my 18:9 aspect ratio phone, but on 16:9 aspect ratio phone the speed of rotation is slower. How can I fix this?
Touch touchZero = Input.GetTouch(0);
Vector3 touchPos = touchZero.deltaPosition;
float rotX = touchPos.x * rotSpeed * Mathf.Deg2Rad;
float rotY = touchPos.y * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(transform.position, Vector3.up, -rotX);
transform.RotateAround(transform.position, Vector3.right, rotY);
This is because you are not rotating it by the time of the frame. If your code is not frame-dependent it will have different speeds on all devices, fastest on a PC. Everything is run on "frames" which is just a single linear execution of all the code, once it is done executing the frame sets the Time.deltaTime float to the amount of time it took to run, and starts a new frame. Allowing us to scale the time it takes things over the course of the time it takes to do things. So change your code to do this.
Touch touchZero = Input.GetTouch(0);
Vector3 touchPos = touchZero.deltaPosition;
float rotX = touchPos.x * rotSpeed *Time.deltaTime * $$anonymous$$athf.Deg2Rad;
float rotY = touchPos.y * rotSpeed *Time.deltaTime * $$anonymous$$athf.Deg2Rad;
transform.RotateAround(transform.position, Vector3.up, -rotX);
transform.RotateAround(transform.position, Vector3.right, rotY)
I just tried this code and I still get the same problem. The cube is being rotated by touching and dragging by the way.
Ah I didn't think about it before but also the problem is probably screen resolution. Every device will have a different resolution so the distance the touch travels will be less or greater on various devices. $$anonymous$$y suggestion would be to create a reference point and use that to scale all movement by. Something like this.
public float ReferenceSize = 720f;
private float refSizeScale;
void Awake()
{
refSizeScale = 1 * (Screen.width / ReferenceSize);
}
Then add that to your movement like so.
Touch touchZero = Input.GetTouch(0);
Vector3 touchPos = touchZero.deltaPosition;
float rotX = touchPos.x * rotSpeed *Time.deltaTime * refSizeScale * $$anonymous$$athf.Deg2Rad;
float rotY = touchPos.y * rotSpeed *Time.deltaTime * refSizeScale * $$anonymous$$athf.Deg2Rad;
transform.RotateAround(transform.position, Vector3.up, -rotX);
transform.RotateAround(transform.position, Vector3.right, rotY)
Your answer
![](https://koobas.hobune.stream/wayback/20220612191833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hi, knife hit, how do you control the speed of the wheel and the speed of the knife? 1 Answer
Need Help, Rotating a wheel with touch or using the mouse 2 Answers
Object Rotation/Character Speed 1 Answer
Rotation with different speed for different axis 2 Answers
How to modify the rotation speed of an object using Transform.Rotate 3 Answers