Last active
October 18, 2024 11:54
-
-
Save Glavak/ada99b57023db3c941c5caebe42a70c5 to your computer and use it in GitHub Desktop.
Скрипт для поддержания постоянной ширины камеры в Unity, для туториала https://youtu.be/0cmxFjP375Y
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Keeps constant camera width instead of height, works for both Orthographic & Perspective cameras | |
/// Made for tutorial https://youtu.be/0cmxFjP375Y | |
/// </summary> | |
public class CameraConstantWidth : MonoBehaviour | |
{ | |
public Vector2 DefaultResolution = new Vector2(720, 1280); | |
[Range(0f, 1f)] public float WidthOrHeight = 0; | |
private Camera componentCamera; | |
private float initialSize; | |
private float targetAspect; | |
private float initialFov; | |
private float horizontalFov = 120f; | |
private void Start() | |
{ | |
componentCamera = GetComponent<Camera>(); | |
initialSize = componentCamera.orthographicSize; | |
targetAspect = DefaultResolution.x / DefaultResolution.y; | |
initialFov = componentCamera.fieldOfView; | |
horizontalFov = CalcVerticalFov(initialFov, 1 / targetAspect); | |
} | |
private void Update() | |
{ | |
if (componentCamera.orthographic) | |
{ | |
float constantWidthSize = initialSize * (targetAspect / componentCamera.aspect); | |
componentCamera.orthographicSize = Mathf.Lerp(constantWidthSize, initialSize, WidthOrHeight); | |
} | |
else | |
{ | |
float constantWidthFov = CalcVerticalFov(horizontalFov, componentCamera.aspect); | |
componentCamera.fieldOfView = Mathf.Lerp(constantWidthFov, initialFov, WidthOrHeight); | |
} | |
} | |
private float CalcVerticalFov(float hFovInDeg, float aspectRatio) | |
{ | |
float hFovInRads = hFovInDeg * Mathf.Deg2Rad; | |
float vFovInRads = 2 * Mathf.Atan(Mathf.Tan(hFovInRads / 2) / aspectRatio); | |
return vFovInRads * Mathf.Rad2Deg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment