- Home /
Question by
danilkomarov243242 · Mar 07, 2021 at 07:14 AM ·
c#unity4unity4.3
The enemy looks at the player incorrectly from behind when he is behind
My enemy doesnt look at player properly when he's behind him. Like it's shown on gif
But when i stay ahead of enemy he looks at me properly, but as soon as I stay behind him like it's shown on gif he doesn't rotate to me and starts looking at me incorectly . I've tried many ways to fix it, but nothing seems to work as i want. So here's my code, I don't know what's wrong with it .
private void RotateToTarget()
{
Quaternion enemyRotation;
Vector3 directionTolook = (player.transform.position - transform.position);
angle = Mathf.Atan2(directionTolook.y, directionTolook.x) * Mathf.Rad2Deg;
enemyRotation = Quaternion.Euler(0f, angle, 0f);
transform.rotation = Quaternion.Slerp(transform.rotation, enemyRotation, rotationSpeed * Time.deltaTime);
}
Thanks in advance!
problem.gif
(147.4 kB)
Comment
Answer by HoweToGaming · Mar 07, 2021 at 12:43 PM
Would lookAt not be better
transform.LookAt(player.transform.position);
I tried this one. But i need my enemy to look at player straight with his hands pointing at him
Your answer
![](https://koobas.hobune.stream/wayback/20220613030330im_/https://answers.unity.com/themes/thub/images/avi.jpg)