- Home /
Constant speed across mobile devices using 2D physics velocity?
We're working on a simple physics game that involves bouncing a ball off of a paddle. We set the velocity of the ball so that it stays constant (_ball.velocity = 500 * (_ball.velocity.normalized)). Problem is that when we deploy the build onto various mobile devices the ball moves at different speeds across the different devices. This is all done in 2D. The objects use rect transforms and rigidbody2D components. We have tried various methods of compensating for different screen sizes and densities, but none have worked. For example, we tried multiplying our velocity by (Screen.width / GetComponent().referenceResolution.x) which has solved other screen size issues for us, but did not work for creating a consistent speed across devices. Any help?
UPDATE: We figured out that the appearance of different speeds was caused by the lower-end devices lagging. The lag on those devices ends up causing everything to move slower than expected. I would have expected the physics engine to compensate and cause some frame loss, not reduce speed. The problem was further obscured by our attempts to compensate for delta-time and dimension which appears to be unneeded.
In short it's: _ball.velocity = 500 * (_ball.velocity.normalized)
We just set the velocity of the ball. We've tried adding things to that calculation like deltatime and the screen width / reference resolution as listed above, but nothing has made it move at a consistent rate across devices.
However, after that it's Unity's physics engine that is ultimately controlling the movement of the ball. Some devices it will move crippling slow on and others it will move blazingly fast.
That math (velocity to 500) looks fine to me. Velocity is in meters/second, and the physics system automatically tries to compensate for frame rate to do that.
But, the objects use RectTransforms? So movement becomes more-or-less in pixels (the game pretends every pixel is one meter)? In that case, clearly something like your screen.width/reference.width
is needed, since the "world" grows and shrinks with the device. Are the devices the same aspect ratio (so you don't also have to worry about the height)?
Answer by Kh4yer · Sep 17, 2017 at 05:18 PM
Help @MikeMcDonald When I move my ball using
Ball.transform.position += Ball.transform.up * speed *Time.deltaTime;
//or
Ball.transform.position += Ball.transform.up * speed ;
It moves at different speeds depending on the resolution
Your answer
![](https://koobas.hobune.stream/wayback/20220611233620im_/https://answers.unity.com/themes/thub/images/avi.jpg)