My sprite only flips when crossing the center of the screen.

I wrote this little code for my game which uses a move to mouseclick movement system. Now I want the sprite to rotate to the direction hes moving. I only want this to happen to his right and left. I got it working kinda but now it only flips when its moving to the other side of the screen.
This is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 1.5f;
    public Vector3 targetPosition;
    public Animator animator;
    private bool isMoving = false;

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            SetTargetPosition();
        }

        if (isMoving)
        {
            Move();
            animator.SetInteger("States", 1);
        } else
        {
            animator.SetInteger("States", 0);
        }
    }

    void SetTargetPosition()
    {
        targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        targetPosition.z = transform.position.z;

        isMoving = true;
    }

    void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPosition), 0.15F);

        Vector2 direction = new Vector2(
           targetPosition.x - transform.rotation.x, 0);

        transform.right = direction;

        if (transform.position == targetPosition)
        {
            isMoving = false;
        }

    }

}

Sorry, meant to reply sooner. The actual problem here is your line

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPosition), 0.15F);

Quaternion.LookRotation takes in a direction and outputs a rotation, you’re simply giving it a position, which is only half a direction. So you want to do this.

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPosition - transform.position), rotationSpeed * Time.deltaTime);