Rotating objects with quaternions

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.

I’m not even entirely sure myself how the quaternion rotation works, but what I’ve come to learn is that when your’e dealing with rotation manually, you are better off with using Euler. That is my experience at least.

E: Also it is suggested that you never change the angle values one by one like that.
To fix it up a litte, I guess something like this

enemyTransform.rotation.=Quaternion.Slerp(enemyTransform.rotation,Quaternion.Euler(0,Quaternion.LookRotation(player.position - enemyTransform.position).eulerAngles.y,0),rotSpeed);

should work.
I haven’t tested the code, so it might be faulty, but you get the general idea.

E: Answer to why one code works and the other doesn’t is that there is a 4th variable for a quaternion angle.
There is also the w variable.

Well thanks, your code works just fine.

but about the first thing I tried, I hadn’notice that the rotation was a quaternion as well,so it must indeed be the w.