变焦相机FOV超时(Zoom camera FOV overtime)

编程入门 行业动态 更新时间:2024-10-25 14:35:22
变焦相机FOV超时(Zoom camera FOV overtime)

我想知道如何使用c#平滑放大并平滑缩小Unity3d中的按钮按下。 我已经缩小了部分,但不知道如何进行放大和缩小的过渡。 作为一个例子,我希望它能像ARMA或DayZ游戏一样平滑放大。

这是我的代码:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class zoomIN : MonoBehaviour { public Camera cam; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButton (1)) { cam.fieldOfView = 20; } if (Input.GetMouseButtonUp (1)) { cam.fieldOfView = 60; } } }

我很感激任何帮助! 谢谢,圣诞快乐!

I'm wondering how to smoothly zoom in and smoothly zoom out on button press in Unity3d using c#. I've got zooming part down already, but not sure how to make a transition of zooming in and out smooth. As an example, I'd like it to zoom in as smooth as it is in ARMA or DayZ game.

Here's my code:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class zoomIN : MonoBehaviour { public Camera cam; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButton (1)) { cam.fieldOfView = 20; } if (Input.GetMouseButtonUp (1)) { cam.fieldOfView = 60; } } }

I'd appreciate any help! Thanks and Merry Xmas!

最满意答案

使用协同程序来执行此操作。 您可以使用它来启用缩放的速度或持续时间。 在cam.fieldOfView和目标( 20或60 )之间执行Mathf.Lerp ,具体取决于是否按下或释放了键。

注意:您必须将Input.GetMouseButton更改为Input.GetMouseButtonDown否则您的第一个if语句将在按住鼠标右键的同时每帧运行。 我想你只想做一次

public Camera cam; Coroutine zoomCoroutine; // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(1)) { //Stop old coroutine if (zoomCoroutine != null) StopCoroutine(zoomCoroutine); //Start new coroutine and zoom within 1 second zoomCoroutine = StartCoroutine(lerpFieldOfView(cam, 20, 1f)); } if (Input.GetMouseButtonUp(1)) { //Stop old coroutine if (zoomCoroutine != null) StopCoroutine(zoomCoroutine); //Start new coroutine and zoom within 1 second zoomCoroutine = StartCoroutine(lerpFieldOfView(cam, 60, 1f)); } } IEnumerator lerpFieldOfView(Camera targetCamera, float toFOV, float duration) { float counter = 0; float fromFOV = targetCamera.fieldOfView; while (counter < duration) { counter += Time.deltaTime; float fOVTime = counter / duration; Debug.Log(fOVTime); //Change FOV targetCamera.fieldOfView = Mathf.Lerp(fromFOV, toFOV, fOVTime); //Wait for a frame yield return null; } }

Use coroutine to do this. You can use it to enable the speed or duration of the zooming. Perform a Mathf.Lerp between cam.fieldOfView and the destination( 20 or 60) depending on if the key is pressed or released.

Note: You must change Input.GetMouseButton to Input.GetMouseButtonDown otherwise your first if statement will be running every frame while the right mouse button is held down. I think you want to be true once only.

public Camera cam; Coroutine zoomCoroutine; // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(1)) { //Stop old coroutine if (zoomCoroutine != null) StopCoroutine(zoomCoroutine); //Start new coroutine and zoom within 1 second zoomCoroutine = StartCoroutine(lerpFieldOfView(cam, 20, 1f)); } if (Input.GetMouseButtonUp(1)) { //Stop old coroutine if (zoomCoroutine != null) StopCoroutine(zoomCoroutine); //Start new coroutine and zoom within 1 second zoomCoroutine = StartCoroutine(lerpFieldOfView(cam, 60, 1f)); } } IEnumerator lerpFieldOfView(Camera targetCamera, float toFOV, float duration) { float counter = 0; float fromFOV = targetCamera.fieldOfView; while (counter < duration) { counter += Time.deltaTime; float fOVTime = counter / duration; Debug.Log(fOVTime); //Change FOV targetCamera.fieldOfView = Mathf.Lerp(fromFOV, toFOV, fOVTime); //Wait for a frame yield return null; } }

更多推荐

本文发布于:2023-08-04 01:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变焦   相机   FOV   overtime   camera

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!