Hey more-experienced developers!
I’ve been working on a top-down 3d game lately, and I would like to make my character (which is a game object) to look at my cursor, rotating on the Y axis. This is the code I’ve been trying to use:
#pragma strict
var target : Transform;
function Start () {
}
function Update () {
var mouse_pos : Vector3;
var object_pos : Vector3;
var angle : float;
mouse_pos = Input.mousePosition;
mouse_pos.y = 5.23; //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.z = mouse_pos.y - object_pos.z;
angle = Mathf.Atan2(mouse_pos.z, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
For some reason, it wants to rotate on my X axis, and I don’t know why. Any help would be appreciated.