-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraOrbit.cs
More file actions
189 lines (168 loc) · 5.63 KB
/
CameraOrbit.cs
File metadata and controls
189 lines (168 loc) · 5.63 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
using UnityEngine;
public class CameraOrbit : MonoBehaviour
{
public Transform target;
public float rotationSpeed = 5f;
public float momentumDampening = 2f;
public float touchSensitivity = 0.1f;
private float yaw = 0f;
private float pitch = 0f;
private float lastMouseX = 0f;
private float lastMouseY = 0f;
private float momentumX = 0f;
private float momentumY = 0f;
private bool isDragging = false;
private float dragThreshold = 0.05f;
private float momentumThreshold = 0.2f;
private MoveToClick playerScript;
void Start()
{
if (target == null)
{
GameObject playerObj = GameObject.FindWithTag("Player");
if (playerObj != null)
{
target = playerObj.transform;
playerScript = playerObj.GetComponent<MoveToClick>();
}
}
else
{
playerScript = target.GetComponent<MoveToClick>();
}
if (target != null)
{
Vector3 angles = transform.eulerAngles;
yaw = angles.y;
pitch = angles.x;
}
else
{
Debug.LogError("CameraOrbit: No target assigned and Player not found!");
}
}
void Update()
{
if (Application.isMobilePlatform)
{
HandleTouchInput();
}
else
{
HandleMouseInput();
}
if (!isDragging)
{
yaw += momentumX * Time.deltaTime;
pitch -= momentumY * Time.deltaTime;
momentumX = Mathf.Lerp(momentumX, 0, Time.deltaTime * momentumDampening);
momentumY = Mathf.Lerp(momentumY, 0, Time.deltaTime * momentumDampening);
}
if (target != null)
{
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
transform.position = target.position - (rotation * Vector3.forward * 5f);
transform.LookAt(target);
}
}
void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0))
{
isDragging = false;
momentumX = 0f;
momentumY = 0f;
}
if (Input.GetMouseButton(0))
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
if (Mathf.Abs(mouseX) > dragThreshold || Mathf.Abs(mouseY) > dragThreshold)
{
isDragging = true;
playerScript?.SetMovementAllowed(false);
}
if (isDragging)
{
yaw += mouseX * rotationSpeed;
pitch -= mouseY * rotationSpeed;
pitch = Mathf.Clamp(pitch, -30f, 80f);
}
lastMouseX = mouseX;
lastMouseY = mouseY;
}
if (Input.GetMouseButtonUp(0))
{
if (isDragging && (Mathf.Abs(lastMouseX) > momentumThreshold || Mathf.Abs(lastMouseY) > momentumThreshold))
{
momentumX = lastMouseX * rotationSpeed * 3;
momentumY = lastMouseY * rotationSpeed * 3;
}
else
{
Camera cam = Camera.main;
if (cam != null)
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
playerScript?.OnTap(hit.point);
}
}
}
playerScript?.SetMovementAllowed(true);
}
}
void HandleTouchInput()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
isDragging = false;
momentumX = 0f;
momentumY = 0f;
}
else if (touch.phase == TouchPhase.Moved)
{
float touchX = touch.deltaPosition.x * touchSensitivity;
float touchY = touch.deltaPosition.y * touchSensitivity;
if (Mathf.Abs(touchX) > dragThreshold || Mathf.Abs(touchY) > dragThreshold)
{
isDragging = true;
playerScript?.SetMovementAllowed(false);
}
if (isDragging)
{
yaw += touchX * rotationSpeed;
pitch -= touchY * rotationSpeed;
pitch = Mathf.Clamp(pitch, -30f, 80f);
}
lastMouseX = touchX;
lastMouseY = touchY;
}
else if (touch.phase == TouchPhase.Ended)
{
if (isDragging && (Mathf.Abs(lastMouseX) > momentumThreshold || Mathf.Abs(lastMouseY) > momentumThreshold))
{
momentumX = lastMouseX * rotationSpeed * 3;
momentumY = lastMouseY * rotationSpeed * 3;
}
else
{
Camera cam = Camera.main;
if (cam != null)
{
Ray ray = cam.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out RaycastHit hit))
{
playerScript?.OnTap(hit.point);
}
}
}
playerScript?.SetMovementAllowed(true);
}
}
}
}