3
\$\begingroup\$

I'm trying to achieve smooth movement for a ball. I seem to be failing to achieve this and now I've created a sample scene, which shows my setup from the game and the problem exists in that sample scene, as well.

I've uploaded the sample project to github: https://github.com/iQew/PhysicsTesting (open Assets/Scenes/SampleScene)

Here's a recording of the problem. It's a bit more extreme here than it is in the editor, but it shows the problem even better because of it: https://imgur.com/a/ByzwKlg

Scenario: I have a 2D mobile game (android) using Unity 2019.4.14f1 with URP. The camera does not move and will always have the same settings. The playfield consists of a certain area, which is blocked in by walls, which have a BoxCollider2D on them. The ball is a simple sprite with a CircleCollider2D, RigidBody2D and a Trail Renderer.

I have deleted all quality settings except the "Low" one, which has VSync Count set to "Don't Sync". In the Awake block of a controller script I set the Application.targetFrameRate to 60. In the Start block I use BallRigidBody2D.AddForce(new Vector2(1f, 10f), ForceMode2D.Impulse) to make the ball move.

The RigidBody2D of the Ball has the following settings:

  • Body Type: Dynamic
  • Material: DefaultBall (a Physics2D material with friction: 0 | bounciness: 1)
  • Simulated: true
  • Use Auto Mass: false
  • Mass: 0.025f
  • Linear Drag: 0
  • Angular Drag: 0
  • Gravity Scale: 0
  • Collision Detection: Continous
  • Sleeping Mode: Start Awake
  • Interpolate: Interpolate

All other project settings are at default.

When I launch the game, the ball starts moving as expected. Most of the time the movement is smooth, but there is stuttering every now and then. I am wondering, if there is a way to make this stuttering go away. The tutorials I found on the internet, which solve this exact problem set the Interpolation mode of the RigidBody2D to "Interpolate", but I did that and there's no change.

There are no other scripts interfering with the physics calculation at all. So, I assume this has to do with some kind of project settings? Using a smaller Fixed Timestep does not help, in fact it gets a lot worse, if I use 1/120 for example.

Any help would be greatly appreciated!

enter image description here

enter image description here

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ smaller time steps are more accurate, its expected to perform worse \$\endgroup\$ – Icebone1000 Dec 26 '20 at 19:53
  • \$\begingroup\$ are you using addforce on FixedUpdate? physics stuff should not be done on the Update \$\endgroup\$ – Icebone1000 Dec 26 '20 at 19:58
  • \$\begingroup\$ @Icebone1000 I'm using AddForce in OnPointerUp method, which is neither in Update nor FixedUpdate block \$\endgroup\$ – iQew Dec 28 '20 at 16:35
0
\$\begingroup\$

I've added VSync back in and removed the targetFrameRate code. The stuttering is still visible in the editor, but on the target devices it's a lot better now.

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't look like a complete solution to the problem. Want to post this update as part of your question instead? \$\endgroup\$ – DMGregory Dec 30 '20 at 14:36
0
\$\begingroup\$

I think the stuttering comes from your set resolution. The default aspect after your checkout was 3840x2160 which is about 4 times the size my screen can handle and even more so in the small preview window. I can see under Stats - Screen that it tried to render the 4k resolution into my 1483x616 game view. For every ~2.5 that it tried to render, it had only the space of one pixe;. The stuttering comes from the fact that the ball can't fit perfectly in and sometimes places it a pixel ahead or behind. Changing to Free Aspect and the ball was running smoothly.

Since you wrote that you are targeting mobile in the first place, it would be better to change the aspect to your test targets resolution and not what is your full monitor resolution (not many phones have that big of a resolution in the first place)

I lowered the fixed timestep to as much as 0.005 and the ball was still running smoothly between 100-120 fps on free aspect but it fell to around 30 on the 4k setting.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.