The object I’m trying to rotate is also following the mouse movement along the y axis. It does rotate along the z axis, but only when it reaches the edge of the screen. I’m wanting the object to always be facing the mouse so that it appears to be looking at it.
Here is the code I’m using
Vector3 objPos= new Vector3 (2.5f, this.transform.position.x, 0 * moveSpeed);
float mousePos = Input.mousePosition.y / Screen.height * 12f;
ninjaPos.y = Mathf.Clamp (mousePos, 1.25f, 10.75f);
this.transform.position = objPos;
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);