I am having problems with making my character face the movement direction after stopping.

Ok so I’m making a 2D top down shooter game following a tutorial by Couch Ferret. When I press only press one direction, the character stays facing that direction when I let go. However when I press two buttons at once, like right and up, to move diagonally and then let go, the character faces one of the two directions but not diagonally. I guess this is because I am not letting go of both directions at exactly the same time but I still want the character to face diagonally even if I don’t let go of the buttons at exactly the same time (maybe like a 0.5 second window). But I don’t know how to do do it as I am still new to coding.
Anyways here is my code:

    public Rigidbody2D rb;
    public float speed;
    public Animator animator;
    public Vector2 movement;

    void Update()
    { 
        movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        rb.velocity = Vector2.ClampMagnitude(movement, 1.0f) * speed;
    
        if (movement != Vector2.zero)
        {
            animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
            animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
        }
       
    }

Thanks in advance for anyone who helps.

IIRC, The reason the direction is not preserved is because of how the GetAxis works.

When you release one of the buttons, the value that represents that axis returns to zero.
If you release 2 buttons, the last axis to equal to zero will be the direction the character is facing if that makes sense.
When I worked on it, I had to create a variable that stored the values of the axis so I could preserve the direction. How you implement it will depend on what your script but this is something that I came up with though untested.

private float xSaved, ySaved;

void Update()
{ 
    //By getting the raw inputs, it only returns a 1, 0 or -1 depend on the axis direction
    float xRaw = Input.GetAxisRaw("Horizontal");
    float yRaw = Input.GetAxisRaw("Vertical");

    //Check if no input is being held.
    if (xRaw!=0 || yRaw!=0)
    {
        //Get the direction from the regular axis.
        xSaved = Input.GetAxis("Horizontal");
        ySaved = Input.GetAxis("Vertical");
        //Apply the preserved direction to your movement code
        movement = new Vector2(xSaved, ySaved);
        //rest of your code below here
    }
}