2020년 1월 10일 금요일

Unity 멀티터치 줌인아웃 ,이동 ,구역제한

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMoveCam : MonoBehaviour
{
    // Start is called before the first frame update
    public float perspectiveZoomSpeed = 0.5f;        // The rate of change of the field of view in perspective mode.
    public float orthoZoomSpeed = 0.5f;
    public Camera camera;
    public float movespeed = 2f;
    Vector2 prevPos = Vector2.zero;
    public float minPosX = -10;
    public float maxPosX = 10;
    public float minPosZ = -10;
    public float maxPosZ = 10;
    //영역제한용
    public float xOffset; 
    public float yOffset;
    public float ZoomInMin;
    public float ZoomInMax;
    public bool showGizmo = true;
    bool isLimited;
    float fv;
    void Start()
    {
        Screen.orientation = ScreenOrientation.Portrait;
        camera.orthographicSize = ZoomInMax;
    }
    public void SpeedChangeZoomPer()
    {
        movespeed = (ZoomInMax - camera.orthographicSize) * 1f+1000f;
    }
    void Update()
    {
        SpeedChangeZoomPer();
        
        if (Input.touchCount == 1)
        {
            Calculate(fv);
            if (prevPos == Vector2.zero)
            {
                prevPos = Input.GetTouch(0).position;
                
                return;
            }
            Vector2 dir = (Input.GetTouch(0).position - prevPos).normalized;
            Vector3 vec = new Vector3(dir.x, dir.y, 0);
            Vector3 tmp = camera.transform.position;
            tmp -= vec * movespeed * Time.deltaTime;
            Debug.Log("dir" + dir);
            switch (Input.GetTouch(0).phase)
            {
                case TouchPhase.Moved:
                    if (dir.x != 0)
                    {
                        if (maxPosX >= tmp.x && tmp.x >= minPosX)
                        {
                            camera.transform.position = tmp;
                        }
                        //좌우 움직임
                    }
                    if (dir.y != 0)
                    {
                        if (maxPosZ >= tmp.y && tmp.y >= minPosZ)
                        {
                            camera.transform.position = tmp;
                        }
                        //상하 움직임
                    }
                    break;
            }
            MoveLimit();
            prevPos = Input.GetTouch(0).position;
        }
        else if (Input.touchCount == 2)
        {
            // Store both touches.
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);
            // Find the position in the previous frame of each touch.
            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
            // Find the magnitude of the vector (the distance) between the touches in each frame.
            float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
            // Find the difference in the distances between each frame.
            float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
            
            // If the camera is orthographic...
            if (camera.orthographic)
            {
                float temp = camera.orthographicSize;
                temp += deltaMagnitudeDiff * orthoZoomSpeed;
                //camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
                //fv = Mathf.Max(camera.orthographicSize, 0.1f);
                fv = Mathf.Max(temp, 0.1f);
                Calculate(fv);
                MoveLimit();
                if (fv > ZoomInMax)
                {
                    camera.orthographicSize = ZoomInMax;
                }
                else if (ZoomInMin > fv)
                {
                    camera.orthographicSize = ZoomInMin;
                }
                else
                {
                    camera.orthographicSize = fv;
                }
                
            }
            else
            {
                // Otherwise change the field of view based on the change in distance between the touches.
                camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
                // Clamp the field of view to make sure it's between 0 and 180.
                camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f);
                fv = camera.fieldOfView;
            }
        }
    }
    void MoveLimit()
    {
        Vector2 temp;
        temp.x = Mathf.Clamp(camera.transform.position.x, minPosX, maxPosX);
        temp.y = Mathf.Clamp(camera.transform.position.y, minPosZ, maxPosZ);
        camera.transform.position = temp;
    }
    void OnDrawGizmos()
    {
        if (showGizmo)
        {
            
            Calculate(fv);
            Vector3 p1 = new Vector3(minPosX , maxPosZ , transform.position.z);
            Vector3 p2 = new Vector3(maxPosX , maxPosZ , transform.position.z);
            Vector3 p3 = new Vector3(maxPosX , minPosZ , transform.position.z);
            Vector3 p4 = new Vector3(minPosX , minPosZ , transform.position.z);
            
            //영역 테스트용
            Gizmos.color = Color.green;
            Gizmos.DrawLine(p1, p2);
            Gizmos.DrawLine(p2, p3);
            Gizmos.DrawLine(p3, p4);
            Gizmos.DrawLine(p4, p1);
        }
    }
    void Calculate(float size)
    {
        minPosX = -(ZoomInMax - camera.orthographicSize) / xOffset;
        maxPosX = (ZoomInMax - camera.orthographicSize) / xOffset;
        minPosZ = -(ZoomInMax - camera.orthographicSize) / yOffset;
        maxPosZ = (ZoomInMax - camera.orthographicSize) / yOffset;
    }
}
cs

해당코드를 사용하면 된다

기즈모로 영역 제한 offset값을 맞추고(손으로 직접..)
카메라에 붙여서 사용하면 됩니다!








댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...