I’m gonna provide you with a Gif of the Problem. The problem seems to be that the Pivot of the gun is attached to the Player, and the flip in the CharacterController script seems to double flip the gun. But I honestly have no clue how to fix it. I’ll provide you with the code aswell.
public float speed = 5f;
public Rigidbody2D rb;
bool facingRight = true;
Vector2 move;
public Animator animator;
void Update()
{
move.x = Input.GetAxisRaw("Horizontal");
move.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Speed", move.sqrMagnitude);
// using mousePosition and player's transform (on orthographic camera view)
var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
if (delta.x >= 0 && !facingRight)
{ // mouse is on right side of player
transform.localScale = new Vector3(1, 1, 1); // or activate look right some other way
facingRight = true;
}
else if (delta.x < 0 && facingRight)
{ // mouse is on left side
transform.localScale = new Vector3(-1, 1, 1); // activate looking left
facingRight = false;
}
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + move.normalized * speed * Time.fixedDeltaTime);
}
Thats the character Controller
public GameObject myPlayer;
private void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ< -90 || rotationZ> 90)
{
if(myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
} else if (myPlayer.transform.eulerAngles.y == 180) {
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
And thats the gun pivot script, the gun sprite being attached to the pivot point which itself is attached to the Player object.
I would be really thankful for some help. Thanks in advance.