using System.Collections.Generic; using Unity.Cinemachine; using UnityEngine; using UnityEngine.Rendering; public class AimCameraRig : CameraRigBase { public InputAxis AimMode = InputAxis.DefaultMomentary; //누르는 동안만 유지 [SerializeField] private CinemachineCamera _aimCamera; [SerializeField] private CinemachineCamera _freeCamera; //CameraRigBase에 전달용 private List _myCameras = new List(); protected override IReadOnlyList CameraCandidates => _myCameras; public CinemachineCamera ActiveCmCamera => LiveChild as CinemachineCamera; private bool _isAiming => AimMode.Value > 0.5f; private float _lastKnownFOV = 60f; protected override void Awake() { base.Awake(); _myCameras.Clear(); if (_aimCamera != null) _myCameras.Add(_aimCamera); if (_freeCamera != null) _myCameras.Add(_freeCamera); } protected override void Start() { base.Start(); if (_aimCamera == null || _freeCamera == null) { Debug.LogError($"{gameObject.name}: 프리팹 인스펙터에서 카메라 할당이 되지 않았습니다."); return; } CinemachineCamera topPriorityCam = GetHighestPriorityCamera() as CinemachineCamera; CinemachineCamera currentTargetCam = (LiveChild as CinemachineCamera) ?? topPriorityCam; if (currentTargetCam != null && currentTargetCam.Follow != null) { _controller = currentTargetCam.Follow.GetComponentInChildren(); } if (_controller == null) { Debug.LogWarning($"현재 카메라의 Follow 대상을 찾을 수 없습니다."); } } public override void GetInputAxes(List axes) { base.GetInputAxes(axes); axes.Add(new() { DrivenAxis = () => ref AimMode, Name = "Aim" }); } protected override CinemachineVirtualCameraBase ChooseCurrentCamera(Vector3 worldUp, float deltaTime) { var oldCam = (CinemachineVirtualCameraBase)LiveChild; var newCam = _isAiming ? _aimCamera : _freeCamera; if (_controller != null && oldCam != newCam) { _controller.RotationMode = _isAiming ? PlayerCharacterController.PlayerRotationMode.CameraCoupled : PlayerCharacterController.PlayerRotationMode.CameraDecoupled; _controller.RecenterPlayer(); } return newCam; } public float CurrentFOV { get { if (ActiveCmCamera != null) _lastKnownFOV = ActiveCmCamera.Lens.FieldOfView; return _lastKnownFOV; } set { if (ActiveCmCamera != null) ActiveCmCamera.Lens.FieldOfView = value; } } }