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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class OrbitCamera : MonoBehaviour
{
Camera regularCamera;
[SerializeField]
Transform focus = default;
[SerializeField, Range(1f, 20f)]
float distance = 5f;
[SerializeField, Min(0f)]
float focusLimitRadius = 1f;
[SerializeField, Range(0f, 1f)]
float focusCenteringSpeed = 0.5f;
[SerializeField, Range(0f, 1f)]
float rotationFlippingSpeed = 0.9f;
[SerializeField, Range(1f, 360f)]
float rotationSpeed = 90f;
[SerializeField, Range(-89f, 89f)]
float minVerticalAngle = -30f;
[SerializeField, Range(-89f, 89f)]
float maxVerticalAngle = 60f;
[SerializeField, Min(0f)]
float alignDelay = 5f;
[SerializeField, Range(0f, 90f)]
float alignSmoothRange = 45;
float lastManualRotationTime;
Vector3 focusPoint;
Vector3 previousFocusPoint;
Vector3 orbitAngles = new Vector2(45f, 0f);
Quaternion gravityAlignment = Quaternion.identity;
Quaternion orbitRotation;
[SerializeField]
LayerMask obstructionMask = -1;
void Awake()
{
regularCamera = GetComponent<Camera>();
focusPoint = focus.position;
transform.localRotation = orbitRotation = Quaternion.Euler(orbitAngles);
OnValidate();
}
void LateUpdate()
{
UpdateGravityAlignment();
UpdateFocusPoint();
if(ManualRotation() || AutomaticRotation())
{
ConstrainAngles();
orbitRotation = Quaternion.Euler(orbitAngles);
}
Quaternion lookRotation = gravityAlignment * orbitRotation;
Vector3 lookDirection = lookRotation * Vector3.forward;
Vector3 lookPosition = focusPoint - lookDirection * distance;
Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane;
Vector3 rectPosition = lookPosition + rectOffset;
Vector3 castFrom = focus.position;
Vector3 castLine = rectPosition - castFrom;
float castDistance = castLine.magnitude;
Vector3 castDirection = castLine / castDistance;
if(Physics.BoxCast(
castFrom,
CameraHalfExtends,
castDirection,
out RaycastHit hit,
lookRotation,
castDistance,
obstructionMask
)
)
{
rectPosition = castFrom + castDirection * hit.distance;
lookPosition = rectPosition - rectOffset;
}
transform.SetPositionAndRotation(lookPosition, lookRotation);
}
void UpdateGravityAlignment() {
Vector3 fromUp = gravityAlignment * Vector3.up;
Vector3 toUp = CustomGravity.GetUpAxis(focusPoint);
float dot = Mathf.Clamp(
Vector3.Dot(fromUp, toUp),
-1f,
1f
);
float angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
Quaternion newAlignment =
Quaternion.FromToRotation(fromUp, toUp) * gravityAlignment;
if(angle > 0.1f && rotationFlippingSpeed > 0f)
{
float t = Mathf.Pow(
1f - rotationFlippingSpeed,
Time.unscaledDeltaTime
);
gravityAlignment = Quaternion.SlerpUnclamped(
gravityAlignment,
newAlignment,
1-t
);
}
else
gravityAlignment = newAlignment;
}
void UpdateFocusPoint()
{
previousFocusPoint = focusPoint;
Vector3 targetPoint = focus.position;
if(focusLimitRadius > 0f)
{
float t = 1f;
float distance = Vector3.Distance(targetPoint, focusPoint);
if(distance > 0.01f && focusCenteringSpeed > 0f)
{
t = Mathf.Pow(
1f - focusCenteringSpeed,
Time.unscaledDeltaTime
);
}
if(distance > focusLimitRadius)
{
t = Mathf.Min(t, focusLimitRadius / distance);
}
focusPoint = Vector3.Lerp(
focusPoint,
targetPoint,
1-t
);
}
else
focusPoint = targetPoint;
}
bool ManualRotation()
{
Vector2 input = new Vector2(
Input.GetAxis("Vertical Camera"),
Input.GetAxis("Horizontal Camera")
);
const float e = 0.001f;
if(
input.x < -e ||
input.x > e ||
input.y < -e ||
input.y > e
)
{
orbitAngles += rotationSpeed * Time.unscaledDeltaTime * (Vector3)input;
lastManualRotationTime = Time.unscaledTime;
return true;
}
return false;
}
void ConstrainAngles()
{
orbitAngles.x = Mathf.Clamp(
orbitAngles.x,
minVerticalAngle,
maxVerticalAngle);
if(orbitAngles.y < 0f)
orbitAngles.y += 360f;
else if(orbitAngles.y >= 360f)
orbitAngles.y -= 360f;
}
bool AutomaticRotation()
{
if(Time.unscaledTime - lastManualRotationTime < alignDelay)
return false;
Vector3 alignedDelta = Quaternion.Inverse(gravityAlignment) *
(focusPoint - previousFocusPoint);
Vector2 movement = new Vector2(
alignedDelta.x,
alignedDelta.z
);
float movementDeltaSqr = movement.sqrMagnitude;
if(movementDeltaSqr < 0.0001f)
return false;
float headingAngle = GetAngle(movement / Mathf.Sqrt(movementDeltaSqr));
float deltaAbs = Mathf.Abs(Mathf.DeltaAngle(orbitAngles.y, headingAngle));
float rotationChange = rotationSpeed * Mathf.Min(Time.unscaledDeltaTime, movementDeltaSqr);
if(deltaAbs < alignSmoothRange)
rotationChange *= deltaAbs / alignSmoothRange;
else if(180f - deltaAbs < alignSmoothRange)
rotationChange *= (180f - deltaAbs) / alignSmoothRange;
orbitAngles.y = Mathf.MoveTowardsAngle(orbitAngles.y, headingAngle, rotationChange);
return true;
}
Vector3 CameraHalfExtends
{
get
{
Vector3 halfExtends;
halfExtends.y =
regularCamera.nearClipPlane *
Mathf.Tan(0.5f * Mathf.Deg2Rad * regularCamera.fieldOfView);
halfExtends.x = halfExtends.y * regularCamera.aspect;
halfExtends.z = 0f;
return halfExtends;
}
}
static float GetAngle (Vector2 direction)
{
float angle = Mathf.Acos(direction.y) * Mathf.Rad2Deg;
return direction.x < 0f ? 360f - angle : angle;
}
// Santizes inspector configuration
void OnValidate()
{
if(maxVerticalAngle < minVerticalAngle)
maxVerticalAngle = minVerticalAngle;
}
}
|