2d Shooter Follow cursor issues,2d Shooter mouse follow issues

I am making a 2d platformer/shooter in Unity C#. I have a script attached to the players pistol which faces it towards the cursor but when the player is flipped (scaled by -1 on the x axis) the pistol inverts the cursors movement. Ive tried using Mathf.Sign but it did not change anything. Here is the code incase it is needed:
public float offset;
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset * Mathf.Sign(transform.localScale.x));
    }

The flipping code:
void Flip()
{
facingRight = !facingRight;
Vector3 scaler = transform.localScale;
scaler.x *= -1;
transform.localScale = scaler;
}
,I am making a game 2d platformer/shooter game in Unity C#. I have a script which faces the players pistol towards the cursor but when the player movement script flips the player (scales x by -1) the cursor follow inverts the mouses movement. Here is the code in case it is needed:

    public float offset;

    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset * Mathf.Sign(transform.localScale.x));
    }

Here is the flipping code:

        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }else if(facingRight == true && moveInput < 0)
        {
            Flip();
        }

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

give a try to rotation

like onflip

rotation = 0,180,0

rotation = 0,0,0

I’m using this class I wrote for my current project, you can use it freely without problems. Just add it to the object that will be looking at the mouse, set the correct axis and hit play. if it’s looking with the wrong side check the bool isFlipAxis. And finally when you flip your player with the scale, just make isFlipAxis the opposite of what you had and it should flip to correctly look at the mouse.


using UnityEngine;

/// <summary>
/// New v2. The gameobject that has this component attached will instantly rotate to make its x or y axis look 
/// towards the assigned target or towards mouse world position if a exposed enum is selected. The direction can be
/// inverted by checking isUseInvertedAxis. Also there is an option to disable local update if a linked control is 
/// needed. It can also use a smooth rotation by enabling isSmoothRotationEnable.
/// </summary>
public class LookAt2Dv2 : MonoBehaviour
{
    // --------------------------------------
    // - 2D TopDown Isometric Shooter Study -
    // ----------- by Tadadosi --------------
    // --------------------------------------
    // ---- https://twitter.com/tadadosi ----
    // --------------------------------------

    [TextArea(4, 10)]
    public string notes = "New v2. The gameobject that has this component attached will instantly rotate to make its x or y axis look " +
        "towards the assigned target or towards mouse world position if a exposed enum is selected. The direction can be inverted by " +
        "checking isUseInvertedAxis. Also there is an option to disable local update if a linked control is needed. It can also use a " +
        "smooth rotation by enabling isSmoothRotationEnable.";

    // TargetTransform: Look at the gameobject Transform from the public variable targetTransform.
    // MouseWorldPosition: Look at the mouse world position stored by the TadaInput class.
    public enum LookAtTarget { TargetTransform, MouseWorldPosition }
    [SerializeField] private LookAtTarget lookAtTarget = LookAtTarget.MouseWorldPosition;

    [Tooltip("If you are using a Transform, select TargetTransform from lookAtTarget dropdown list.")]
    public Transform targetTransform;

    private enum Axis { X, Y }
    [SerializeField] private Axis axis = Axis.Y;

    [Tooltip("Used when isSmoothRotationEnable is true.")]
    [SerializeField] private float turnRate = 10f;

    public float offsetAngle = 20f;

    [Tooltip("Check to let this behaviour be run by the local Update() method and Uncheck if you want to call it from any other class by using UpdateLookAt().")]
    [SerializeField] private bool isUpdateCalledLocally = true;

    [Tooltip("Check to smoothly rotate towards target rotation using turnRate as variable.")]
    public bool isSmoothRotationEnable = false;

    [Tooltip("Check to flip the axis and use the negative side to look at")]
    public bool isFlipAxis = false;

    [Header("Debug")]
    [SerializeField] private Color debugColor = Color.green;
    [SerializeField] private bool debug = false;

    private Vector3 targetPosition;
    private Vector3 direction;
    private Vector3 upwardAxis;

    private void Update()
    {
        if (!isUpdateCalledLocally)
            return;
        UpdateLookAt();
    }

    public void UpdateLookAt()
    {
        #region ---------------------------- TARGET

        Vector3 myPosition = transform.position;

        if (lookAtTarget == LookAtTarget.MouseWorldPosition)
        {
            Vector3 mousePixPos = Input.mousePosition;
            mousePixPos.z = 20f;
            Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePixPos);
            targetPosition = mouseWorldPos;
        }

        else if ((lookAtTarget == LookAtTarget.TargetTransform))
        {
            if (targetTransform == null)
            {
                Debug.LogError(gameObject.name + " target missing!");
                return;
            }
            targetPosition = targetTransform.position;
        }

        // Ensure there is no 3D rotation by aligning Z position
        targetPosition.z = myPosition.z;

        #endregion

        #region ---------------------------- ROTATION

        // Vector from this object towards the target position
        direction = (targetPosition - myPosition).normalized;

        switch (axis)
        {
            case Axis.X:

                if (!isFlipAxis)
                {
                    // Rotate direction by 90 degrees around the Z axis
                    upwardAxis = Quaternion.Euler(0, 0, 90 + offsetAngle) * direction;
                }
                else
                {
                    // Rotate direction by -90 degrees around the Z axis
                    upwardAxis = Quaternion.Euler(0, 0, -90 + offsetAngle) * direction;
                }
                break;

            case Axis.Y:

                if (!isFlipAxis)
                    upwardAxis = direction;
                else
                    upwardAxis = -direction;
                break;

            default:
                break;
        }

        // Get the rotation that points the Z axis forward, and the X or Y axis 90° away from the target
        // (resulting in the Y or X axis facing the target)
        Quaternion targetRotation = Quaternion.LookRotation(forward: Vector3.forward, upwards: upwardAxis);

        if (debug)
            Debug.DrawLine(transform.position, targetPosition, debugColor);

        if (!isSmoothRotationEnable)
        {
            transform.rotation = targetRotation;
            return;
        }
        transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnRate * Time.deltaTime);
        #endregion
    }

    public void SwitchToTargetTransform()
    {
        lookAtTarget = LookAtTarget.TargetTransform;
    }

    public void SwitchToMouseWorldPosition()
    {
        lookAtTarget = LookAtTarget.MouseWorldPosition;
    }
}