열차 목업의 내부 확인용 프로젝트
smchoi
2024-07-31 9fe33c1ae076b0f6501619388e6b4cc872b76f80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using UnityEngine;
using UnityEngine.InputSystem.Controls;
 
public class LeverController : MonoBehaviour
{
    public enum Axis
    {
        forward,
        right,
        up,
    }
    public Axis RotationAxis;
    public float maxAngle = 45f;  // ·¹¹öÀÇ ÃÖ´ë °¢µµ
    public float minAngle = -45f;  // ·¹¹öÀÇ ÃÖ¼Ò °¢µµ
    public float sensitivity = 0.2f;  // ¸¶¿ì½º ¿òÁ÷ÀÓ¿¡ ´ëÇÑ ¹Î°¨µµ
    private Vector3 rotationAxis;  // È¸ÀüÃà (±âº»°ª: XÃà)
 
    private float currentAngle;
    private Vector3 lastMousePosition;
 
 
    /// <summary>
    /// µå·¡±× °¡´É ¿©ºÎ
    /// </summary>
    private bool isDragging = false;
    /// <summary>
    /// ·¹¹ö°¡ ¹Ø¿¡ ÀÖ´ÂÁö ¾Æ·¡ÀÖ´ÂÁö ÆÇ´Ü
    /// </summary>
    private bool isDown = false;
    private bool isRun = false;
    private float startMouseY;
    private float startAngle;
 
 
    private int layerMask;
 
 
    private  float time = 0;
    private Quaternion downTargetRos;
    private Quaternion upTargetRos;
    private float speed = 10;
    private void Start()
    {
        Quaternion q = transform.localRotation;
        layerMask = 1 << LayerMask.NameToLayer("Train");
        if (RotationAxis == Axis.forward)
        {
            rotationAxis = Vector3.forward;
            downTargetRos = q * Quaternion.Euler(0,0, maxAngle);
            upTargetRos =q * Quaternion.Euler(0, 0, minAngle);
            
        }
        else if (RotationAxis == Axis.right)
        {
            rotationAxis = Vector3.right;
            downTargetRos = q * Quaternion.Euler(maxAngle, 0, 0);
            upTargetRos = q * Quaternion.Euler(minAngle, 0, 0);
        }
        else if (RotationAxis == Axis.up)
        {
            rotationAxis = Vector3.up;
            downTargetRos = q * Quaternion.Euler(0, maxAngle, 0);
            upTargetRos = q * Quaternion.Euler(0, minAngle, 0);
        }
 
        currentAngle = GetCurrentAngle();
    }
 
 
    public void OnDrag()
    {
        isDragging = true;
        lastMousePosition = Input.mousePosition;
        currentAngle = GetCurrentAngle(); // ÇöÀç 
    }
 
    public void OffDrag()
    {
        isDragging = false;
    }
 
    public void OnClick()
    {
        isRun = true;
        isDown = !isDown;
    }
 
    private void Update()
    {
        if (isDragging)
        {
            Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
            float angleDelta = mouseDelta.y * sensitivity;
            float newAngle = currentAngle - angleDelta;
            newAngle = Mathf.Clamp(newAngle, minAngle, maxAngle);
 
            ApplyRotation(newAngle);
 
            lastMousePosition = Input.mousePosition;
            currentAngle = newAngle;
        }
        if (isRun)
        {
            time = Time.deltaTime * speed;
            if (isDown)
            {
                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);
    }
}