Unity/Unity 리서치

마우스 드래그로 Camera 회전

lipnus 2021. 7. 29. 23:44
반응형
    private Vector2 movePosDiff;
    private enum MouseState { Begin, Moved, Cancel }
    private MouseState mouseState = MouseState.Cancel;

    private float mouseDragSpeed = 3f;
    void Update() {
        RotateCamera( GetMouseDragValue() );
    }

    private void RotateCamera(Vector2 diff) {
        Vector3 nextEulerAngles = mainCamera.transform.rotation.eulerAngles;
        nextEulerAngles.y -= diff.x * mouseDragSpeed;

        mainCamera.transform.rotation = Quaternion.Euler(nextEulerAngles);
    }

    private Vector2 GetMouseDragValue() {
        movePosDiff = Vector3.zero;

        if(Input.GetMouseButtonDown(0)) {
            if(mouseState == MouseState.Cancel) {
                mouseState = MouseState.Moved;
            }
        }

        if(Input.GetMouseButtonUp(0)) {
            mouseState = MouseState.Cancel;
        }

        if(mouseState == MouseState.Moved) {
            Debug.Log($"#### Moved ({Input.GetAxis("Mouse X")}, {Input.GetAxis("Mouse Y")})");
            movePosDiff = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));    
        }

        return movePosDiff;

 

마우스로 화면 끌어서 회전 POC

반응형