Hello, like title says, i need some help understanding some code.
I’m making basic 2D game and i needed a script to move player’s hand in the direction of the mouse position. I copied some code from the internet and it worked well until i decided that i want my player to look at the direction which he is moving. (right or left ). The way my script works is i have a pivot to which hand is attached and hand rotates around it. Here is the first piece of code :
public GameObject Player;
public Vector3 diffrerence;
private void FixedUpdate()
{
diffrerence = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diffrerence.Normalize();
float rotationZ = Mathf.Atan2(diffrerence.y, diffrerence.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (Player.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (Player.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
Everything is fine until i created this piece of code to the player’s movement script:
if (horizontal > 0)
{
transform.localScale = new Vector3(scale, transform.localScale.y, transform.localScale.y);
}
else if (horizontal < 0)
{
transform.localScale = new Vector3(-scale, transform.localScale.y, transform.localScale.y);
}
When local scale is negative, player changes his direction but hand’s rotation is completely broken.
It’s like inverted ? Here is the quick showcase of how it looks :

So basically i know something is wrong with the rotation part of the script but i don’t know what so i would appreciate if someone would explain how it works or would give me solution to the problem.