C# 变焦摄像机视场

C# 变焦摄像机视场,c#,unity3d,C#,Unity3d,我想知道如何使用c#在Unity3d中平滑地放大和缩小按钮。我已经有了缩小部分,但不知道如何使放大和缩小平滑过渡。举个例子,我希望它能像在ARMA或DayZ游戏中一样平滑地放大 这是我的密码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class zoomIN : MonoBehaviour { public Camera cam; // Use

我想知道如何使用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;
        }

    }
}
我将感谢任何帮助!
谢谢,圣诞快乐

获得平滑缩放动画的简单方法是在多个帧上执行缩放操作。因此,不要立即将
fieldOfView
从20更改为60,而是将
fieldOfView
每帧增加5,直到达到60的目标。(要延长动画,当然可以使用小于5的数字。)因此,根据鼠标输入,可以保留一个状态
\u zoomedIn
,并根据该状态和当前
视野
,可以确定是否仍然需要添加或删除该值。这将为您提供如下代码:(未测试)

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类zoomIN:MonoBehavior{
公共摄像机;
private bool_zoomedIn=false;
私有int_zoomedInTarget=60;
私有int_zoomedOutTarget=20;
//每帧调用一次更新
无效更新(){
if(Input.GetMouseButtonDown(1))
_zoomedIn=true;
if(Input.GetMouseButtonUp(1)){
_zoomedIn=假;
}
如果(_zoomedIn){
如果(cam.fieldOfView<\u ZoomedintTarget)
cam.fieldOfView+=5;
}否则{
如果(cam.fieldOfView>\u ZoomeOutTarget)
cam.fieldOfView-=5;
}
}

使用协同程序来执行此操作。您可以使用它来启用缩放的速度或持续时间。根据按键是按下还是松开,在
cam.fieldOfView
和目标(
20
还是
60
)之间执行
Mathf.Lerp

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

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;
    }
}
公共摄像机;
共青虫红素;
//每帧调用一次更新
无效更新()
{
if(Input.GetMouseButtonDown(1))
{
//停止旧的协同程序
if(zoomCoroutine!=null)
StopCoroutine(zoomCoroutine);
//在1秒内启动新的协同程序并进行缩放
zoomCoroutine=开始常规(lerpFieldOfView(cam,20,1f));
}
if(Input.GetMouseButtonUp(1))
{
//停止旧的协同程序
if(zoomCoroutine!=null)
StopCoroutine(zoomCoroutine);
//在1秒内启动新的协同程序并进行缩放
zoomCoroutine=开始常规(lerpFieldOfView(cam,60,1f));
}
}
IEnumerator lerpFieldOfView(摄影机目标摄影机、浮动toFOV、浮动持续时间)
{
浮点计数器=0;
float fromFOV=targetCamera.fieldOfView;
while(计数器<持续时间)
{
计数器+=时间增量时间;
浮动fOVTime=计数器/持续时间;
Debug.Log(fOVTime);
//改变视野
targetCamera.fieldOfView=Mathf.Lerp(fromFOV、toFOV、fOVTime);
//等一帧
收益返回空;
}
}

这似乎正是我想要的。但有一个小问题。当游戏开始时,它已经开始放大。但在单击鼠标右键后,它恢复正常,然后工作正常。我尝试在开始函数中使用_zoomedIO=false;但出于某种原因它似乎没有这样做。我做错了吗?哈哈哈顺便说一句,nks是的,正是我想要的。再次感谢,朋友,谢谢。圣诞快乐!
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;
    }
}