- Home /
Question by
buttblaster · Feb 08 at 10:20 PM ·
unity 2danglelocalrotationflippinglookatmouse
Angles invert after flipping player by 180 on Y-axis in 2D.
I wanted to implement looking at the cursor using this code:
private void LookAtCursor()
{
Vector2 vectorTowardsCursor = (cursor.position - headTransform.position).normalized;
float vectorAngle = Mathf.Atan2(vectorTowardsCursor.y, vectorTowardsCursor.x) * Mathf.Rad2Deg;
if ((cursor.position.x > transform.position.x && !facingRight) ||
(cursor.position.x < transform.position.x && facingRight))
{
Flip();
}
float smoothedAngle = Mathf.SmoothDampAngle(headTransform.eulerAngles.z, vectorAngle, ref
refRotation, headSmoothing * Time.deltaTime);
headTransform.localRotation = Quaternion.Euler(0f, 0f, smoothedAngle);
}
and the problem arises when I try to flip 180 degrees on Y-axis using this code:
private void Flip()
{
facingRight = !facingRight;
gfxTransform.Rotate(0f, 180f, 0f);
}
the angle gets inverted and offset by 180 degrees. I tried subtracting 180 degrees to "vectorAngle" variable but it only solves the offset problem. Inverting still persists. I have tried using "headTransform.rotation" before, which solves the issue of inverting but it does not want to flip with it's parent. Thank you for anyone willing to help!
Comment