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 Vector3 velocityRefV3; public CameraSettingSingleValue(float defaultSmoothing, T original = default(T)){ originalValue = original; targetValue = original; smoothing = defaultSmoothing; velocityRef = 0; velocityRefV2 = default; velocityRefV3 = default; } public void Reset(){ targetValue = originalValue; } } public struct CameraSettingValues{ public CameraSettingSingleValue mainFieldOfView; public CameraSettingSingleValue orbitPositionDamping; public CameraSettingSingleValue orbitFollowTopHeight; public CameraSettingSingleValue orbitFollowTopRadius; public CameraSettingSingleValue orbitFollowCenterHeight; public CameraSettingSingleValue orbitFollowCenterRadius; public CameraSettingSingleValue orbitFollowBottomHeight; public CameraSettingSingleValue orbitFollowBottomRadius; public CameraSettingSingleValue rotationComposerScreenPos; public CameraSettingSingleValue cameraOffsetOffset; public CameraSettingValues(float defaultSmoothing){ mainFieldOfView = new CameraSettingSingleValue(defaultSmoothing); orbitPositionDamping = new CameraSettingSingleValue(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); cameraOffsetOffset = 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 CinemachineCamera main; private CinemachineOrbitalFollow orbit; private CinemachineRotationComposer rotComp; private CinemachineCameraOffset offset; public void Awake(){ // Singleton management if (Instance != null && Instance != this) { Destroy(this); } else { Instance = this; } // Set references for camera object and cinemachine components mainCamera = gameObject; main = mainCamera.GetComponent(); orbit = mainCamera.GetComponent(); rotComp = mainCamera.GetComponent(); offset = mainCamera.GetComponent(); // Initialize camera settings values values = new CameraSettingValues{ cameraOffsetOffset = new CameraSettingSingleValue(defaultSmoothing: .2f, offset.Offset), mainFieldOfView = new CameraSettingSingleValue(defaultSmoothing: .2f, main.Lens.FieldOfView), orbitPositionDamping = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.TrackerSettings.PositionDamping), orbitFollowTopHeight = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Top.Height), orbitFollowTopRadius = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Top.Radius), orbitFollowCenterHeight = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Center.Height), orbitFollowCenterRadius = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Center.Radius), orbitFollowBottomHeight = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Bottom.Height), orbitFollowBottomRadius = new CameraSettingSingleValue(defaultSmoothing: .2f, orbit.Orbits.Bottom.Radius), rotationComposerScreenPos = new CameraSettingSingleValue(defaultSmoothing: .2f, rotComp.Composition.ScreenPosition), }; } void ProcessCameraValues(){ main.Lens.FieldOfView = Mathf.SmoothDamp(main.Lens.FieldOfView, values.mainFieldOfView.targetValue, ref values.mainFieldOfView.velocityRef, values.mainFieldOfView.smoothing); orbit.TrackerSettings.PositionDamping = Vector3.SmoothDamp(orbit.TrackerSettings.PositionDamping, values.orbitPositionDamping.targetValue, ref values.orbitPositionDamping.velocityRefV3, values.orbitPositionDamping.smoothing); 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); offset.Offset = Vector3.SmoothDamp(offset.Offset, values.cameraOffsetOffset.targetValue, ref values.cameraOffsetOffset.velocityRefV3, values.cameraOffsetOffset.smoothing); } void Update(){ ProcessCameraValues(); } }