How do I rotate my character only once?

So we’re basically making a 2D shooter type of game in Unity with a friend from school. I’m trying to make my character shoot in both directions which I’ve achieved with rotating the character on the y axis by 180 degrees on pressing the movement keys. The problem is, everytime I press the movement key (A and D), the character rotates to the wrong side if I press the direction key multiple times.

void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
            {
            transform.Rotate(0f, 180f, 0f);
            }
        if (Input.GetKeyUp(KeyCode.D))
            {
            transform.Rotate(0f, 180f, 0f);
            }
        isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
        movement = Input.GetAxis("Horizontal");
        if (movement > 0f)
        {
            rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
        }
        else if (movement < 0f)
        {
            rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
        }
        else
        {
            rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
        }
        if (Input.GetButtonDown("Jump") && isTouchingGround) {
            rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpspeed);
        }
        playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
        playerAnimation.SetBool("OnGround", isTouchingGround);

We are barely beginners to programming so I would appreciate some help

Hey,

As you are creating a 2D game, instead of rotating the game object to handle facing right or left, I recommend flipping the sprite on its local scale.

Have a go with something like this…

[HideInInspector] public bool facingRight = true;


void FixedUpdate()
{
	float h = Input.GetAxis("Horizontal");
	if (h * rb2d.velocity.x < maxSpeed) rb2d.AddForce(Vector2.right * h * moveForce);
	if (Mathf.Abs (rb2d.velocity.x) > maxSpeed) rb2d.velocity = new Vector2(Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);

	if (h > 0 && !facingRight)
		Flip ();
	else if (h < 0 && facingRight)
		Flip ();
}


void Flip()
{
	facingRight = !facingRight;
	Vector3 theScale = transform.localScale;
	theScale.x *= -1;
	transform.localScale = theScale;
}