Why does my rotation turn negative over the Y axis boundary (2d)?

I am struggling to understand what causes this flip and looking for some advice on how to correct it.

Quaternion newRotation = Quaternion.LookRotation(target.transform.position
- transform.position, Vector2.right);
            newRotation.x = 0;
            newRotation.y = 0;
            target.transform.rotation = Quaternion.Slerp(target.transform.rotation, newRotation, Time.deltaTime * 8);

Here, everything is as expected.

75559-1.jpg

Here, the angle flips.

75560-2.jpg

Thanks for reading.

The newRotation.y is not the representation of rotation of a gameobject in degree. I think you should work with newRotation.eulerAngles.

it will be like this:

newRotation.eulerAngles = new Vector3(0,0,newRotation.eularAngles.z);

The cause of the flip is most likely because you are changing directly the coordinates of a Quaternion,
and these coordinates, even though they are related to a rotation of an object, they are not directly angle values. Therefore the following lines:

newRotation.x = 0;
newRotation.y = 0;

are not successfully doing what you are expecting, which I guess is to zero out rotations around the axis x and y, while looking at your target. One way you can achieve this “look-at”
functionality restricted to a plane, is to use the Vector3.ProjectOnPlane method :

using UnityEngine;
using System.Collections;

public class LookAtOnPlaneTest : MonoBehaviour {

	public Transform target;

	void Update () {
		//change as needed (here rotate around y axis)
		Vector3 rotationAxis = Vector3.up; 

		//project direction to target -> into the rotation plane 
		Vector3 projDirToTarget = Vector3.ProjectOnPlane (target.position - transform.position, rotationAxis);

		//get the rotation for looking into the projected direction
		Quaternion newRotation = Quaternion.LookRotation (projDirToTarget, rotationAxis);

		//animate rotation of character to the new rotation
		transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime);
	}
}

the ProjectOnPlane method will do what you want of projecting the dirToTarget into the plane with normal equal to rotationAxis, and you look-at that projected vector.