首页 文章

变焦相机FOV超时

提问于
浏览
1

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

    }
}

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

2 回答

  • 2

    使用协程执行此操作 . 您可以使用它来启用缩放的速度或持续时间 . 根据是否按下或释放按键,在 cam.fieldOfView 和目的地( 2060 )之间执行 Mathf.Lerp .

    注意:您必须将 Input.GetMouseButton 更改为 Input.GetMouseButtonDown ,否则在按住鼠标右键的同时,每帧都会运行第一个 if 语句 . 我想你想成为真正的 once .

    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;
        }
    }
    
  • 1

    获得平滑缩放动画的一种简单方法是在多个帧上执行缩放操作 . 因此,不要立即将 fieldOfView 从20更改为60,每帧增加 fieldOfView ,直到达到60的目标为止 . (为了延长动画,你当然可以使用小于5的数字 . )所以基于鼠标输入您可以保持状态 _zoomedIn 并根据该状态和当前 fieldOfView ,您可以确定是否仍需要添加或减去该值 . 这给你类似下面的代码:(未测试)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class zoomIN : MonoBehaviour {
    
        public Camera cam;
        private bool _zoomedIn = false;
        private int _zoomedInTarget = 60;
        private int _zoomedOutTarget = 20;
    
        // Update is called once per frame
        void Update () {
    
            if (Input.GetMouseButtonDown (1))
                _zoomedIn = true;
    
            if (Input.GetMouseButtonUp (1)) {
                _zoomedIn = false;
            }
    
            if (_zoomedIn) {
                if (cam.fieldOfView < _zoomedInTarget)
                    cam.fieldOfView += 5;
            } else {
                if (cam.fieldOfView > _zoomedOutTarget)
                    cam.fieldOfView -= 5;
            }
        }
    

相关问题