I’ve adapted a script from these forums to make an object rotate to face the mouse cursor, acting as if the screen and the relevant game object are on the same 2D plane. It does the job, rotating around X as I’d like it to, but it flips its Y rotation at the top and bottom extremes. In other words, when the mouse pointer crosses the centre of the screen from right to left, the Y rotation switches from 0 to 180 degrees and vice versa. This creates a visual problem in that the object keeps inverting and prevents me from having a reliable rotation value to build on for other effects.
Any insights would be greatly appreciated.
var mousePos : Vector3;
var target : Transform; //Assign to the object you want to rotate (self)
var objectPos : Vector3;
function Update (){
mousePos = Input.mousePosition;
mousePos.z = 0; //The distance between the camera and object
objectPos = Camera.main.WorldToScreenPoint(target.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
transform.LookAt(mousePos);
transform.position.z = 0;
}