- Home /
When fliping the character the rotation of character's weapon goes backward
I have two game objects on the Scene, 1.Character(Parent Object) and 2.Weapon(Child Object) . The problem is when the character is moving to the right side, the rotation of the weapon is fine, it is toward where character is facing and rotating as expected, as you can see in the Gif image attach below. But when i Flip to left side everything goes wrong, the weapon goes backward and when i press down arrow the rotation goes up and when press up arrow the rotation goes down, see the Gif image attach below.. Please help How to Fix it. Here is my Code:
public float weaponRotationSpeed = 13f;
private Animator anim;
private float angle;
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector3 changeParentScale = transform.localScale;
if (hv != Vector2.zero)
{
if (Input.GetAxis("Horizontal") < 0)
{
changeParentScale.x = -5f;
transform.localScale = changeParentScale;
}
else if (Input.GetAxis("Horizontal") > 0)
{
changeParentScale.x = 5f;
transform.localScale = changeParentScale;
}
angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;
transform.Find("Weapon").rotation = Quaternion.Lerp(transform.Find("Weapon").rotation,
Quaternion.Euler(0, 0, angle),
weaponRotationSpeed * Time.deltaTime);
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
Your answer
Follow this Question
Related Questions
Change gameObject rotation towards reflection direction. 1 Answer
Weird flipping gameobject 1 Answer
Flip over an object (smooth transition) 3 Answers
How to rotate a gameobject (Y Axis) via c#? 2 Answers
Sprite disapears when moving left 2 Answers