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
|
using UnityEngine;
using System.Collections;
public class PinchZoom : MonoBehaviour
{
/* Dette vil bare funke for et ortografisk kamera! */
private static readonly float PanSpeed = 20f;
private static readonly float ZoomSpeedTouch = 0.1f;
private static readonly float ZoomSpeedMouse = .5f;
public static readonly float[] BoundsX = new float[]{-10f, 5f};
public static readonly float[] BoundsZ = new float[]{-18f, -4f};
public static readonly float[] ZoomBounds = new float[]{10f, 85f};
private Camera cam;
private bool panActive;
private Vector3 lastPanPosition;
private int panFingerId; // Touch mode only
private bool zoomActive;
private Vector2[] lastZoomPositions; // Touch mode only
void Awake() {
cam = GetComponent<Camera>();
#if UNITY_ANDROID || UNITY_IOS
cam.fieldOfView = 60f;
#endif
}
void Update() {
// If there's an open menu, or the clicker is being pressed, ignore the touch.
/*
if (GameManager.Instance.MenuManager.HasOpenMenu || GameManager.Instance.BitSpawnManager.IsSpawningBits) {
return;
}*/
if (Input.touchSupported && Application.platform != RuntimePlatform.WebGLPlayer) {
HandleTouch();
} else {
HandleMouse();
}
}
void HandleTouch() {
switch(Input.touchCount) {
case 1: // Panning
zoomActive = false;
// If the touch began, capture its position and its finger ID.
// Otherwise, if the finger ID of the touch doesn't match, skip it.
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
lastPanPosition = touch.position;
panFingerId = touch.fingerId;
panActive = true;
} else if (touch.fingerId == panFingerId && touch.phase == TouchPhase.Moved) {
PanCamera(touch.position);
}
break;
case 2: // Zooming
panActive = false;
Vector2[] newPositions = new Vector2[]{Input.GetTouch(0).position, Input.GetTouch(1).position};
if (!zoomActive) {
lastZoomPositions = newPositions;
zoomActive = true;
} else {
// Zoom based on the distance between the new positions compared to the
// distance between the previous positions.
float newDistance = Vector2.Distance(newPositions[0], newPositions[1]);
float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]);
float offset = newDistance - oldDistance;
ZoomCamera(offset, ZoomSpeedTouch);
lastZoomPositions = newPositions;
}
break;
default:
panActive = false;
zoomActive = false;
break;
}
}
void HandleMouse() {
// On mouse down, capture it's position.
// On mouse up, disable panning.
// If there is no mouse being pressed, do nothing.
if (Input.GetMouseButtonDown(0)) {
panActive = true;
lastPanPosition = Input.mousePosition;
} else if (Input.GetMouseButtonUp(0)) {
panActive = false;
} else if (Input.GetMouseButton(0)) {
PanCamera(Input.mousePosition);
}
// Check for scrolling to zoom the camera
float scroll = Input.GetAxis("Mouse ScrollWheel");
zoomActive = true;
ZoomCamera(scroll, ZoomSpeedMouse);
zoomActive = false;
}
void PanCamera(Vector3 newPanPosition) {
if (!panActive) {
return;
}
// Translate the camera position based on the new input position
Vector3 offset = cam.ScreenToViewportPoint(lastPanPosition - newPanPosition);
Vector3 move = new Vector3(offset.x * PanSpeed, 0, offset.y * PanSpeed);
transform.Translate(move, Space.World);
ClampToBounds();
lastPanPosition = newPanPosition;
}
void ZoomCamera(float offset, float speed) {
if (!zoomActive || offset == 0) {
return;
}
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView - (offset * speed), ZoomBounds[0], ZoomBounds[1]);
}
void ClampToBounds() {
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x, BoundsX[0], BoundsX[1]);
pos.z = Mathf.Clamp(transform.position.z, BoundsZ[0], BoundsZ[1]);
transform.position = pos;
}
}
|