Files
Genesis_Unity/Assets/02_Scripts/Player/Camera/AimCameraRig.cs

95 lines
3.1 KiB
C#

using System.Collections.Generic;
using Unity.Cinemachine;
using UnityEngine;
using UnityEngine.Rendering;
public class AimCameraRig : CameraRigBase
{
[SerializeField] private CinemachineCamera _aimCamera;
[SerializeField] private CinemachineCamera _freeCamera;
//CameraRigBase에 전달용
private List<CinemachineVirtualCameraBase> _myCameras = new List<CinemachineVirtualCameraBase>();
protected override IReadOnlyList<CinemachineVirtualCameraBase> CameraCandidates => _myCameras;
public CinemachineCamera ActiveCmCamera => LiveChild as CinemachineCamera;
private bool _isAiming => _controller != null && _controller.IsAiming;
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.GetComponentInParent<PlayerCharacterController>();
}
if (_controller == null)
{
Debug.LogWarning($"현재 카메라의 Follow 대상을 찾을 수 없습니다.");
}
}
public override void GetInputAxes(List<IInputAxisOwner.AxisDescriptor> axes)
{
base.GetInputAxes(axes);
}
protected override CinemachineVirtualCameraBase ChooseCurrentCamera(Vector3 worldUp, float deltaTime)
{
var oldCam = (CinemachineVirtualCameraBase)LiveChild;
var newCam = _isAiming ? _aimCamera : _freeCamera;
if (_controller != null && oldCam != newCam)
{
//에임모드에서 다시 돌아갈때 플레이어의 현재 회전값에 카메라를 일치시킴
if(newCam == _freeCamera)
{
CinemachineOrbitalFollow orbitalFollow = newCam.GetComponent<CinemachineOrbitalFollow>();
orbitalFollow.HorizontalAxis.Recentering.Enabled = true;
orbitalFollow.VerticalAxis.Recentering.Enabled = true;
orbitalFollow.HorizontalAxis.Recentering.Enabled = false;
orbitalFollow.VerticalAxis.Recentering.Enabled = false;
}
_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;
}
}
}