Putar objek ke Unity posisi mouse


public class RotateToMouse : MonoBehaviour {
    // public variables
    public float speed = 1.0f; // Set it to whatever you want
    public float distance = 10.0f; // Set it to whatever you want

    // private variables
    private Vector3 mousePos;
    private Vector3 objectPos;
    private float angle;

    // Use this for initialization
    void Start () {
        // get the mouse position
        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));
    }

    // Update is called once per frame
    void Update () {
        // get the mouse position
        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));
    }

    // rotate the object to the mouse position
    void FixedUpdate() {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // rotate the object to the mouse position (again)
    void LateUpdate() {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // end of class
}
Old-fashioned Otter