LookAt only X and Z positions

In my game I have been trying to make this line:

transform.LookAt(player.transform.position)

So it only looks at the x and z axis, I’ve tried some experiments and the only solution I found is to make an object that follows the player on the x and z axis and the object that has to look at the player on the y axis, is there a simpler way?

You can cache the initial rotation and apply one of those axis after the LookAt.
Or code your own LookAt, just as chatgtp for the code

one lazy hack is also

  • take player position into vector3
  • set player.y = transform.y (so that they are in the same level)
  • then lookat() into that adjusted position (so it shouldn’t look up or down now)