- Home /
2D enemy look at player, but he flips upside down
wondering if anyone can help me. My enemy dashes towards the player, and I want the enemy to face the player when I do so.
I'm doing that now by doing transform.right = (player.transform.position - transform.position).normalized;
when the enemy is facing right, it works fine, but if the enemy is facing left (I have a flip rotation that rotates him 180f on the Y axis to look left) he ends up going upside down, like this:
anyone know how to avoid this? Rotations drive me nuts lol.
Answer by jeff13gaydos · Apr 22 at 02:48 AM
To fix your issue directly, I think you need to apply a 180 rotation in the x direction (which would be something like:
transform.Rotate(new Vector3(180.0f, 0.0f, 0.0f));
But the possibly better solution (and maybe easier since you don't like rotations) could be to just scale the enemy to -1 on the x axis.
transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
thanks. By only applying the transform.Rotate it reversed my problem. The enemy flipped when moving in the right direction.
So I created a bool to keep track of his facing direction, and was able to get it working with:
transform.right = -directionToPlayer;
if (!isFacingRight)
{
transform.Rotate(new Vector3(180.0f, 0.0f, 0.0f));
}