How do you store and call last direction pressed for idle animations?

I’m trying to code two different idle animations–one for facing right, and one for facing left. By default, my character as of now only plays the idle animation facing right. My code looks like this:


if (Input.GetKey("d"))
  {
    rb2d.velocity = new Vector2(hSpeed, rb2d.velocity.y);
    if (isGrounded)
    animator.Play("RunR");
  }   
 else if(Input.GetKey("a"))
  {
    rb2d.velocity = new Vector2(-hSpeed, rb2d.velocity.y);
    if (isGrounded)
    animator.Play("RunL");
  }

else{
    if (isGrounded)
    animator.Play("IdleR");
    rb2d.velocity = new Vector2(0, rb2d.velocity.y);

I know the best thing to do is to somehow store the last key press as a velocity, and then call that last velocity to determine the correct idle animation (be it “IdleR” or “IdleL”). Can someone explain how to do it?

bool playerDirectionRight = true; // start position true=right, false=left

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("d"))
        {
            rb2d.velocity = new Vector2(hSpeed, rb2d.velocity.y);
            if (isGrounded)
                animator.Play("RunR");
            playerDirectionRight = true;  // set true for facing right
        }
        else if (Input.GetKey("a"))
        {
            rb2d.velocity = new Vector2(-hSpeed, rb2d.velocity.y);
            if (isGrounded)
                animator.Play("RunL");
            playerDirectionRight = false;  // set false for facing left
        }
        else if(playerDirectionRight)
        {
            if (isGrounded)
                animator.Play("IdleR");
            rb2d.velocity = new Vector2(0, rb2d.velocity.y);
        }
        else
        {
            if (isGrounded)
                animator.Play("IdleL");
            rb2d.velocity = new Vector2(0, rb2d.velocity.y);
        }
    }