| | 1 | | using Cinemachine; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class CameraStateFPS : CameraStateBase |
| | 7 | | { |
| 0 | 8 | | protected Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 3350 | 9 | | protected Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| | 10 | |
|
| | 11 | | private CinemachinePOV pov; |
| | 12 | |
|
| | 13 | | public override void Initialize(UnityEngine.Camera cameraTransform) |
| | 14 | | { |
| 597 | 15 | | base.Initialize(cameraTransform); |
| | 16 | |
|
| 597 | 17 | | if (defaultVirtualCamera is CinemachineVirtualCamera vcamera) |
| 597 | 18 | | pov = vcamera.GetCinemachineComponent<CinemachinePOV>(); |
| 597 | 19 | | } |
| | 20 | |
|
| | 21 | | public override void OnUpdate() |
| | 22 | | { |
| 3350 | 23 | | var xzPlaneForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1)); |
| 3350 | 24 | | characterForward.Set(xzPlaneForward); |
| 3350 | 25 | | } |
| | 26 | |
|
| | 27 | | public override Vector3 OnGetRotation() |
| | 28 | | { |
| 12 | 29 | | if (pov != null) |
| 12 | 30 | | return new Vector3(pov.m_VerticalAxis.Value, pov.m_HorizontalAxis.Value, 0); |
| | 31 | |
|
| 0 | 32 | | return Vector3.zero; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public override void OnSetRotation(CameraController.SetRotationPayload payload) |
| | 36 | | { |
| 6 | 37 | | var eulerDir = Vector3.zero; |
| | 38 | |
|
| 6 | 39 | | if (payload.cameraTarget.HasValue) |
| | 40 | | { |
| 6 | 41 | | var cameraTarget = payload.cameraTarget.GetValueOrDefault(); |
| | 42 | |
|
| 6 | 43 | | var horizontalAxisLookAt = payload.y - cameraTarget.y; |
| 6 | 44 | | var verticalAxisLookAt = new Vector3(cameraTarget.x - payload.x, 0, cameraTarget.z - payload.z); |
| | 45 | |
|
| 6 | 46 | | eulerDir.y = Vector3.SignedAngle(Vector3.forward, verticalAxisLookAt, Vector3.up); |
| 6 | 47 | | eulerDir.x = Mathf.Atan2(horizontalAxisLookAt, verticalAxisLookAt.magnitude) * Mathf.Rad2Deg; |
| | 48 | | } |
| | 49 | |
|
| 6 | 50 | | if (pov != null) |
| | 51 | | { |
| 6 | 52 | | pov.m_HorizontalAxis.Value = eulerDir.y; |
| 6 | 53 | | pov.m_VerticalAxis.Value = eulerDir.x; |
| | 54 | | } |
| 6 | 55 | | } |
| | 56 | |
|
| | 57 | | public override void OnBlock(bool blocked) |
| | 58 | | { |
| 0 | 59 | | base.OnBlock(blocked); |
| | 60 | |
|
| 0 | 61 | | if (pov != null) |
| | 62 | | { |
| 0 | 63 | | if (blocked) |
| | 64 | | { |
| | 65 | | // Before block the virtual camera, we make the main camera point to the same point as the virtual o |
| 0 | 66 | | defaultVirtualCamera.transform.rotation = Quaternion.Euler(pov.m_VerticalAxis.Value, pov.m_Horizonta |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | pov.enabled = !blocked; |
| | 70 | |
|
| | 71 | | // NOTE(Santi): After modify the 'CinemachinePOV.enabled' parameter, the only (and not very elegant) way |
| | 72 | | // offers to force the update is deactivating and re-activating the virtual camera. |
| 0 | 73 | | defaultVirtualCamera.enabled = false; |
| 0 | 74 | | defaultVirtualCamera.enabled = true; |
| | 75 | | } |
| 0 | 76 | | } |
| | 77 | | } |
| | 78 | | } |