-
Notifications
You must be signed in to change notification settings - Fork 0
/
cameraMove.cs
181 lines (148 loc) · 6.83 KB
/
cameraMove.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class cameraMove : MonoBehaviour
{
//public Transform target;
public float speed = 1.5f;
private float x = 0.0f;
private float y = 0.0f;
private Touch touch;
private Vector3 dragOrigin;
private float maxx = 200;
private float minx = -200;
private float maxy = 200;
private float miny = -200;
public int editmode;
public Camera sketchcam;
public Camera extcam;
Vector3 touchStart;
public float zoomOutMin = 1;
public float zoomOutMax = 20;
float sensitivity = 10f;
private float init;
//private Vector3 distance;
// Use this for initialization
public void editingmode(int mode)
{
editmode = mode;
//Debug.Log("here");
}
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
private bool IsPointerOverUIObject()
{
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
void Update()
{
if (editmode == 1)
{
if (Input.GetMouseButtonDown(0))
{
touchStart = sketchcam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, sketchcam.nearClipPlane));
}
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
float difference = currentMagnitude - prevMagnitude;
zoom(difference * 0.01f);
}
else if (Input.GetMouseButton(0))
{
Vector3 direction = touchStart - sketchcam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, sketchcam.nearClipPlane));
sketchcam.transform.position += direction;
//Debug.Log(direction);
}
zoom(Input.GetAxis("Mouse ScrollWheel"));
//Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
}
if(editmode == 2)
{
if (Input.GetMouseButtonDown(0) )
{
touchStart = sketchcam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, sketchcam.nearClipPlane));
}
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
float difference = currentMagnitude - prevMagnitude;
extcam.transform.position = extzoom(difference * 0.001f);
}
else if (Input.GetMouseButton(0) && Input.touchCount == 1 && !IsPointerOverUIObject())
{
//Version1
float x_ = Input.GetAxis("Mouse X");
float y_ = Input.GetAxis("Mouse Y");
Vector3 camrotation = extcam.transform.eulerAngles;
camrotation.x += y_ * 2f;
camrotation.y -= x_ * 2f;
extcam.transform.rotation = Quaternion.Euler(camrotation.x,camrotation.y,0);
//version2
}
Vector3 pos = extzoom(Input.GetAxis("Mouse ScrollWheel"));
//istance -= delta * (distance / MAX_CAM_DISTANCE) * (zoomSpeed * 1000) * Time.deltaTime;
//distance = Mathf.Clamp(distance, MIN_CAM_DISTANCE, MAX_CAM_DISTANCE);
extcam.transform.position = pos;
extcam.transform.position = extcam.transform.rotation * (Vector3.forward * -pos.magnitude);//Move the main camera
}
//y = ClampAngle(y, yMinLimit, yMaxLimit);
//Quaternion rotation = Quaternion.Euler(y, x, 0);
//transform.rotation = rotation;
}
void zoom(float increment)
{
sketchcam.orthographicSize = Mathf.Clamp(sketchcam.orthographicSize - increment, zoomOutMin, zoomOutMax);
//sketchcam.transform.position += new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * sensitivity);
//Debug.Log("here");
}
Vector3 extzoom(float change)
{
//extcam.fieldOfView = Mathf.Clamp(extcam.fieldOfView - fov, 4f, 180f);
float R = change * 15; //The radius from current camera
float PosX = extcam.transform.eulerAngles.x + 90; //Get up and down
float PosY = -1 * (extcam.transform.eulerAngles.y - 90); //Get left to right
PosX = PosX / 180 * Mathf.PI; //Convert from degrees to radians
PosY = PosY / 180 * Mathf.PI; //^
float X = R * Mathf.Sin(PosX) * Mathf.Cos(PosY); //Calculate new coords
float Z = R * Mathf.Sin(PosX) * Mathf.Sin(PosY); //^
float Y = R * Mathf.Cos(PosX); //^
float CamX = extcam.transform.position.x; //Get current camera postition for the offset
float CamY = extcam.transform.position.y; //^
float CamZ = extcam.transform.position.z; //^
Vector3 pos = new Vector3(CamX + X, CamY + Y, CamZ + Z);
if (pos.magnitude>50f || pos.magnitude < 5f)
{
pos = new Vector3(CamX, CamY, CamZ);
}
return pos;
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}