열차 목업의 내부 확인용 프로젝트
smchoi
2024-07-31 9fe33c1ae076b0f6501619388e6b4cc872b76f80
Assets/LevelController.cs
@@ -15,6 +15,8 @@
    public float sensitivity = 0.2f;  // 마우스 움직임에 대한 민감도
    private Vector3 rotationAxis;  // 회전축 (기본값: X축)
    private float currentAngle;
    private Vector3 lastMousePosition;
    /// <summary>
@@ -60,15 +62,16 @@
            downTargetRos = q * Quaternion.Euler(0, maxAngle, 0);
            upTargetRos = q * Quaternion.Euler(0, minAngle, 0);
        }
        currentAngle = GetCurrentAngle();
    }
    public void OnDrag()
    {
        isDragging = true;
        startMouseY = Input.mousePosition.y;
        startAngle = transform.localRotation.eulerAngles.z;
        if (startAngle > 180) startAngle -= 360;  // -180에서 180 사이의 각도로 변환
        lastMousePosition = Input.mousePosition;
        currentAngle = GetCurrentAngle(); // 현재
    }
    public void OffDrag()
@@ -84,46 +87,62 @@
    private void Update()
    {
        if (isDragging)
        {
            float mouseDelta = Input.mousePosition.y - startMouseY;
            float newAngle = startAngle - mouseDelta * sensitivity;
            // 레버의 각도를 제한된 범위 내로 조정
            Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
            float angleDelta = mouseDelta.y * sensitivity;
            float newAngle = currentAngle - angleDelta;
            newAngle = Mathf.Clamp(newAngle, minAngle, maxAngle);
            Vector3 currentRotation = transform.localRotation.eulerAngles;
            if (RotationAxis == Axis.forward)
            {
                currentRotation.z = newAngle;
            }
            else if (RotationAxis == Axis.right)
            {
                currentRotation.x = newAngle;
            }
            else if (RotationAxis == Axis.up)
            {
                currentRotation.y = newAngle;
            }
            // 새로운 회전 적용
            transform.localRotation = Quaternion.Euler(currentRotation);
            ApplyRotation(newAngle);
            lastMousePosition = Input.mousePosition;
            currentAngle = newAngle;
        }
        if (isRun)
        {
            time = Time.deltaTime * speed;
            if (isDown)
            {
                transform.localRotation = Quaternion.Slerp(transform.localRotation, upTargetRos,time );
                transform.localRotation = Quaternion.Slerp(transform.localRotation, upTargetRos, time);
            }
            else
            {
                transform.localRotation = Quaternion.Slerp(transform.localRotation, downTargetRos, time);
            }
        }
        }
    }
    private float GetCurrentAngle()
    {
        Vector3 currentRotation = transform.localRotation.eulerAngles;
        float angle = 0f;
        if (RotationAxis == Axis.forward)
            angle = currentRotation.z;
        else if (RotationAxis == Axis.right)
            angle = currentRotation.x;
        else if (RotationAxis == Axis.up)
            angle = currentRotation.y;
        // -180에서 180 사이의 각도로 변환
        if (angle > 180) angle -= 360;
        return angle;
    }
    private void ApplyRotation(float angle)
    {
        Vector3 currentRotation = transform.localRotation.eulerAngles;
        if (RotationAxis == Axis.forward)
            currentRotation.z = angle;
        else if (RotationAxis == Axis.right)
            currentRotation.x = angle;
        else if (RotationAxis == Axis.up)
            currentRotation.y = angle;
        transform.localRotation = Quaternion.Euler(currentRotation);
    }
}