Script gives wrong rotation.

So,i have 3D Character in 2D Space,all i wanted to do is to make character HEAD fallow to my mouse direction,so all works PERFECT,untill i start the game,so what happens is that the character head instantly turns up (90 degrees). Anyway the head fallows my mouse but head rotation is not not accurate to mouse position,everything is messed up,head rotatates wrong!

    #pragma strict
     
    var mouse_pos : Vector3;
    var target : Transform; //Assign to the object you want to rotate
    var object_pos : Vector3;
    var angle : float;
     
    function LateUpdate ()
    {
    mouse_pos = Input.mousePosition;
    mouse_pos.z = 15; //The distance between the camera and object
    object_pos = Camera.main.WorldToScreenPoint(target.position);
    mouse_pos.x = mouse_pos.x - object_pos.x;
    mouse_pos.y = mouse_pos.y - object_pos.y;
    angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
    }

I hope that you understand me,please help me out!

i dont even want to look at
Quaternion.LookRotation cause i know
its not gonna work.

Just because you don’t know how it works doesn’t mean it won’t work, you are in luck because I felt like proving you wrong :stuck_out_tongue:

private var mouse_pos : Vector3;
var target : Transform;
private var dirUp : Vector3;
private var dirFwd : Vector3;
private var zDistance : float;

function Start(){
	zDistance = target.position.z-Camera.main.transform.position.z;
}

function Update(){
	mouse_pos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, zDistance));
	dirFwd = mouse_pos-target.position;
	dirUp = Vector3(-dirFwd.y, dirFwd.x, 0);
}

function LateUpdate (){
	target.rotation = Quaternion.LookRotation(Vector3.forward*Mathf.Sign(dirFwd.x), dirUp*Mathf.Sign(dirFwd.x));
}

Scribe