unity3d - 统一缩放脚本
问题描述
我使用这个简单的缩放脚本,改变相机的视野而不是移动相机(以避免剪裁),效果很好。
但是我想为其添加一个最大 FOV,例如 FOV 80 左右。也许还有一个最小 FOV(不如最大 FOV 重要)?
我可以修改这个脚本吗?
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
GetComponent<Camera>().fieldOfView--;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
GetComponent<Camera>().fieldOfView++;
}
}
}
解决方案
只需将所需的最小值和最大值作为成员添加到脚本中。然后你可以使用检查器来调整它们:
private Camera cam;
[SerializeField] private float minFov = 20;
[SerializeField] private float maxFov = 80;
void Awake()
{
cam = GetComponent<Camera>();
// ensures that the field of view is within min and max on startup
UpdateFov(cam.fieldOfView);
}
void Update()
{
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
if(scrollInput > 0)
UpdateFov(cam.fieldOfView - 1);
else if(scrollInput < 0)
UpdateFov(cam.fieldOfView + 1);
}
void UpdateFov(float newFov)
{
cam.fieldOfView = Mathf.Clamp(newFov, minFov, maxFov);
}
我还缓存了相机,因为GetComponent
调用相当昂贵。
推荐阅读
- dns - 从命令行设置 MX 记录
- dimple.js - 可以跳过在折线图中绘制数据点吗?
- powershell - Powershell - 打开 URL 并登录
- web - 我下载了一个网站到我的硬盘。无论如何下载链接的谷歌驱动器文件?
- image - asp.net core 3.1 Razor Pages 将图像作为字节 [] 加载到 View
- elasticsearch - 具有动态属性的字段上的 Elasticsearch 聚合
- mysql - 从 MySql 中获取唯一值
- python - 寻找另一种方法来避免 if 语句
- java - 如何使用由 Spring 管理的具有多个安全配置的单独自定义过滤器?
- asp.net - .Net Console.WriteLine 和 System.Diagnostics.Debug.WriteLine(); 不工作