-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathFollow.cs
More file actions
768 lines (659 loc) · 29.7 KB
/
Follow.cs
File metadata and controls
768 lines (659 loc) · 29.7 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using Unity.Profiling;
using Unity.XR.CoreUtils.GUI;
using UnityEngine;
using UnityEngine.Serialization;
namespace MixedReality.Toolkit.SpatialManipulation
{
/// <summary>
/// Follow solver positions an element in front of the of the tracked target (relative to its local forward axis).
/// The element can be loosely constrained (a.k.a. tag-along) so that it doesn't follow until the tracked target moves
/// beyond user defined bounds.
/// </summary>
[AddComponentMenu("MRTK/Spatial Manipulation/Solvers/Follow")]
public class Follow : Solver
{
// todo: consider merging with Radial solver
[SerializeField]
[Tooltip("The desired orientation of this object")]
private SolverOrientationType orientationType = SolverOrientationType.FaceTrackedObject;
/// <summary>
/// The desired orientation of this object.
/// </summary>
public SolverOrientationType OrientationType
{
get => orientationType;
set => orientationType = value;
}
[SerializeField]
[Tooltip("The object will face the tracked object while the object is outside of the distance/direction bounds defined in this component.")]
private bool faceTrackedObjectWhileClamped = true;
/// <summary>
/// The object will face the tracked object while the object is outside of the distance/direction bounds defined in this component.
/// </summary>
public bool FaceTrackedObjectWhileClamped
{
get => faceTrackedObjectWhileClamped;
set => faceTrackedObjectWhileClamped = value;
}
[SerializeField]
[Tooltip("Face a user defined transform rather than using the solver orientation type.")]
private bool faceUserDefinedTargetTransform = false;
/// <summary>
/// Face a user defined transform rather than using the solver orientation type.
/// </summary>
public bool FaceUserDefinedTargetTransform
{
get => faceUserDefinedTargetTransform;
set => faceUserDefinedTargetTransform = value;
}
[SerializeField]
[Tooltip("Transform this object should face rather than using the solver orientation type.")]
private Transform targetToFace = null;
/// <summary>
/// Transform this object should face rather than using the solver orientation type.
/// </summary>
public Transform TargetToFace
{
get => targetToFace;
set => targetToFace = value;
}
[SerializeField, FlagsProperty]
[Tooltip("Rotation axes used when facing target.")]
private AxisFlags pivotAxis = AxisFlags.XAxis | AxisFlags.YAxis | AxisFlags.ZAxis;
/// <summary>
/// Rotation axes used when facing target.
/// </summary>
public AxisFlags PivotAxis
{
get => pivotAxis;
set => pivotAxis = value;
}
[SerializeField]
[Tooltip("Min distance from eye to position element around, i.e. the sphere radius")]
private float minDistance = 0.3f;
/// <summary>
/// Min distance from eye to position element around, i.e. the sphere radius.
/// </summary>
public float MinDistance
{
get => minDistance;
set => minDistance = value;
}
[SerializeField]
[Tooltip("Max distance from eye to element")]
private float maxDistance = 0.9f;
/// <summary>
/// Max distance from eye to element.
/// </summary>
public float MaxDistance
{
get => maxDistance;
set => maxDistance = value;
}
[SerializeField]
[Tooltip("Default distance from eye to position element around, i.e. the sphere radius")]
private float defaultDistance = 0.7f;
/// <summary>
/// Initial placement distance. Should be between min and max.
/// </summary>
public float DefaultDistance
{
get => defaultDistance;
set => defaultDistance = value;
}
[SerializeField]
[Tooltip("The horizontal angle from the tracked target forward axis to this object will not exceed this value")]
private float maxViewHorizontalDegrees = 30f;
/// <summary>
/// The horizontal angle from the tracked target forward axis to this object will not exceed this value.
/// </summary>
public float MaxViewHorizontalDegrees
{
get => maxViewHorizontalDegrees;
set => maxViewHorizontalDegrees = value;
}
[SerializeField]
[Tooltip("The vertical angle from the tracked target forward axis to this object will not exceed this value")]
private float maxViewVerticalDegrees = 20f;
/// <summary>
/// The vertical angle from the tracked target forward axis to this object will not exceed this value.
/// </summary>
public float MaxViewVerticalDegrees
{
get => maxViewVerticalDegrees;
set => maxViewVerticalDegrees = value;
}
[SerializeField]
[Tooltip("The element will only reorient when the object is outside of the distance/direction bounds defined in this component.")]
private bool reorientWhenOutsideParameters = true;
/// <summary>
/// The element will only reorient when the object is outside of the distance/direction bounds above.
/// </summary>
public bool ReorientWhenOutsideParameters
{
get => reorientWhenOutsideParameters;
set => reorientWhenOutsideParameters = value;
}
[SerializeField]
[FormerlySerializedAs("orientToControllerDeadzoneDegrees")]
[Tooltip("The element will not reorient until the angle between the forward vector and vector to the controller is greater then this value")]
private float orientToControllerDeadZoneDegrees = 60f;
/// <summary>
/// The element will not reorient until the angle between the forward vector and vector to the controller is greater then this value.
/// </summary>
public float OrientToControllerDeadZoneDegrees
{
get => orientToControllerDeadZoneDegrees;
set => orientToControllerDeadZoneDegrees = value;
}
[SerializeField]
[Tooltip("Option to ignore angle clamping")]
private bool ignoreAngleClamp = false;
/// <summary>
/// Option to ignore angle clamping.
/// </summary>
public bool IgnoreAngleClamp
{
get => ignoreAngleClamp;
set => ignoreAngleClamp = value;
}
[SerializeField]
[Tooltip("Option to ignore distance clamping")]
private bool ignoreDistanceClamp = false;
/// <summary>
/// Option to ignore distance clamping.
/// </summary>
public bool IgnoreDistanceClamp
{
get => ignoreDistanceClamp;
set => ignoreDistanceClamp = value;
}
[SerializeField]
[Tooltip("Option to ignore the pitch and roll of the reference target")]
private bool ignoreReferencePitchAndRoll = false;
/// <summary>
/// Option to ignore the pitch and roll of the reference target
/// </summary>
public bool IgnoreReferencePitchAndRoll
{
get => ignoreReferencePitchAndRoll;
set => ignoreReferencePitchAndRoll = value;
}
[SerializeField]
[Tooltip("Pitch offset from reference element (relative to Max Distance)")]
private float pitchOffset = 0;
/// <summary>
/// Pitch offset from reference element (relative to MaxDistance).
/// </summary>
public float PitchOffset
{
get => pitchOffset;
set => pitchOffset = value;
}
[SerializeField]
[Tooltip("Max vertical distance between element and reference")]
private float verticalMaxDistance = 0.0f;
/// <summary>
/// Max vertical distance between element and reference.
/// </summary>
public float VerticalMaxDistance
{
get => verticalMaxDistance;
set => verticalMaxDistance = value;
}
/// <summary>
/// Specifies the method used to ensure the refForward vector remains within the bounds set by the leashing parameters.
/// </summary>
public enum AngularClampType
{
/// <summary>
/// Locks the rotation with a viewing cone.
/// </summary>
ViewDegrees = 0,
/// <summary>
/// Locks the rotation to a specified number of steps around the tracked object.
/// </summary>
AngleStepping = 1,
/// <summary>
/// Uses the gameObject's renderer bounds to keep within the view frustum.
/// </summary>
RendererBounds = 2,
/// <summary>
/// Uses the gameObject's collider bounds to keep within the view frustum.
/// </summary>
ColliderBounds = 3,
}
[SerializeField]
[Tooltip("Specifies the method used to ensure the refForward vector remains within the bounds set by the leashing parameters.")]
private AngularClampType angularClampMode = AngularClampType.ViewDegrees;
/// <summary>
/// Accessors for specifying the method used to ensure the refForward vector remains within the bounds set by the leashing parameters.
/// </summary>
public AngularClampType AngularClampMode
{
get => angularClampMode;
set
{
angularClampMode = value;
RecalculateBoundsExtents();
}
}
[Range(2, 24)]
[SerializeField]
[Tooltip("The division of steps this object can tether to. Higher the number, the more snapping steps.")]
private int tetherAngleSteps = 6;
/// <summary>
/// The division of steps this object can tether to. Higher the number, the more snapping steps.
/// </summary>
public int TetherAngleSteps
{
get => tetherAngleSteps;
set => tetherAngleSteps = Mathf.Clamp(value, 2, 24);
}
[SerializeField]
[Tooltip("Scales the bounds to impose a larger or smaller bounds than the calculated bounds.")]
private float boundsScaler = 1.0f;
/// <summary>
/// Scales the bounds to impose a larger or smaller bounds than the calculated bounds.
/// </summary>
public float BoundsScaler
{
get => boundsScaler;
set
{
boundsScaler = value;
RecalculateBoundsExtents();
}
}
/// <summary>
/// Re-centers the target in the next update.
/// </summary>
public void Recenter()
{
recenterNextUpdate = true;
}
/// <summary>
/// Recalculates the bounds based on the angular clamp mode.
/// </summary>
public void RecalculateBoundsExtents()
{
GetBounds(gameObject, angularClampMode, out Bounds bounds);
boundsExtents = bounds.extents * boundsScaler;
}
private Vector3 ReferencePosition => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.position : Vector3.zero;
private Quaternion ReferenceRotation => SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.rotation : Quaternion.identity;
private Quaternion PreviousGoalRotation = Quaternion.identity;
private bool recenterNextUpdate = true;
private Vector3 boundsExtents = Vector3.one;
/// <inheritdoc />
protected override void OnEnable()
{
base.OnEnable();
RecalculateBoundsExtents();
}
private static readonly ProfilerMarker SolverUpdatePerfMarker =
new ProfilerMarker("[MRTK] Follow.SolverUpdate");
/// <inheritdoc />
public override void SolverUpdate()
{
using (SolverUpdatePerfMarker.Auto())
{
Vector3 refPosition = Vector3.zero;
Quaternion refRotation = Quaternion.identity;
Vector3 refForward = Vector3.zero;
GetReferenceInfo(ref refPosition, ref refRotation, ref refForward);
// Determine the current position of the element
Vector3 currentPosition = WorkingPosition;
if (recenterNextUpdate)
{
currentPosition = refPosition + refForward * DefaultDistance;
}
bool wasClamped = false;
// Angular clamp to determine goal direction to place the element
Vector3 goalDirection = refForward;
if (!ignoreAngleClamp && !recenterNextUpdate)
{
wasClamped |= AngularClamp(refPosition, refRotation, currentPosition, ref goalDirection);
}
// Distance clamp to determine goal position to place the element
Vector3 goalPosition = currentPosition;
if (!ignoreDistanceClamp && !recenterNextUpdate)
{
wasClamped |= DistanceClamp(currentPosition, refPosition, goalDirection, ref goalPosition);
}
// Figure out goal rotation of the element based on orientation setting
Quaternion goalRotation = Quaternion.identity;
ComputeOrientation(goalPosition, wasClamped || recenterNextUpdate, ref goalRotation);
if (recenterNextUpdate)
{
PreviousGoalRotation = goalRotation;
SnapTo(goalPosition, goalRotation, WorkingScale);
recenterNextUpdate = false;
}
else
{
// Avoid drift by not updating the goal position when not clamped
if (wasClamped)
{
GoalPosition = goalPosition;
}
GoalRotation = goalRotation;
PreviousGoalRotation = goalRotation;
UpdateWorkingPositionToGoal();
UpdateWorkingRotationToGoal();
}
}
}
private static readonly ProfilerMarker AngleBetweenOnPlanePerfMarker =
new ProfilerMarker("[MRTK] Follow.AngleBetweenOnPlane");
/// <summary>
/// Projects from and to on to the plane with given normal and gets the
/// angle between these projected vectors.
/// </summary>
/// <returns>Angle between project from and to in degrees</returns>
private float AngleBetweenOnPlane(Vector3 from, Vector3 to, Vector3 normal)
{
using (AngleBetweenOnPlanePerfMarker.Auto())
{
from.Normalize();
to.Normalize();
normal.Normalize();
Vector3 right = Vector3.Cross(normal, from);
Vector3 forward = Vector3.Cross(right, normal);
float angle = Mathf.Atan2(Vector3.Dot(to, right), Vector3.Dot(to, forward));
return SimplifyAngle(angle) * Mathf.Rad2Deg;
}
}
/// <summary>
/// Calculates the angle between vec and a plane described by normal. The angle returned
/// is signed.
/// </summary>
/// <returns>Signed angle between vec and the plane described by normal</returns>
private float AngleBetweenVectorAndPlane(Vector3 vec, Vector3 normal)
{
return 90 - (Mathf.Acos(Vector3.Dot(vec, normal)) * Mathf.Rad2Deg);
}
private float SimplifyAngle(float angle)
{
while (angle > Mathf.PI)
{
angle -= 2 * Mathf.PI;
}
while (angle < -Mathf.PI)
{
angle += 2 * Mathf.PI;
}
return angle;
}
private static readonly ProfilerMarker AngularClampPerfMarker =
new ProfilerMarker("[MRTK] Follow.AngularClamp");
/// <summary>
/// This method ensures that the refForward vector remains within the bounds set by the
/// leashing parameters. To do this, it determines the angles between toTarget and the reference
/// local xz and yz planes. If these angles fall within the leashing bounds, then we don't have
/// to modify refForward. Otherwise, we apply a correction rotation to bring it within bounds.
/// </summary>
/// <returns>Whether <paramref name="refForward"/> was clamped or not.</returns>
private bool AngularClamp(Vector3 refPosition, Quaternion refRotation, Vector3 currentPosition, ref Vector3 refForward)
{
using (AngularClampPerfMarker.Auto())
{
Vector3 toTarget = currentPosition - refPosition;
float currentDistance = toTarget.magnitude;
if (currentDistance <= Mathf.Epsilon)
{
// No need to clamp
return false;
}
toTarget.Normalize();
if (toTarget == Vector3.zero)
{
// No need to clamp
return false;
}
// Start off with a rotation towards the target. If it's within leashing bounds, we can leave it alone.
Quaternion rotation = Quaternion.LookRotation(toTarget, Vector3.up);
Vector3 currentRefForward = refRotation * Vector3.forward;
Vector3 refRight = refRotation * Vector3.right;
bool angularClamped = false;
// X-axis leashing
// Leashing around the reference's X axis only makes sense if the reference isn't gravity aligned.
if (IgnoreReferencePitchAndRoll)
{
float angle = AngleBetweenOnPlane(toTarget, currentRefForward, refRight);
rotation = Quaternion.AngleAxis(angle, refRight) * rotation;
}
else
{
float angle = -AngleBetweenOnPlane(toTarget, currentRefForward, refRight);
float minMaxAngle;
switch (angularClampMode)
{
default:
case AngularClampType.ViewDegrees:
case AngularClampType.AngleStepping:
{
minMaxAngle = MaxViewVerticalDegrees * 0.5f;
}
break;
case AngularClampType.RendererBounds:
case AngularClampType.ColliderBounds:
{
Vector3 top = refRotation * new Vector3(0.0f, boundsExtents.y, currentDistance);
minMaxAngle = AngleBetweenOnPlane(top, currentRefForward, refRight) * 2.0f;
}
break;
}
if (angle < -minMaxAngle)
{
rotation = Quaternion.AngleAxis(-minMaxAngle - angle, refRight) * rotation;
angularClamped = true;
}
else if (angle > minMaxAngle)
{
rotation = Quaternion.AngleAxis(minMaxAngle - angle, refRight) * rotation;
angularClamped = true;
}
}
// Y-axis leashing
switch (angularClampMode)
{
case AngularClampType.AngleStepping:
{
float stepAngle = 360f / tetherAngleSteps;
int numberOfSteps = Mathf.RoundToInt(SolverHandler.TransformTarget.transform.eulerAngles.y / stepAngle);
float newAngle = stepAngle * numberOfSteps;
rotation = Quaternion.Euler(rotation.eulerAngles.x, newAngle, rotation.eulerAngles.z);
}
break;
case AngularClampType.ViewDegrees:
case AngularClampType.RendererBounds:
case AngularClampType.ColliderBounds:
{
float angle = AngleBetweenVectorAndPlane(toTarget, refRight);
float minMaxAngle;
if (angularClampMode == AngularClampType.ViewDegrees)
{
minMaxAngle = MaxViewHorizontalDegrees * 0.5f;
}
else
{
Vector3 side = refRotation * new Vector3(boundsExtents.x, 0.0f, boundsExtents.z);
minMaxAngle = AngleBetweenVectorAndPlane(side, refRight) * 2.0f;
}
if (angle < -minMaxAngle)
{
rotation = Quaternion.AngleAxis(-minMaxAngle - angle, Vector3.up) * rotation;
angularClamped = true;
}
else if (angle > minMaxAngle)
{
rotation = Quaternion.AngleAxis(minMaxAngle - angle, Vector3.up) * rotation;
angularClamped = true;
}
}
break;
}
refForward = rotation * Vector3.forward;
return angularClamped;
}
}
/// <summary>
/// This method ensures that the distance from clampedPosition to the tracked target remains within
/// the bounds set by the leashing parameters. To do this, it clamps the current distance to these
/// bounds and then uses this clamped distance with refForward to calculate the new position. If
/// IgnoreReferencePitchAndRoll is true and we have a PitchOffset, we only apply these calculations
/// for xz.
/// </summary>
private bool DistanceClamp(Vector3 currentPosition, Vector3 refPosition, Vector3 refForward, ref Vector3 clampedPosition)
{
float clampedDistance;
float currentDistance = Vector3.Distance(currentPosition, refPosition);
Vector3 direction = refForward;
if (IgnoreReferencePitchAndRoll && PitchOffset != 0)
{
// If we don't account for pitch offset, the casted object will float up/down as the reference
// gets closer to it because we will still be casting in the direction of the pitched offset.
// To fix this, only modify the XZ position of the object.
Vector3 directionXZ = refForward;
directionXZ.y = 0;
directionXZ.Normalize();
Vector3 refToElementXZ = currentPosition - refPosition;
refToElementXZ.y = 0;
float desiredDistanceXZ = refToElementXZ.magnitude;
Vector3 minDistanceXZVector = refForward * MinDistance;
minDistanceXZVector.y = 0;
float minDistanceXZ = minDistanceXZVector.magnitude;
Vector3 maxDistanceXZVector = refForward * MaxDistance;
maxDistanceXZVector.y = 0;
float maxDistanceXZ = maxDistanceXZVector.magnitude;
desiredDistanceXZ = Mathf.Clamp(desiredDistanceXZ, minDistanceXZ, maxDistanceXZ);
Vector3 desiredPosition = refPosition + directionXZ * desiredDistanceXZ;
float desiredHeight = refPosition.y + refForward.y * MaxDistance;
desiredPosition.y = desiredHeight;
direction = desiredPosition - refPosition;
clampedDistance = direction.magnitude;
direction /= clampedDistance;
clampedDistance = Mathf.Max(MinDistance, clampedDistance);
}
else
{
clampedDistance = Mathf.Clamp(currentDistance, MinDistance, MaxDistance);
}
clampedPosition = refPosition + direction * clampedDistance;
// Apply vertical clamp on reference
if (VerticalMaxDistance > 0)
{
clampedPosition.y = Mathf.Clamp(clampedPosition.y, ReferencePosition.y - VerticalMaxDistance, ReferencePosition.y + VerticalMaxDistance);
}
return Vector3EqualEpsilon(clampedPosition, currentPosition, 0.0001f);
}
private void ComputeOrientation(Vector3 goalPosition, bool needsRefresh, ref Quaternion orientation)
{
SolverOrientationType defaultOrientationType = OrientationType;
if (!needsRefresh && reorientWhenOutsideParameters)
{
Vector3 nodeToCamera = goalPosition - ReferencePosition;
float angle = Mathf.Abs(AngleBetweenOnPlane(transform.forward, nodeToCamera, Vector3.up));
if (angle < OrientToControllerDeadZoneDegrees)
{
orientation = PreviousGoalRotation;
return;
}
}
if (FaceUserDefinedTargetTransform)
{
Vector3 directionToTarget = TargetToFace != null ? goalPosition - TargetToFace.position : Vector3.zero;
if ((PivotAxis | AxisFlags.XAxis) != PivotAxis)
{
directionToTarget.x = 0;
}
if ((PivotAxis | AxisFlags.YAxis) != PivotAxis)
{
directionToTarget.y = 0;
}
if ((PivotAxis | AxisFlags.ZAxis) != PivotAxis)
{
directionToTarget.z = 0;
}
if (directionToTarget.sqrMagnitude == 0)
{
orientation = Quaternion.identity;
return;
}
orientation = Quaternion.LookRotation(directionToTarget);
return;
}
if (needsRefresh && FaceTrackedObjectWhileClamped)
{
defaultOrientationType = SolverOrientationType.FaceTrackedObject;
}
switch (defaultOrientationType)
{
case SolverOrientationType.YawOnly:
float targetYRotation = SolverHandler.TransformTarget != null ? SolverHandler.TransformTarget.eulerAngles.y : 0.0f;
orientation = Quaternion.Euler(0f, targetYRotation, 0f);
break;
case SolverOrientationType.Unmodified:
orientation = transform.rotation;
break;
case SolverOrientationType.CameraAligned:
orientation = Camera.main.transform.rotation;
break;
case SolverOrientationType.FaceTrackedObject:
orientation = SolverHandler.TransformTarget != null ? Quaternion.LookRotation(goalPosition - ReferencePosition) : Quaternion.identity;
break;
case SolverOrientationType.CameraFacing:
orientation = SolverHandler.TransformTarget != null ? Quaternion.LookRotation(goalPosition - Camera.main.transform.position) : Quaternion.identity;
break;
case SolverOrientationType.FollowTrackedObject:
orientation = SolverHandler.TransformTarget != null ? ReferenceRotation : Quaternion.identity;
break;
default:
Debug.LogError($"Invalid OrientationType for Orbital Solver on {gameObject.name}");
break;
}
}
private void GetReferenceInfo(ref Vector3 refPosition, ref Quaternion refRotation, ref Vector3 refForward)
{
refPosition = ReferencePosition;
refRotation = ReferenceRotation;
if (IgnoreReferencePitchAndRoll)
{
Vector3 forward = ReferenceRotation * Vector3.forward;
forward.y = 0;
refRotation = Quaternion.LookRotation(forward);
if (PitchOffset != 0)
{
Vector3 right = refRotation * Vector3.right;
forward = Quaternion.AngleAxis(PitchOffset, right) * forward;
refRotation = Quaternion.LookRotation(forward);
}
}
refForward = refRotation * Vector3.forward;
}
private bool Vector3EqualEpsilon(Vector3 x, Vector3 y, float eps)
{
float sqrMagnitude = (x - y).sqrMagnitude;
return sqrMagnitude > eps;
}
private static bool GetBounds(GameObject target, AngularClampType angularClampType, out Bounds bounds)
{
switch (angularClampType)
{
case AngularClampType.RendererBounds:
{
return BoundsExtensions.GetRenderBounds(target, out bounds, 0);
}
case AngularClampType.ColliderBounds:
{
return BoundsExtensions.GetColliderBounds(target, out bounds, 0);
}
}
bounds = new Bounds();
return false;
}
}
}