| | 1 | | using Cinemachine; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class CameraStateTPS : CameraStateBase |
| | 7 | | { |
| 0 | 8 | | private CinemachineFreeLook defaultVirtualCameraAsFreeLook => defaultVirtualCamera as CinemachineFreeLook; |
| | 9 | |
|
| | 10 | | [SerializeField] private CinemachineVirtualCamera fallingVirtualCamera; |
| | 11 | | [SerializeField] private InputAction_Measurable characterYAxis; |
| | 12 | | [SerializeField] private InputAction_Measurable characterXAxis; |
| | 13 | | private CinemachineTransposer freeLookTopRig; |
| | 14 | | private CinemachineTransposer freeLookMidRig; |
| | 15 | | private CinemachineTransposer freeLookBotRig; |
| | 16 | | private Vector3 freeLookTopRigOriginalBodyDamping; |
| | 17 | | private Vector3 freeLookMidRigOriginalBodyDamping; |
| | 18 | | private Vector3 freeLookBotRigOriginalBodyDamping; |
| | 19 | |
|
| 0 | 20 | | protected Vector3Variable characterPosition => CommonScriptableObjects.playerUnityPosition; |
| 100 | 21 | | protected Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| 0 | 22 | | protected Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 0 | 23 | | protected Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| 0 | 24 | | protected Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| | 25 | |
|
| | 26 | | public FollowWithDamping cameraTargetProbe; |
| | 27 | |
|
| | 28 | | [Header("Avatar Feel Settings")] |
| 598 | 29 | | public float avatarRotationLerpSpeed = 10; |
| | 30 | |
|
| | 31 | | [SerializeField] |
| 598 | 32 | | private CameraFreefall.Settings cameraFreefallSettings = new CameraFreefall.Settings(); |
| | 33 | |
|
| | 34 | | public CameraFreefall cameraFreefall; |
| | 35 | |
|
| | 36 | | [SerializeField] |
| 598 | 37 | | private CameraDampOnGroundType.Settings cameraDampOnGroundTypeSettings = new CameraDampOnGroundType.Settings(); |
| | 38 | |
|
| | 39 | | public CameraDampOnGroundType cameraDampOnGroundType; |
| | 40 | |
|
| | 41 | | [SerializeField] |
| 598 | 42 | | private CameraDampOnSprint.Settings cameraSprintDampingSettings = new CameraDampOnSprint.Settings(); |
| | 43 | |
|
| | 44 | | public CameraDampOnSprint cameraDampOnSprint; |
| | 45 | |
|
| | 46 | | public override void Initialize(UnityEngine.Camera camera) |
| | 47 | | { |
| 597 | 48 | | freeLookTopRig = defaultVirtualCameraAsFreeLook.GetRig(0).GetCinemachineComponent<CinemachineTransposer>(); |
| 597 | 49 | | freeLookTopRigOriginalBodyDamping = new Vector3(freeLookTopRig.m_XDamping, freeLookTopRig.m_YDamping, freeLo |
| 597 | 50 | | freeLookMidRig = defaultVirtualCameraAsFreeLook.GetRig(1).GetCinemachineComponent<CinemachineTransposer>(); |
| 597 | 51 | | freeLookMidRigOriginalBodyDamping = new Vector3(freeLookMidRig.m_XDamping, freeLookMidRig.m_YDamping, freeLo |
| 597 | 52 | | freeLookBotRig = defaultVirtualCameraAsFreeLook.GetRig(2).GetCinemachineComponent<CinemachineTransposer>(); |
| 597 | 53 | | freeLookBotRigOriginalBodyDamping = new Vector3(freeLookBotRig.m_XDamping, freeLookBotRig.m_YDamping, freeLo |
| | 54 | |
|
| 597 | 55 | | cameraDampOnSprint = new CameraDampOnSprint(cameraSprintDampingSettings, defaultVirtualCameraAsFreeLook, cha |
| 597 | 56 | | cameraDampOnGroundType = new CameraDampOnGroundType(cameraDampOnGroundTypeSettings, cameraTargetProbe); |
| 597 | 57 | | cameraFreefall = new CameraFreefall(cameraFreefallSettings, defaultVirtualCameraAsFreeLook); |
| | 58 | |
|
| 597 | 59 | | base.Initialize(camera); |
| 597 | 60 | | } |
| | 61 | |
|
| | 62 | | public override void Cleanup() |
| | 63 | | { |
| 597 | 64 | | if (cameraTargetProbe != null) |
| 597 | 65 | | Destroy(cameraTargetProbe.gameObject); |
| 597 | 66 | | } |
| | 67 | |
|
| | 68 | | private void OnEnable() |
| | 69 | | { |
| 98 | 70 | | CommonScriptableObjects.playerIsOnMovingPlatform.OnChange += UpdateMovingPlatformCamera; |
| 98 | 71 | | } |
| | 72 | |
|
| 196 | 73 | | private void OnDisable() { CommonScriptableObjects.playerIsOnMovingPlatform.OnChange -= UpdateMovingPlatformCame |
| | 74 | |
|
| | 75 | | void UpdateMovingPlatformCamera(bool isOnMovingPlatform, bool wasOnMovingPlatform) |
| | 76 | | { |
| 0 | 77 | | if (isOnMovingPlatform) |
| | 78 | | { |
| 0 | 79 | | freeLookTopRig.m_XDamping = 0; |
| 0 | 80 | | freeLookTopRig.m_YDamping = 0; |
| 0 | 81 | | freeLookTopRig.m_ZDamping = 0; |
| | 82 | |
|
| 0 | 83 | | freeLookMidRig.m_XDamping = 0; |
| 0 | 84 | | freeLookMidRig.m_YDamping = 0; |
| 0 | 85 | | freeLookMidRig.m_ZDamping = 0; |
| | 86 | |
|
| 0 | 87 | | freeLookBotRig.m_XDamping = 0; |
| 0 | 88 | | freeLookBotRig.m_YDamping = 0; |
| 0 | 89 | | freeLookBotRig.m_ZDamping = 0; |
| 0 | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | freeLookTopRig.m_XDamping = freeLookTopRigOriginalBodyDamping.x; |
| 0 | 94 | | freeLookTopRig.m_YDamping = freeLookTopRigOriginalBodyDamping.y; |
| 0 | 95 | | freeLookTopRig.m_ZDamping = freeLookTopRigOriginalBodyDamping.z; |
| | 96 | |
|
| 0 | 97 | | freeLookMidRig.m_XDamping = freeLookMidRigOriginalBodyDamping.x; |
| 0 | 98 | | freeLookMidRig.m_YDamping = freeLookMidRigOriginalBodyDamping.y; |
| 0 | 99 | | freeLookMidRig.m_ZDamping = freeLookMidRigOriginalBodyDamping.z; |
| | 100 | |
|
| 0 | 101 | | freeLookBotRig.m_XDamping = freeLookBotRigOriginalBodyDamping.x; |
| 0 | 102 | | freeLookBotRig.m_YDamping = freeLookBotRigOriginalBodyDamping.y; |
| 0 | 103 | | freeLookBotRig.m_ZDamping = freeLookBotRigOriginalBodyDamping.z; |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public override void OnSelect() |
| | 108 | | { |
| 98 | 109 | | if (characterForward.Get().HasValue) |
| | 110 | | { |
| 2 | 111 | | defaultVirtualCameraAsFreeLook.m_XAxis.Value = Quaternion.LookRotation(characterForward.Get().Value, Vec |
| 2 | 112 | | defaultVirtualCameraAsFreeLook.m_YAxis.Value = 0.5f; |
| | 113 | | } |
| | 114 | |
|
| 98 | 115 | | base.OnSelect(); |
| 98 | 116 | | } |
| | 117 | |
|
| | 118 | | public override void OnUpdate() |
| | 119 | | { |
| 2198 | 120 | | defaultVirtualCameraAsFreeLook.m_BindingMode = CinemachineTransposer.BindingMode.WorldSpace; |
| | 121 | |
|
| 2198 | 122 | | bool didHit = DCLCharacterController.i.CastGroundCheckingRays(20, 0.1f, out RaycastHit hitInfo); |
| | 123 | |
|
| 2198 | 124 | | cameraDampOnSprint.Update(); |
| 2198 | 125 | | cameraDampOnGroundType.Update(didHit, hitInfo); |
| 2198 | 126 | | cameraFreefall.Update(didHit, hitInfo); |
| | 127 | |
|
| 2198 | 128 | | UpdateAvatarRotationDamping(); |
| 2198 | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// This methods ensures the Avatar rotates smoothly when changing direction. |
| | 133 | | /// Note that this will NOT affect the player movement, is only cosmetic. |
| | 134 | | /// TODO(Brian): This is not camera feel, move this elsewhere |
| | 135 | | /// </summary> |
| | 136 | | private void UpdateAvatarRotationDamping() |
| | 137 | | { |
| 2198 | 138 | | var xzPlaneForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1)); |
| 2198 | 139 | | var xzPlaneRight = Vector3.Scale(cameraTransform.right, new Vector3(1, 0, 1)); |
| | 140 | |
|
| 2198 | 141 | | if (characterYAxis.GetValue() != 0f || characterXAxis.GetValue() != 0f) |
| | 142 | | { |
| 0 | 143 | | Vector3 forwardTarget = Vector3.zero; |
| | 144 | |
|
| 0 | 145 | | if (characterYAxis.GetValue() > 0) |
| 0 | 146 | | forwardTarget += xzPlaneForward; |
| | 147 | |
|
| 0 | 148 | | if (characterYAxis.GetValue() < 0) |
| 0 | 149 | | forwardTarget -= xzPlaneForward; |
| | 150 | |
|
| 0 | 151 | | if (characterXAxis.GetValue() > 0) |
| 0 | 152 | | forwardTarget += xzPlaneRight; |
| | 153 | |
|
| 0 | 154 | | if (characterXAxis.GetValue() < 0) |
| 0 | 155 | | forwardTarget -= xzPlaneRight; |
| | 156 | |
|
| 0 | 157 | | forwardTarget.Normalize(); |
| | 158 | |
|
| 0 | 159 | | if (!characterForward.HasValue()) |
| | 160 | | { |
| 0 | 161 | | characterForward.Set(forwardTarget); |
| 0 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 0 | 165 | | var lerpedForward = Vector3.Slerp(characterForward.Get().Value, forwardTarget, avatarRotationLerpSpe |
| 0 | 166 | | characterForward.Set(lerpedForward); |
| | 167 | | } |
| | 168 | | } |
| 2198 | 169 | | } |
| | 170 | |
|
| 0 | 171 | | public override Vector3 OnGetRotation() { return new Vector3(defaultVirtualCameraAsFreeLook.m_YAxis.Value, defau |
| | 172 | |
|
| | 173 | | public override void OnSetRotation(CameraController.SetRotationPayload payload) |
| | 174 | | { |
| 0 | 175 | | var eulerDir = Vector3.zero; |
| | 176 | |
|
| 0 | 177 | | if (payload.cameraTarget.HasValue) |
| | 178 | | { |
| 0 | 179 | | var cameraTarget = payload.cameraTarget.GetValueOrDefault(); |
| | 180 | |
|
| 0 | 181 | | var horizontalAxisLookAt = payload.y - cameraTarget.y; |
| 0 | 182 | | var verticalAxisLookAt = new Vector3(cameraTarget.x - payload.x, 0, cameraTarget.z - payload.z); |
| | 183 | |
|
| 0 | 184 | | eulerDir.y = Vector3.SignedAngle(Vector3.forward, verticalAxisLookAt, Vector3.up); |
| 0 | 185 | | eulerDir.x = Mathf.Atan2(horizontalAxisLookAt, verticalAxisLookAt.magnitude) * Mathf.Rad2Deg; |
| | 186 | | } |
| | 187 | |
|
| 0 | 188 | | defaultVirtualCameraAsFreeLook.m_XAxis.Value = eulerDir.y; |
| | 189 | |
|
| | 190 | | //value range 0 to 1, being 0 the bottom orbit and 1 the top orbit |
| 0 | 191 | | var yValue = Mathf.InverseLerp(-90, 90, eulerDir.x); |
| 0 | 192 | | defaultVirtualCameraAsFreeLook.m_YAxis.Value = yValue; |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | public override void OnBlock(bool blocked) |
| | 196 | | { |
| 0 | 197 | | base.OnBlock(blocked); |
| 0 | 198 | | defaultVirtualCameraAsFreeLook.enabled = !blocked; |
| 0 | 199 | | } |
| | 200 | | } |
| | 201 | | } |