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