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