The Z axis is depth in a 2D game. It is known as the “forward” axis in Unity. In 2D games however, generally it is the object’s X axis that represent its forward direction.
2D objects are like pieces of paper on a table. To rotate the paper, you dont flip it over (rotate on x axis or y axis), you can only turn it while keeping it flat on the table (z axis rotation). You can observe this in the editor by rotating sprites on each axis. (exceptions being like, the paper mario turning effect)
You can use LookRotation, but you have to understand what that function is asking you for, and what it does to your object in order to use it properly.
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
The function wants you to tell it where the 3D forward axis (z) and the up axis (y) should be pointing, and it will give you a rotation with the axes pointing in those directions. It does not handle the X axis directly, it will set Z and Y, and the X will lie where it falls. (90 degrees away from both of those axes)
Keep in mind that it’s usually used to point a 3D object’s “forward” axis (Z) at something, where the object’s “up” axis is assumed to be (0,1,0), which in 3D is up towards the sky. If you don’t pass an up vector, it will default to (0,1,0).
Since you want your object to point its X axis towards another object, we have to supply the correct Z and Y axes to make that happen.
3D “Forward” Z Axis:
A 2D object is always flat on the XY plane, so the Z axis always points forward perfectly, so we know that we want the Z axis to end up as (0,0,1), or Vector3.forward for shorthand.
3D “Up” Y Axis:
We know that the Y axis is always 90 degrees away from the X axis (which is what we want to end up pointing at the target), so we can either point the Y axis at the target, then rotate 90 degrees counterclockwise afterwards, or rotate first and then pass in the rotated Y direction. I’m going to show you the second option.
Vector3 myLocation = transform.position;
Vector3 targetLocation = path.lookPoints[pathIndex];
targetLocation.z = myLocation.z; // ensure there is no 3D rotation by aligning Z position
// vector from this object towards the target location
Vector3 vectorToTarget = targetLocation - myLocation;
// rotate that vector by 90 degrees around the Z axis
Vector3 rotatedVectorToTarget = Quaternion.Euler(0, 0, 90) * vectorToTarget;
// get the rotation that points the Z axis forward, and the Y axis 90 degrees away from the target
// (resulting in the X axis facing the target)
Quaternion targetRotation = Quaternion.LookRotation(forward: Vector3.forward, upwards: rotatedVectorToTarget);
// changed this from a lerp to a RotateTowards because you were supplying a "speed" not an interpolation value
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
You just have to work with the function in the way it’s meant to be used. There are other ways to avoid this function and just do the math, but I find it’s easier to read & understand this way, using a very common function.
I hope you managed to follow that, if you have more questions let me know.