So this is basically a 2D situation, where you want to keep your objects in X and Y and rotate only around Z.
LookAt’s not really made for that; it assumes that you want to rotate around the object’s local Y axis. You could probably make it work by jumping through some hoops, but…
For this sort of thing, I usually use this method:
/// <summary>
/// Returns the signed angle between this 3D vector and another,
/// with respect to some orthogonal "up" vector. If looking in
/// the "up" direction, then + angles are counter-clockwise.
/// </summary>
/// <returns>The signed angle, in degrees, from A to B.</returns>
/// <param name="a">Vector this was called on.</param>
/// <param name="b">Vector to measure the angle to.</param>
public static float SignedAngleTo(this Vector3 a, Vector3 b, Vector3 up) {
return Mathf.Atan2(
Vector3.Dot(up.normalized, Vector3.Cross(a, b)),
Vector3.Dot(a, b)) * Mathf.Rad2Deg;
}
This lets you find the angle between two vectors, as rotated around anything axis you like. In your case you’d do something like:
float angle = Vector3.right.SignedAngleTo(targetPosition - transform.position, Vector3.forward);
Specifying Vector3.forward for the last parameter tells the function to consider the forward vector (i.e. 0,0,1) to be “up” for the purposes of this angle measurement. And then it returns the degrees between right (i.e. 1,0,0) and a vector that points at the target.
Then you would just set the rotation of your object accordingly, e.g. transform.eulerAngles = new Vector3(0, 0, angle).
HTH,