diff options
| author | realtradam <[email protected]> | 2023-01-18 17:34:16 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-01-18 17:34:16 -0500 |
| commit | f39e9a0f20729eb084e75444445a5087790ac75f (patch) | |
| tree | 175fc5d0fa2ed2ce4c8eefe5f22c8013625dc13b /Assets/Scripts/OrbitCamera.cs | |
| parent | fa1b19c206acf0bac5a3f0d10c24b77e6bab5969 (diff) | |
| download | Magnet-Run-3D-f39e9a0f20729eb084e75444445a5087790ac75f.tar.gz Magnet-Run-3D-f39e9a0f20729eb084e75444445a5087790ac75f.zip | |
Built test level and added some comments
Diffstat (limited to 'Assets/Scripts/OrbitCamera.cs')
| -rw-r--r-- | Assets/Scripts/OrbitCamera.cs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/Assets/Scripts/OrbitCamera.cs b/Assets/Scripts/OrbitCamera.cs index 57efdec..0a2b1f5 100644 --- a/Assets/Scripts/OrbitCamera.cs +++ b/Assets/Scripts/OrbitCamera.cs @@ -7,7 +7,6 @@ public class OrbitCamera : MonoBehaviour {
Camera regularCamera;
-
[SerializeField]
Transform focus = default;
@@ -62,16 +61,21 @@ public class OrbitCamera : MonoBehaviour {
UpdateGravityAlignment();
UpdateFocusPoint();
+
if(ManualRotation() || AutomaticRotation())
{
ConstrainAngles();
orbitRotation = Quaternion.Euler(orbitAngles);
}
+
Quaternion lookRotation = gravityAlignment * orbitRotation;
Vector3 lookDirection = lookRotation * Vector3.forward;
Vector3 lookPosition = focusPoint - lookDirection * distance;
+ // --- Perform box cast between camera and focus target ---
+ // determine if anything is blocking the camera.
+ // If it is, place camera in front of that object.
Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane;
Vector3 rectPosition = lookPosition + rectOffset;
Vector3 castFrom = focus.position;
@@ -80,22 +84,26 @@ public class OrbitCamera : MonoBehaviour Vector3 castDirection = castLine / castDistance;
if(Physics.BoxCast(
- castFrom,
- CameraHalfExtends,
- castDirection,
- out RaycastHit hit,
- lookRotation,
- castDistance,
- obstructionMask
+ castFrom, // center
+ CameraHalfExtends, // halfExtents
+ castDirection, // direction
+ out RaycastHit hit, // hitInfo
+ lookRotation, // orientation
+ castDistance, // maxDistance
+ obstructionMask // layerMask
)
)
{
rectPosition = castFrom + castDirection * hit.distance;
lookPosition = rectPosition - rectOffset;
}
+ // --- ---
+
transform.SetPositionAndRotation(lookPosition, lookRotation);
}
+ // Orients the camera depending on the direction of gravity.
+ // Interpolates the rotation so that it is smooth.
void UpdateGravityAlignment() {
Vector3 fromUp = gravityAlignment * Vector3.up;
Vector3 toUp = CustomGravity.GetUpAxis(focusPoint);
@@ -126,6 +134,8 @@ public class OrbitCamera : MonoBehaviour gravityAlignment = newAlignment;
}
+ // Applies camera movement smoothing by interpolating
+ // the current focus point and the actual focus point
void UpdateFocusPoint()
{
previousFocusPoint = focusPoint;
@@ -216,6 +226,8 @@ public class OrbitCamera : MonoBehaviour return true;
}
+ // Box cast requires a box that extends 1/2 in all directions
+ // (width, height, depth)
Vector3 CameraHalfExtends
{
get
|
