Fliping character

Hello, im making a 2D shooter game were the gun rotates 360 around the player. But when i flip the character depending on the direction im walking the whole gun flips and inverts the controls. The gun aims at the mousepointer.

This is my gun rotate script. It sits on a gameObject behind the player that is a child of the player. The problem is that i flip the player and that flips all its children aswell.
Im wondering if there is anyway i can prevent it.

public class AimRotate : MonoBehaviour
{

    [SerializeField]
    public GameObject player;
    void Start()
    {
       
    }
   
    void Update()
    {

        Vector3 mousepos = Input.mousePosition;
        Vector3 gunposition = Camera.main.WorldToScreenPoint(transform.position);
        mousepos.x = mousepos.x - gunposition.x;
        mousepos.y = mousepos.y - gunposition.y;
        float gunangle = Mathf.Atan2(mousepos.y, mousepos.x) * Mathf.Rad2Deg;
        if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < transform.position.x)
        {
            transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -gunangle));
        }
        else
        {
            transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, gunangle));
        }
    }
}

This is the movement script

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

public class PlayerMovement : MonoBehaviour
{

    [SerializeField]
    public float movementSpeed;
   
    [SerializeField]
    public float jumpForce;
   
    [SerializeField]
    public float sprintSpeed;
   
    [SerializeField]
    public float fallSpeed;

    private Rigidbody2D _rigidbody;

    private float timer;
   
    public bool isGrounded = false;
    private bool facingRight = true;

    void Start()
    {
        _rigidbody = gameObject.GetComponent<Rigidbody2D>();
    }
   
    void Update()
    {
        Movement();
        Jump();
        Sprint();
       
       
    }

    public void Movement()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position += movement * Time.deltaTime * movementSpeed;
       
        if (facingRight == false && movement.x > 0)
        {
            Flip();
        }
        else if(facingRight == true && movement.x < 0)
        {
            Flip();
        }
    }

    public void Jump()
    {
        if (Input.GetButtonDown("Jump") && isGrounded == true)
        {
            _rigidbody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
        }

        if (_rigidbody.velocity.y < 0)
        {
            _rigidbody.velocity += Vector2.up * (Physics2D.gravity.y * (fallSpeed - 1) * Time.deltaTime);
        }
       
    }

    public void Sprint()
    {
        if (Input.GetKey(KeyCode.LeftShift) && isGrounded == true)
        {
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
            {
                var sprint = Input.GetAxis("Horizontal");
                transform.position += new Vector3(sprint, 0, 0) * (Time.deltaTime * sprintSpeed);
            }
        }
    }

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

@Nilskam178

One way to deal with this: don’t parent the gun to player character sprite, make them both have a common root object. When you flip the character sprite, switch the weapon to L/R hand location.

Care to explain, im quite new to unity and C#. Moved from Java

@Nilskam178
“Care to explain, im quite new to unity and C#. Moved from Java”

I’m going to pretty much repeat myself…

This is nothing specific to Unity, just your typical 3D rigging stuff you would do in 3D software… but in Unity you’ll have to use Gameobjects to build your hierarchy. If you are unfamiliar with this, check some basics tutorials.

But first have a root Gameobject. Make it have your scripts perhaps. Doesn’t matter. Add sprite as a child. Then add another child as child of root object. This will be your weapon, or empty Gameobject where you parent your weapon.

This way you can transform your sprite anyway you like (or use its flip feature) as it has no children that would follow it when transformed. Then you’ll have to simply have some method to move your weapon/weapon root when you flip from your default character facing direction, as it isn’t going to be moved by character flipping (which is what you wanted I guess).

Dude, thanks. Works like a charm. I thought of this before but actually forgot about it. Thanks for reminding me!