- Home /
2D head breaks when flipping character
Hi!
I have an enemy in my 2D game that rotates it's head towards the player and when the player move to the right side of the enemy the head gets inverted or something. I can't really explain it very well. Like when the player goes up, the head tilts down and when the player goes down the head tilts up.
Here is the code:
Vector3 headDifference = player.transform.position - head.transform.position;
headDifference.Normalize();
float rotZ = Mathf.Atan2(headDifference.y, headDifference.x) * Mathf.Rad2Deg;
if(playerIsOnTheRight == true)
{
head.transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
} else
{
head.transform.rotation = Quaternion.Euler(0f, 0f, rotZ + 180);
}
Do anyone know what I could do to fix this?
Thanks in advance!
Comment