transform.LookAt but only on a specific axis?

I’m trying to make a very simple enemy that’s always facing the player. But transform.LookAt looks at it on all of the axis, where I want it to just be the y axis.
I tried this

transform.LookAt(new Vector3(target.transform.eulerAngles.x, transform.position.y, target.transform.eulerAngles.z));

but it has a problem where for some reason it’s not updating when the player moves. The enemy is always staring at the position where the player started and not where the player is. I’ve checked to make sure the target variable is correct and that the target transform is where I want it to be. I don’t know what causes the bug. I also added a log statement to see if it’s running sure enough it’s running each frame.

Just get the direction from A to B, zero out the y component, then use Quaternion.LookRotation to get your rotation.

You’re using LookAt wrong. It’s a super-shortcut made to be easy to use. The input is the position to make yourself look at, like: Vector3 pos; ... transform.LookAt(pos);. And as Spiny wrote, pos can be the actual thing to look at, or you can adjust it to be flat on the ground, or where-ever.

Ya know I don’t know why I didn’t think about the fact that since the player and the enemy are on the same y axis it doesn’t matter that it’ll adjust the x and z axis. Just using LookAt normally fixed the problem. Thanks for the help.