Rigidbody2D jitter collision

So I am pretty new to C# coding so this may be completely wrong, but here goes. When i try to walk into a wall, my player starts jittering and i hjave no ida how to solve it. My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public float speed;

    private Vector2 direction;

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

    void Update() {

        GetInput();
        Move();
    }

    public void GetInput() {
        direction = Vector2.zero;

        if (Input.GetKey(KeyCode.W)) {
            direction += Vector2.up;
        }

        if (Input.GetKey(KeyCode.S)) {
            direction += Vector2.down;
        }
      
        if (Input.GetKey(KeyCode.A)) {
            direction += Vector2.left;
        }
      
        if (Input.GetKey(KeyCode.D)) {
            direction += Vector2.right;
        }
    }

    public void Move() {
        transform.Translate(direction * speed * Time.deltaTime);
    }
}

If anyone can spot how to fix it, that’ll be great!

Thanks!

EDIT: I fixed it using rigidbody2d.addforce. Weirdly, I have to multiply my speed by 1000 so i just created another variable and let the script multiply it by itself.

Because you’re not allowing the Rigidbody2D to update the Transform which is its primary reason for being there. Instead you’re changing the Transform which cause the Rigidbody2D to be instaltly repositioned to that position prior to the simulation running. Doing this means you can position the Rigidbody2D so that its collider(s) are overlapped. The solver runs and solves it. You then repeat this and force an overlap again.

When you add a Rigidbody2D you are putting it in charge of the Transform so don’t ever modify the Transform. Instead use the Rigidbody2D API such as Rigidbody2D.MovePosition, Rigidbody2D.Velocity etc.

If you’re not already aware, the physics runs during the fixed update not per-frame unless you step it manually per-frame yourself. There’s plenty of info about fixed-update/update online.