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