public class RotateToMouse : MonoBehaviour {
public float speed = 1.0f;
public float distance = 10.0f;
private Vector3 mousePos;
private Vector3 objectPos;
private float angle;
void Start () {
mousePos = Input.mousePosition;
mousePos.z = distance;
objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
void Update () {
mousePos = Input.mousePosition;
mousePos.z = distance;
objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
void FixedUpdate() {
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
void LateUpdate() {
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}