| | 1 | | using Cinemachine; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class CameraFreefall |
| | 7 | | { |
| | 8 | | [System.Serializable] |
| | 9 | | public class Settings |
| | 10 | | { |
| 0 | 11 | | public bool enabled = true; |
| | 12 | |
|
| | 13 | | public CinemachineVirtualCamera freefallVirtualCamera; |
| | 14 | |
|
| | 15 | | public float fallDetectionDelay; |
| | 16 | |
|
| | 17 | | [Tooltip("Min raycast ground distance to be considered a fall")] |
| | 18 | | public float fallGroundDistanceThreshold; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public Settings settings; |
| | 22 | | private CinemachineFreeLook defaultFreeLookCamera; |
| | 23 | |
|
| | 24 | | private Vector3 lastPosition; |
| | 25 | | private float fallDetectionTimer; |
| 0 | 26 | | protected Vector3Variable characterPosition => CommonScriptableObjects.playerUnityPosition; |
| | 27 | |
|
| 597 | 28 | | public CameraFreefall (Settings settings, CinemachineFreeLook defaultFreeLookCamera) |
| | 29 | | { |
| 597 | 30 | | this.defaultFreeLookCamera = defaultFreeLookCamera; |
| 597 | 31 | | this.settings = settings; |
| 597 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Update(bool hitGround, RaycastHit hitInfo) |
| | 35 | | { |
| 2198 | 36 | | if ( !settings.enabled ) |
| 2198 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | bool useFallingCamera = false; |
| | 40 | |
|
| 0 | 41 | | if ( hitGround ) |
| | 42 | | { |
| 0 | 43 | | useFallingCamera = hitInfo.distance > settings.fallGroundDistanceThreshold; |
| 0 | 44 | | } |
| | 45 | | else |
| | 46 | | { |
| 0 | 47 | | useFallingCamera = true; |
| | 48 | | } |
| | 49 | |
|
| | 50 | |
|
| 0 | 51 | | Vector3 velocity = characterPosition - lastPosition; |
| | 52 | |
|
| 0 | 53 | | if ( velocity.y < 0 ) |
| | 54 | | { |
| 0 | 55 | | fallDetectionTimer += Time.deltaTime; |
| | 56 | |
|
| 0 | 57 | | if ( fallDetectionTimer < settings.fallDetectionDelay ) |
| 0 | 58 | | useFallingCamera = false; |
| 0 | 59 | | } |
| | 60 | | else |
| | 61 | | { |
| 0 | 62 | | fallDetectionTimer = 0; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | lastPosition = characterPosition; |
| | 66 | |
|
| 0 | 67 | | if ( settings.freefallVirtualCamera.gameObject.activeSelf != useFallingCamera) |
| 0 | 68 | | settings.freefallVirtualCamera.gameObject.SetActive(useFallingCamera); |
| | 69 | |
|
| 0 | 70 | | if ( useFallingCamera ) |
| | 71 | | { |
| 0 | 72 | | var orbitalTransposer = settings.freefallVirtualCamera.GetCinemachineComponent<CinemachineOrbitalTranspo |
| | 73 | |
|
| 0 | 74 | | if ( orbitalTransposer != null ) |
| 0 | 75 | | orbitalTransposer.m_XAxis = defaultFreeLookCamera.m_XAxis; |
| | 76 | | } |
| 0 | 77 | | } |
| | 78 | | } |
| | 79 | | } |