2D physics jerky movement

Hello,
I have a problem with 2D movement.
It’s not smooth as it should be.

Thats the code I use to move player:

void FixedUpdate() {          
            if(Input.GetMouseButton(0) || Input.GetKey(KeyCode.Space))
                rb.AddForce(new Vector2(0, 10));
      rb.velocity = new Vector2(speed*Time.fixedDeltaTime, rb.velocity.y);
      cam.transform.position = new Vector3(transform.position.x + cameraDistance,
                                cam.transform.position.y, cam.transform.position.z);

After enabling V-Sync and Interpolation problem exists but only in editor. On build evertying is smooth.

It’s because the Editor is a managed application that performs a bunch of tasks while you’re playing your game and produces garbage etc.

cam.transform.position = new Vector3(transform.position.x + cameraDistance,
                                cam.transform.position.y, cam.transform.position.z);

The above code only moves the camera each fixed-update; you’d want to do that per-frame. If you set the Rigidbody2D to use interpolation then it’ll update the Transform position/rotation per-frame; you can track the Transform during the Update.

2 Likes