Movement help,Help with some movement code

I got a bug, every time the player moves to the right(D) it keeps moving , when the key is pressed ,untip the left key(A) is pressed, but if The right key is pressed again the player kepps moving to the left. Same thing happens with up and down

      //Up
       if (Input.GetKey(KeyCode.W))
        {
             moveY = 1.0f;
        }
       //Right
        if (Input.GetKey(KeyCode.D))
        {
            moveX = 1.0f;
        }
      //Left
        if (Input.GetKey(KeyCode.A))
        {
            moveX = -1.0f;
        }
       //down
        if (Input.GetKey(KeyCode.S))
        {
            moveY = -1.0f;
        }
        
        rb.velocity = new Vector2(moveX, moveY).normalized * moveSpeed;

        //stop if no key is pressed
        if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.W)|| Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
        {
            moveX = 0f;
            moveY = 0f;
        }

I see two things wrong here:

  1. You should set moveX and moveY to zero if it’s time to stop before you set the rigidbody’s velocity, not after. Doing this after you set the velocity does nothing.

  2. The if statement you have where you check if no key is pressed is actually reading true if ANY key is pressed, so that’ll need to be fixed too.

I’ve yet to work purely in 2D, so I could be wrong, but would this work for you?

        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");

        Vector2 movement = new Vector2(moveX, moveY);

        rb.velocity = new Vector2(moveX, moveY).normalized * moveSpeed;

        if (movement == null)
        {
            moveX = 0f;
            moveY = 0f;
        }

Input.GetAxisRaw will give you a value in the range -1…1 based on which direction you’re pressing. These keys are bound to WASD and the arrow keys by default (also the left thumbstick of a gamepad).
even without the vector2 you should be able to use that to clean up the first 4 if statements.