using Unity.Cinemachine; using UnityEngine; public struct CameraSettingSingleValue{ // NOTE: Could add a locked variable here if needed?? public T originalValue; public T targetValue; public float smoothing; public float velocityRef; public Vector2 velocityRefV2; public CameraSettingSingleValue(float defaultSmoothing, T original = default(T)){ originalValue = original; targetValue = default(T); smoothing = defaultSmoothing; velocityRef = 0; velocityRefV2 = default; } public void Reset(){ targetValue = originalValue; } } public struct CameraSettingValues{ public CameraSettingSingleValue orbitFollowTopHeight; public CameraSettingSingleValue orbitFollowTopRadius; public CameraSettingSingleValue orbitFollowCenterHeight; public CameraSettingSingleValue orbitFollowCenterRadius; public CameraSettingSingleValue orbitFollowBottomHeight; public CameraSettingSingleValue orbitFollowBottomRadius; public CameraSettingSingleValue rotationComposerScreenPos; public CameraSettingValues(float defaultSmoothing){ orbitFollowTopHeight = new CameraSettingSingleValue(defaultSmoothing); orbitFollowTopRadius = new CameraSettingSingleValue(defaultSmoothing); orbitFollowCenterHeight = new CameraSettingSingleValue(defaultSmoothing); orbitFollowCenterRadius = new CameraSettingSingleValue(defaultSmoothing); orbitFollowBottomHeight = new CameraSettingSingleValue(defaultSmoothing); orbitFollowBottomRadius = new CameraSettingSingleValue(defaultSmoothing); rotationComposerScreenPos = new CameraSettingSingleValue(defaultSmoothing); } } public class CameraSettingsProcessor : MonoBehaviour{ public static CameraSettingsProcessor Instance{ get; private set; } public static CameraSettingValues values = new(defaultSmoothing: .2f); public static GameObject mainCamera; private CinemachineOrbitalFollow orbit; private CinemachineRotationComposer rotComp; public void Awake(){ if (Instance != null && Instance != this) { Destroy(this); } else { Instance = this; } mainCamera = gameObject; orbit = mainCamera.GetComponent(); rotComp = mainCamera.GetComponent(); StashCameraOriginalSettings(this); SetTargetsToOriginalValues(this); } public static void StashCameraOriginalSettings(CameraSettingsProcessor settings){ values.orbitFollowTopHeight.originalValue = settings.orbit.Orbits.Top.Height; values.orbitFollowTopRadius.originalValue = settings.orbit.Orbits.Top.Radius; values.orbitFollowCenterHeight.originalValue = settings.orbit.Orbits.Center.Height; values.orbitFollowCenterRadius.originalValue = settings.orbit.Orbits.Center.Radius; values.orbitFollowBottomHeight.originalValue = settings.orbit.Orbits.Bottom.Height; values.orbitFollowBottomRadius.originalValue = settings.orbit.Orbits.Bottom.Radius; values.rotationComposerScreenPos.originalValue = settings.rotComp.Composition.ScreenPosition; } public static void SetTargetsToOriginalValues(CameraSettingsProcessor settings){ values.orbitFollowTopHeight.Reset(); values.orbitFollowTopRadius.Reset(); values.orbitFollowCenterHeight.Reset(); values.orbitFollowCenterRadius.Reset(); values.orbitFollowBottomHeight.Reset(); values.orbitFollowBottomRadius.Reset(); values.rotationComposerScreenPos.Reset(); } void ProcessCameraValues(){ orbit.Orbits.Top.Height = Mathf.SmoothDamp(orbit.Orbits.Top.Height, values.orbitFollowTopHeight.targetValue, ref values.orbitFollowTopHeight.velocityRef, values.orbitFollowTopHeight.smoothing); orbit.Orbits.Top.Radius = Mathf.SmoothDamp(orbit.Orbits.Top.Radius, values.orbitFollowTopRadius.targetValue, ref values.orbitFollowTopRadius.velocityRef, values.orbitFollowTopRadius.smoothing); orbit.Orbits.Center.Height = Mathf.SmoothDamp(orbit.Orbits.Center.Height, values.orbitFollowCenterHeight.targetValue, ref values.orbitFollowCenterHeight.velocityRef, values.orbitFollowCenterHeight.smoothing); orbit.Orbits.Center.Radius = Mathf.SmoothDamp(orbit.Orbits.Center.Radius, values.orbitFollowCenterRadius.targetValue, ref values.orbitFollowCenterRadius.velocityRef, values.orbitFollowCenterRadius.smoothing); orbit.Orbits.Bottom.Height = Mathf.SmoothDamp(orbit.Orbits.Bottom.Height, values.orbitFollowBottomHeight.targetValue, ref values.orbitFollowBottomHeight.velocityRef, values.orbitFollowBottomHeight.smoothing); orbit.Orbits.Bottom.Radius = Mathf.SmoothDamp(orbit.Orbits.Bottom.Radius, values.orbitFollowBottomRadius.targetValue, ref values.orbitFollowBottomRadius.velocityRef, values.orbitFollowBottomRadius.smoothing); rotComp.Composition.ScreenPosition = Vector2.SmoothDamp(rotComp.Composition.ScreenPosition, values.rotationComposerScreenPos.targetValue, ref values.rotationComposerScreenPos.velocityRefV2, values.rotationComposerScreenPos.smoothing); } void Update(){ ProcessCameraValues(); } }