I separated the main_body sprite from the arm sprite and made a script for body flip and another for arm rotation and flip, but now this happens:
If i make arm sprite child of main_body sprite i get this:
Here is my arm_rotation code:
public class ArmRotation : MonoBehaviour
{
SpriteRenderer spriteRend;
public Transform Firepoint;
void Awake()
{
spriteRend = GetComponent<SpriteRenderer>();
}
void Update()
{
AimArmAtMouse();
}
void AimArmAtMouse()
{
Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition); //get the mouse position as a Vector2
Vector2 armToMouse = mousePosition - (Vector2)transform.position; //get the vector from the arm to the mouse
float rotationZ = Vector2.SignedAngle(transform.right, armToMouse); //calculates the angle
transform.Rotate(0f, 0f, rotationZ); //rotates the transform by that angle
FlipArm(Vector2.SignedAngle(transform.right, Vector2.right)); //calls the FlipArm method and passes in the angle of the arm relative to the world
}
void FlipArm(float rotation)
{
if (rotation < -90f || rotation > 90f)
{
spriteRend.flipY = true;
FlipFirePoint(true);
}
else
{
spriteRend.flipY = false;
FlipFirePoint(false);
}
}
void FlipFirePoint(bool flip)
{
var pos = Firepoint.localPosition;
pos.y = Mathf.Abs(pos.y) * (flip ? -1 : 1);
Firepoint.localPosition = pos;
}
}
and my body rotation code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BodyRotation : MonoBehaviour
{
void Update()
{
Flip();
}
void Flip() //Flips player based on mouse's position relative to it's center.
{
Vector3 theScale = transform.localScale;
Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
float WorldXPos = Camera.main.ScreenToWorldPoint(pos).x; //Converts screen point to point in world
if (WorldXPos > gameObject.transform.position.x)
{
theScale.x = 1;
transform.localScale = theScale;
}
else
{
theScale.x = -1;
transform.localScale = theScale;
}
}
}
Any sugestion on how i can fix my problem? Thank you!