Hi, for learning purposes I’m trying to make a game in Unity.
I have a question about something that I got to work, but don’t understant why wasn’t working previously.
One of the things that I want is that enemies look at (rotate towards) the maincharacter.The code that you find in tutorials for that purpose is:
enemyTransform.rotation=Quaternion.Slerp(enemyTransform.rotation,Quaternion.LookRotation(player.position - enemyTransform.position),rotSpeed);
Which works and makes enemies rotate in all 3 axis.
I wanted them to rotate just on the y axis so I tried the obvious:
enemyTransform.rotation.y=(Quaternion.Slerp(enemyTransform.rotation,Quaternion.LookRotation(player.position - enemyTransform.position),rotSpeed)).y;
But that did’t (completely) work, they rotate towards the player, but just for (about) 180°, while I’m in the other 180° they just stay still. (if I walk around them clockwise or counterclockwise the “blind spot” isnotthe same).
After a while of wondering what could be happening I decided to try and do this:
enemyTransform.rotation=Quaternion.Slerp(enemyTransform.rotation,Quaternion.LookRotation(player.position - enemyTransform.position),rotSpeed;
enemyTransform.rotation.x=0;
enemyTransform.rotation.z=0;
And it works perfect, but what’s bothering me is that both ways seem correct (and the first seems better because it uses less operations) so, why isn’t the first working? What am I missinghere?
Thanks fortaking the time to read.