본문 바로가기

Programming/Unity

Unity C# Zoom in out

그냥 거리만 축소되는 줌인이 아니라 디아블로3를 하면서 느낀게 거리가 축소되면서 카메라의 x축으로 화전이 가해지는 줌인을 만들어보고 싶어 생각하게되었다.


1. 줌인 명령을 해줄 버튼을 NGUI를 이용해 만들어 둔위 스크립트를 추가

ZoomInOut.cs

- zoom in out 할 때 한번에 확 되는게 아니라 부드럽게 될 수 있도록 delta time을 넣어줬다.

- 각도의 경우 거리와 같은 비율로 할 경우 회전정도가 작아보여서 RoatateSpeed라고 더 넣어줬는데

  사실 Speed라기 보단 Offset이 더 의미전달이 잘 될거같아 이름을 바꾸었다.

  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

using UnityEngine; using System.Collections; public class ZoomInOut : MonoBehaviour { bool zoom = false; bool zoomEnd = false; public float zoomvalue = 5; public float zoomSpeed = 1.0f; public float RotateOffset = 2.0f; MainCamera MainCam = null; void Awake() { // MainCamera를 찾음 MainCam = GameObject.Find("MainCamera").GetComponent<MainCamera>(); } void OnClick() { if ( !zoomEnd ) { StartCoroutine(Zoom_InOut(!zoom)); zoom = !zoom; } } IEnumerator Zoom_InOut(bool _Zoom) { bool DistanceEnd = false; bool RotateEnd = false; zoomEnd = true; // 삼항연산자 버전 int signe = 1; if (!zoom) { signe *= -1; } while (true) { // 거리 MainCam.Currentdistance += (zoomvalue * Time.smoothDeltaTime * zoomSpeed) * signe; if (zoom ? MainCam.Currentdistance < MainCam.GetOriDistance() - zoomvalue : MainCam.Currentdistance > MainCam.GetOriDistance()) { if (zoom) { MainCam.Currentdistance = MainCam.GetOriDistance() - zoomvalue; } else { MainCam.Currentdistance = MainCam.GetOriDistance(); } DistanceEnd = true; } // 각도 MainCam.RotateX += (zoomvalue * Time.smoothDeltaTime * (zoomSpeed + RotateOffset)) * signe; if (zoom ? MainCam.RotateX < MainCam.GetOriRotate() - (zoomvalue + RotateOffset) : MainCam.RotateX > MainCam.GetOriRotate()) { if (zoom) { MainCam.RotateX = MainCam.GetOriRotate() - (zoomvalue + RotateOffset); } else { MainCam.RotateX = MainCam.GetOriRotate(); } RotateEnd = true; } if (RotateEnd && DistanceEnd) { break; } MainCam.Reset(MainCam.RotateX, MainCam.RotateY); yield return null; } zoomEnd = false; //// 일반버전 //if (_Zoom) //{ // // zoomin // while (true) // { // // 거리 // MainCam.Currentdistance -= zoomvalue * Time.smoothDeltaTime * zoomSpeed; // if (MainCam.Currentdistance < MainCam.GetOriDistance() - zoomvalue) // { // MainCam.Currentdistance = MainCam.GetOriDistance() - zoomvalue; // DistanceEnd = true; // } // // 각도 // MainCam.RotateX -= zoomvalue * Time.smoothDeltaTime * zoomSpeed * RotateOffset; // if (MainCam.RotateX < MainCam.GetOriRotate() - (zoomvalue * RotateOffset)) // { // MainCam.RotateX = MainCam.GetOriRotate() - (zoomvalue * RotateOffset); // RotateEnd = true; // } // if (RotateEnd && DistanceEnd) // { // break; // } // MainCam.Reset(MainCam.RotateX, MainCam.RotateY); // yield return null; // } //} //else //{ // // zoomout // while (true) // { // // 거리 // MainCam.Currentdistance += zoomvalue * Time.smoothDeltaTime * zoomSpeed; // if (MainCam.Currentdistance > MainCam.GetOriDistance()) // { // MainCam.Currentdistance = MainCam.GetOriDistance(); // DistanceEnd = true; // } // // 각도 // MainCam.RotateX += zoomvalue * Time.smoothDeltaTime * zoomSpeed * RotateOffset; // if (MainCam.RotateX > MainCam.GetOriRotate()) // { // MainCam.RotateX = MainCam.GetOriRotate(); // RotateEnd = true; // } // if (RotateEnd && DistanceEnd) // { // break; // } // MainCam.Reset(MainCam.RotateX, MainCam.RotateY); // yield return null; // } //} //zoomEnd = false; } }


2. 메인카메라 오브젝트에도 스크립트를 추가

MainCamera.cs

 - 줌인을 하면 거리와 회전값이 변경되어야 하기에 초기값을 저장해 가지고있는다 외부에서 잘못 변경하지 못 하도록 private

   으로 해둔다.

 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

using UnityEngine; using System.Collections; public class MainCamera : MonoBehaviour { public GameObject Player; public float RotateX = 45f; float OriRotateX = 0.0f; public float RotateY = 45f; public float Currentdistance = 10; float OriDistance = 0.0f; Transform TempTrans = null; // Use this for initialization void Start () { OriDistance = Currentdistance; OriRotateX = RotateX; transform.position = new Vector3(0, 0, 0); TempTrans = transform; Reset(RotateX, RotateY); } // Update is called once per frame void Update () { transform.position = Player.transform.position + (-TempTrans.forward) * Currentdistance; } public void Reset(float _RotateX, float _RotateY) { TempTrans = transform; TempTrans.rotation = Quaternion.identity; TempTrans.Rotate(_RotateX, _RotateY, 0.0f); } public float GetOriDistance() { return OriDistance; } public float GetOriRotate() { return OriRotateX; } }


완성된 Gif 파일(gif로 만드니까 화질이 확 떨어짐 -ㅅ-;)


'Programming > Unity' 카테고리의 다른 글

Template을 이용한 Data 저장 Component 만들기(소스코드)  (0) 2017.09.23
NGUI 사용 흐르는 문자열 만들기  (2) 2017.03.27
NGUI HUDText 사용  (2) 2015.07.30
NGUI HP Bar 만들기  (0) 2015.07.30
몬스터 FSM 구현  (3) 2015.07.30