How to make player rotate 180° and run that direction with mecanim + vice versa?

My player has the X-axis locked, so the player can only jump and move forward/backward. I’m trying to make it so the player runs forward (with W) and then stops and runs backward when the “S” key is pressed and vice-versa.

I have all the animations to do this.

My player script:

using UnityEngine;

public class Player_Movement : MonoBehaviour

{
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float rotationSpeed = 5.0f;

    private float playerDirection;
    private float playerSpeed;

    private Vector3 moveDirection = Vector3.zero;
    private Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

	void Update()
	{
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
        }
        playerDirection = Input.GetAxis("Horizontal");
        playerSpeed = Input.GetAxis("Vertical");

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);

        anim.SetFloat("Direction", playerDirection);
        anim.SetFloat("Speed", playerSpeed);
	}
}

Found the answer!