How to make sprite face direction of last movement when moving vertically

Hi! I’m brand new to Unity.

I used this post to nearly fix my problem. My sprite is now using the correct idle animation when moving horizontally.

My new problem is that when I move on only the Y axis (only W or S iputs), the sprite always faces left, not remembering which way the sprite was facing last.

To be clear, my sprite only has left and right animations.

The code from the post I used looks like this (together with my own (brackeys) code):


public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Animator animator;

    Vector2 movement;
    Vector2 movementLast;

    // Update is called once per frame
    void Update()
    {
        // Input - Horizontal sets X coordinates to 1 and vertical sets Y coordinates to 1.
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);

        // Sets player facing direction of last movement
        if (movement.x == 0 && movement.y == 0)
        {
            animator.SetFloat("HorizontalIdle", movementLast.x);
            animator.SetFloat("VerticalIdle", movementLast.y);
        }
        else
        {
            animator.SetFloat("HorizontalIdle", 0f);
            animator.SetFloat("VerticalIdle", 0f);
        }

        if (movement.x != 0) movementLast.x = movement.x;
        if (movement.y != 0) movementLast.y = movement.y;
    }

    // Fixed Update makes sure movement speed doesn't change with framerate, like "update once per frame" does. 50 times/second by default.

    void FixedUpdate()
    {
        // Movement - sets movement to our current position plus our movement. Fixeddeltatime makes sure movement speed stays the same no matter how many times our fixed update gets called. (=time elapsed since last time the function was called)
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

Animator: