- Home /
I need help understanding piece of code
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.
Answer by Rain478293 · Jul 04, 2021 at 02:12 PM
Hi @Nistroy Thank you for your answer. I decided not to use SpriteRenderer because it doesn't change collider position but i used your advice to detach hand from the rest of the body. I made simple script that allows hand to have same transform as the player, with the offset available. I also checked if player is looking left or right by getting information about Horizontal Axis. This allowed me to change the offset to match the position i want to have hand on my player. Here is code for my solution :
private void Update()
{
transform.position = new Vector3(Player.transform.position.x + xOffset, Player.transform.position.y + yOffset, transform.position.z);
if(Player.GetComponent<MovementScript>().horizontal > 0)
{
xOffset = -0.07f;
}
else if (Player.GetComponent<MovementScript>().horizontal < 0)
{
xOffset = 0.07f;
}
}
and this is the piece from other script related to this:
horizontal = Input.GetAxisRaw("Horizontal");
Answer by Nistroy · Jul 04, 2021 at 09:17 AM
Hi @Rain478293 ! Is the hand a child object of the character's base artwork? If so, then drag it so that the rotation of the character does not affect the rotation of the hand. If you ever want a child object of the base design to remain, then you need to flip the SpriteRenderer like this:
if (horizontal > 0)
{
spriteRenderer.flipX = false;
}
else if (horizontal < 0)
{
spriteRenderer.flipX = true;
}