| | 1 | | using Cinemachine; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCLFeatures.ScreencaptureCamera.CameraObject |
| | 6 | | { |
| | 7 | | public class ScreencaptureCameraMovement : MonoBehaviour |
| | 8 | | { |
| | 9 | | private const float MAX_DISTANCE_FROM_PLAYER = 16f; |
| | 10 | |
|
| | 11 | | [Header("TRANSLATION")] |
| 0 | 12 | | [SerializeField] private float translationSpeed = 5f; |
| | 13 | | private TranslationInputSchema translationInputSchema; |
| | 14 | |
|
| | 15 | | [Header("ROTATION")] |
| 0 | 16 | | [SerializeField] private float rotationSpeed = 100f; |
| 0 | 17 | | [SerializeField] private float maxRotationPerFrame = 1.5f; |
| 0 | 18 | | [SerializeField] private float rotationDamping = 7; |
| | 19 | | private RotationInputSchema rotationInputSchema; |
| | 20 | |
|
| | 21 | | private ScreencaptureCameraTranslation translation; |
| | 22 | | private ScreencaptureCameraRotation rotation; |
| | 23 | | private CharacterController target; |
| | 24 | |
|
| | 25 | | private bool isInitialized; |
| | 26 | | private CinemachineVirtualCamera virtualCamera; |
| | 27 | | private CinemachineBrain cinemachineBrain; |
| | 28 | | private Transform characterCamera; |
| | 29 | |
|
| 0 | 30 | | private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 0 | 31 | | private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| 0 | 32 | | private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| 0 | 33 | | private BaseVariable<Quaternion> cameraRotation => DataStore.i.camera.rotation; |
| | 34 | |
|
| | 35 | | public void Initialize(CharacterController cameraTarget, CinemachineVirtualCamera virtualCamera, Transform chara |
| | 36 | | { |
| 0 | 37 | | target = cameraTarget; |
| 0 | 38 | | this.virtualCamera = virtualCamera; |
| 0 | 39 | | characterCamera = characterCameraTransform; |
| | 40 | |
|
| 0 | 41 | | isInitialized = true; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | private void Awake() |
| | 45 | | { |
| 0 | 46 | | cinemachineBrain = GetComponent<CinemachineBrain>(); |
| | 47 | |
|
| 0 | 48 | | rotationInputSchema = new RotationInputSchema( |
| | 49 | | Resources.Load<InputAction_Measurable>("ScreenshotCameraXRotationAxis"), |
| | 50 | | Resources.Load<InputAction_Measurable>("ScreenshotCameraYRotationAxis"), |
| | 51 | | Resources.Load<InputAction_Hold>("MouseFirstClickDown") |
| | 52 | | ); |
| | 53 | |
|
| 0 | 54 | | translationInputSchema = new TranslationInputSchema( |
| | 55 | | Resources.Load<InputAction_Measurable>("ScreenshotCameraXTranslationAxis"), |
| | 56 | | Resources.Load<InputAction_Measurable>("ScreenshotCameraYTranslationAxis"), |
| | 57 | | Resources.Load<InputAction_Hold>("ScreenshotCameraUp"), |
| | 58 | | Resources.Load<InputAction_Hold>("ScreenshotCameraDown") |
| | 59 | | ); |
| | 60 | |
|
| 0 | 61 | | rotation = new ScreencaptureCameraRotation(rotationInputSchema); |
| 0 | 62 | | translation = new ScreencaptureCameraTranslation(translationInputSchema); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private void Update() |
| | 66 | | { |
| 0 | 67 | | if (!isInitialized) return; |
| | 68 | |
|
| 0 | 69 | | rotation.Rotate(target.transform, Time.deltaTime, rotationSpeed, rotationDamping, maxRotationPerFrame); |
| 0 | 70 | | translation.Translate(target, translationSpeed, MAX_DISTANCE_FROM_PLAYER, Time.deltaTime); |
| | 71 | |
|
| 0 | 72 | | UpdateDataStore(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | private void OnEnable() |
| | 76 | | { |
| 0 | 77 | | target.enabled = true; |
| 0 | 78 | | rotation.Activate(); |
| 0 | 79 | | ResetVirtualCameraToPlayerCamera(); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | private void OnDisable() |
| | 83 | | { |
| 0 | 84 | | rotation.Deactivate(); |
| 0 | 85 | | target.enabled = false; |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void UpdateDataStore() |
| | 89 | | { |
| 0 | 90 | | cameraForward.Set(transform.forward); |
| 0 | 91 | | cameraRight.Set(transform.right); |
| 0 | 92 | | cameraRotation.Set(transform.rotation); |
| 0 | 93 | | cameraPosition.Set(transform.position); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private void ResetVirtualCameraToPlayerCamera() |
| | 97 | | { |
| 0 | 98 | | CinemachineHardLockToTarget body = virtualCamera.GetCinemachineComponent<CinemachineHardLockToTarget>(); |
| 0 | 99 | | CinemachineSameAsFollowTarget composer = virtualCamera.GetCinemachineComponent<CinemachineSameAsFollowTarget |
| | 100 | |
|
| 0 | 101 | | body.m_Damping = 0; |
| 0 | 102 | | composer.m_Damping = 0; |
| | 103 | |
|
| 0 | 104 | | target.enabled = false; |
| 0 | 105 | | target.transform.SetPositionAndRotation(characterCamera.position, characterCamera.rotation); |
| 0 | 106 | | target.enabled = true; |
| | 107 | |
|
| 0 | 108 | | cinemachineBrain.ManualUpdate(); |
| 0 | 109 | | body.m_Damping = 1; |
| 0 | 110 | | composer.m_Damping = 1; |
| 0 | 111 | | } |
| | 112 | | } |
| | 113 | | } |