Repository navigation
Expand file tree
/
Copy pathCameraHandle.cs
More file actions
196 lines (159 loc) · 6.54 KB
/
Copy pathCameraHandle.cs
File metadata and controls
196 lines (159 loc) · 6.54 KB
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
using UnityEngine;
using System.Collections;
public class CameraHandle : MonoBehaviour {
public GameObject background;
public const float SENSITIVITY = 3f;
public const float MINZOOM = 2.7f;
public const float MAXZOOM = 6.5f;
private const float ZOOMSPEED = 5.0f;
private const float TRANSLATESPEED = 5.0f;
private float scroll;
private float scrollTarget;
public bool translating;
private Vector3 originalPosition;
private float newX,newY;
private float CameraScaleY;
private float CameraScaleX;
private float BGrightBound,CameraRightBound;
private float BGleftBound,CameraLeftBound;
private float BGtopBound,CameraTopBound;
private float BGbottomBound,CameraBottomBound;
public BoneThrowing bone;
void Start() {
BGleftBound = background.GetComponent<SpriteRenderer>().bounds.center.x - background.GetComponent<SpriteRenderer>().bounds.size.x / 2.0f;
BGrightBound = background.GetComponent<SpriteRenderer>().bounds.center.x + background.GetComponent<SpriteRenderer>().bounds.size.x / 2.0f;
BGbottomBound = background.GetComponent<SpriteRenderer>().bounds.center.y - background.GetComponent<SpriteRenderer>().bounds.size.y / 2.0f;
BGtopBound = background.GetComponent<SpriteRenderer>().bounds.center.y + background.GetComponent<SpriteRenderer>().bounds.size.y / 2.0f;
CameraScaleY = 1.0f;
CameraScaleX = CameraScaleY * Camera.main.aspect;
scrollTarget = Camera.main.orthographicSize;
translating = false;
newX = transform.position.x;
newY = transform.position.y;
}
//Correct for view out of boundary
//when not CorrectingBound, function simply tell now is out of bound of not.
// otherwise, tell and correct.
bool CorrectBound(bool CorrectingBound){
bool OutOfBound = false;
CameraRightBound = Camera.main.orthographicSize * CameraScaleX + transform.position.x;
CameraLeftBound = Camera.main.orthographicSize * CameraScaleX - transform.position.x;
CameraTopBound = Camera.main.orthographicSize * CameraScaleY + transform.position.y;
CameraBottomBound = Camera.main.orthographicSize * CameraScaleY - transform.position.y;
if ( CameraRightBound > Mathf.Abs(BGrightBound)){
if(CorrectingBound)
transform.position = new Vector3(
transform.position.x - (CameraRightBound - Mathf.Abs(BGrightBound)),
transform.position.y,transform.position.z);
OutOfBound = true;
}
if (CameraLeftBound > Mathf.Abs (BGleftBound)) {
if(CorrectingBound)
transform.position = new Vector3(
transform.position.x + (CameraLeftBound - Mathf.Abs(BGleftBound)),
transform.position.y,transform.position.z);
OutOfBound = true;
}
if (CameraTopBound > Mathf.Abs (BGtopBound)) {
if(CorrectingBound)
transform.position = new Vector3(
transform.position.x,
transform.position.y - (CameraTopBound - Mathf.Abs(BGtopBound)),
transform.position.z);
OutOfBound = true;
}
if (CameraBottomBound > Mathf.Abs (BGbottomBound)) {
if(CorrectingBound)
transform.position = new Vector3(
transform.position.x,
transform.position.y + (CameraBottomBound - Mathf.Abs(BGbottomBound)),
transform.position.z);
OutOfBound = true;
}
return OutOfBound;
}
void updateAndCorrectScreen(){
if(Mathf.Abs(transform.position.x - newX) > 0.5f || Mathf.Abs(transform.position.y - newY) > 0.5f)
{
transform.localPosition = new Vector3 (Mathf.Lerp(transform.position.x,newX,Time.deltaTime*TRANSLATESPEED),
Mathf.Lerp(transform.position.y,newY,Time.deltaTime*TRANSLATESPEED),
transform.position.z);
CorrectBound (true);
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Application.Quit();
}
if (!Input.touchSupported) {
//Zooming
scroll = Input.GetAxis ("Mouse ScrollWheel") * SENSITIVITY;
if (scroll != 0) {
scrollTarget = Camera.main.orthographicSize - scroll*ZOOMSPEED;
}else if(Mathf.Abs(scrollTarget - Camera.main.orthographicSize) > 0.1f)
{
Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize,scrollTarget,Time.deltaTime*ZOOMSPEED);
if (Camera.main.orthographicSize < MINZOOM)
Camera.main.orthographicSize = MINZOOM;
else if (Camera.main.orthographicSize > MAXZOOM)
Camera.main.orthographicSize = MAXZOOM;
CorrectBound (true);
newX = transform.position.x;
newY = transform.position.y;
}
//Translating by Pressing and Releasing ScrollWheel
if (Input.GetMouseButtonUp (0)) {
translating = false;
} else if (Input.GetMouseButtonDown (0) && !bone.throwing) {
translating = true;
} else if(translating){
newX = transform.position.x - Input.GetAxis ("Mouse X")*TRANSLATESPEED;
newY = transform.position.y - Input.GetAxis ("Mouse Y")*TRANSLATESPEED;
}
updateAndCorrectScreen();
} else {
if(Input.touchCount == 1 && !bone.throwing){
translating = true;
Touch touch0 = Input.GetTouch(0);
newX = transform.position.x - touch0.deltaPosition.x/(TRANSLATESPEED/2);
newY = transform.position.y - touch0.deltaPosition.y/(TRANSLATESPEED/2);
}
else if(Input.touchCount == 2){
translating = true;
Touch touch0 = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
//Determine is zooming or translating
//if two fingers are moving in the same vector then is translating,
Vector2 translateTolerance;
translateTolerance.x = translateTolerance.y = 1.0f;
if(Mathf.Abs(touch0.deltaPosition.x - touch1.deltaPosition.x) < translateTolerance.x &&
Mathf.Abs(touch0.deltaPosition.y - touch1.deltaPosition.y) < translateTolerance.y)
{
/*
newX = transform.position.x + touch0.deltaPosition.x * TRANSLATESPEED;
newY = transform.position.y + touch1.deltaPosition.y * TRANSLATESPEED;
*/
}
//else is zooming.
else {
//Get difference in magniture
Vector2 touch0PrevPos = touch0.position - touch0.deltaPosition;
Vector2 touch1PrevPos = touch1.position - touch1.deltaPosition;
float prevTouchDeltaMag = (touch0PrevPos - touch1PrevPos).magnitude;
float touchDeltaMag = (touch0.position-touch1.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
Camera.main.orthographicSize += deltaMagnitudeDiff * 0.02f;
if (Camera.main.orthographicSize < MINZOOM)
Camera.main.orthographicSize = MINZOOM;
else if (Camera.main.orthographicSize > MAXZOOM)
Camera.main.orthographicSize = MAXZOOM;
CorrectBound (true);
}
} else if(Input.touchCount == 0){
translating = false;
}
updateAndCorrectScreen();
}
}
}