| | 1 | | using System; |
| | 2 | | using DCL.Camera; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class FirstPersonCameraEntityReference : MonoBehaviour |
| | 6 | | { |
| | 7 | | public CameraController cameraController; |
| | 8 | | public Transform cameraPosition; |
| | 9 | |
|
| | 10 | | private Transform nextParent, firtPersonParent, initialParent; |
| | 11 | |
|
| | 12 | | private void Awake() |
| | 13 | | { |
| | 14 | | // Assign the camera position to the game object |
| 145 | 15 | | if (cameraPosition != null) |
| | 16 | | { |
| 124 | 17 | | transform.position = cameraPosition.position; |
| | 18 | | } |
| 145 | 19 | | initialParent = transform.parent; |
| 145 | 20 | | firtPersonParent = Camera.main.transform; |
| | 21 | |
|
| | 22 | | // Listen to changes on the camera mode |
| 145 | 23 | | CommonScriptableObjects.cameraMode.OnChange += OnCameraModeChange; |
| | 24 | |
|
| | 25 | | // Trigger the initial camera set |
| 145 | 26 | | OnCameraModeChange(CommonScriptableObjects.cameraMode, CommonScriptableObjects.cameraMode); |
| | 27 | |
|
| | 28 | | //There is no blend in the first set so we parent to the correct initial transform |
| 145 | 29 | | SetNextParent(); |
| 145 | 30 | | } |
| | 31 | |
|
| 134 | 32 | | private void UpdateForward(Vector3 newForward, Vector3 prev) { transform.forward = newForward; } |
| | 33 | |
|
| | 34 | | private void OnDestroy() |
| | 35 | | { |
| 144 | 36 | | CommonScriptableObjects.cameraMode.OnChange -= OnCameraModeChange; |
| 144 | 37 | | CommonScriptableObjects.cameraForward.OnChange -= UpdateForward; |
| 144 | 38 | | } |
| | 39 | |
|
| | 40 | | private void OnCameraModeChange(CameraMode.ModeId newMode, CameraMode.ModeId prev) |
| | 41 | | { |
| 164 | 42 | | CommonScriptableObjects.cameraForward.OnChange -= UpdateForward; |
| | 43 | |
|
| 164 | 44 | | if (newMode == CameraMode.ModeId.FirstPerson) |
| | 45 | | { |
| 122 | 46 | | CommonScriptableObjects.cameraForward.OnChange += UpdateForward; |
| 122 | 47 | | nextParent = firtPersonParent; |
| | 48 | |
|
| 122 | 49 | | if (cameraController != null) |
| 106 | 50 | | cameraController.onCameraBlendFinished += SetNextParent; |
| 106 | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| 42 | 54 | | if (newMode == CameraMode.ModeId.ThirdPerson) |
| 24 | 55 | | transform.forward = initialParent.forward; |
| | 56 | |
|
| 42 | 57 | | nextParent = initialParent; |
| | 58 | |
|
| 42 | 59 | | if (cameraController != null) |
| 36 | 60 | | cameraController.onCameraBlendStarted += SetNextParent; |
| | 61 | | } |
| 58 | 62 | | } |
| | 63 | |
|
| | 64 | | private void SetNextParent() |
| | 65 | | { |
| 147 | 66 | | if (cameraController != null) |
| | 67 | | { |
| 126 | 68 | | cameraController.onCameraBlendFinished -= SetNextParent; |
| 126 | 69 | | cameraController.onCameraBlendStarted -= SetNextParent; |
| | 70 | | } |
| 147 | 71 | | transform.SetParent(nextParent); |
| 147 | 72 | | } |
| | 73 | | } |