Files
back_cantanBuilding/Assets/Scripts/UI/ShootingRange/ShootingRangeCameraAspectAdjustment.cs
2026-05-26 16:15:54 +08:00

31 lines
1.1 KiB
C#

using Cinemachine;
using Unity.Mathematics;
using UnityEngine;
public class ShootingRangeCameraAspectAdjustment : MonoBehaviour
{
[SerializeField] private CinemachineVirtualCamera virtualCamera;
[SerializeField] private float aspectUpperLimit = 2.2f;
private void Start()
{
if (virtualCamera == null)
{
Debug.LogError("No Virtual Camera found.", this);
return;
}
float aspect = (float)Screen.height / Screen.width;
// Debug.Log($"[FOV]height: {Screen.height}, width: {Screen.width}, aspect: {aspect}]");
var oldFov = virtualCamera.m_Lens.FieldOfView;
// Debug.Log($"[FOV]old fov: {oldFov}");
if (aspect >= 16.0 / 9)
{
var newFov = 2 * math.atan(math.tan(oldFov * 0.5f / 180 * math.PI) * 9 / 16 * (aspect < aspectUpperLimit ? aspect : aspectUpperLimit)) / math.PI * 180;
// Debug.Log($"[FOV]new fov raw{newFov}");
// while (newFov < 0)
// newFov += 180;
// Debug.Log($"[FOV]new fov{newFov}");
virtualCamera.m_Lens.FieldOfView = newFov;
}
}
}