[Solved] Rigidbody2d smooth movement

Hello. I am new to Unity.
I have a question about the movement of Rigidbody2D.
I add a sprite with Rigidbody2D to the 2d scene.

Player movement script:

    private Vector2 movement;
    private Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        movement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    }

    private void FixedUpdate()
    {
        rb.AddForce(movement * 4000 * Time.fixedDeltaTime);
    }

Script on camera:

    private GameObject player;

    void Start()
    {
        player = GameObject.FindWithTag("Player");
    }

    void Update()
    {
        Vector3 moveTo = new Vector3(player.transform.position.x, player.transform.position.y, -10f);
        transform.position = Vector3.Lerp(transform.position, moveTo, 10.0f * Time.deltaTime);
    }

When I start the game, the player moves with shaking, not smoothly. How to avoid this?
Thanks.

Enabling interpolation for Rigidbody2D and increasing the values of Linear Drag and Angular Drag solved the problem. In addition, you must include your sprite in the Player object as a child and rotate it if necessary (for example, rotate to a crosshair).

The reason you’re having to scale the force up so much is because you’re scaling it down by the fixed delta-time. You don’t need to do this because the force you specify here will be time-integrated when it’s applied during the simulation step.

In other words, the force you specify is being time-integrated (reduced) twice.

1 Like

Yes you are right. I also deleted the delta

1 Like