Skip to content

Instantly share code, notes, and snippets.

@Glavak
Last active February 12, 2024 15:43
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Glavak/ada99b57023db3c941c5caebe42a70c5 to your computer and use it in GitHub Desktop.
Save Glavak/ada99b57023db3c941c5caebe42a70c5 to your computer and use it in GitHub Desktop.
Скрипт для поддержания постоянной ширины камеры в Unity, для туториала https://youtu.be/0cmxFjP375Y
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