unity学习知识(2)--镜头的远近变化代码(狙击镜)

设置Camera的FOV属性实现:

按下镜头拉近,再按下拉远

逐渐拉近

缩放等级变化

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CareamZoom : MonoBehaviour
{
    // Start is called before the first frame update

    private Camera camera;
    void Start()
    {
        camera = GetComponent<Camera>();
    }
    private bool isFar =true;

    // Update is called once per frame
    void Update()
    {
        plan1();

    }

    private void plan1()
    {
        if (Input.GetMouseButtonDown(1))
        {
            //拉远60
            isFar = !isFar;
            if (isFar)
            {
                camera.fieldOfView = 60;
            }
            //拉近20
            else
                camera.fieldOfView = 20;
        }
    }
    private void plan2()
    {
        if (Input.GetMouseButtonDown(1))
        {
            //拉远60
            isFar = !isFar;
            if (isFar)
            {
                //if(camera.fieldOfView<60)
                //camera.fieldOfView += 2;
                camera.fieldOfView= Mathf.Lerp(camera.fieldOfView, 60, 0.1f);//起点,终点,比例 但不会等于终点
                if (Mathf.Abs(camera.fieldOfView - 60) < 0.1f)
                    camera.fieldOfView = 60;

            }
            //拉近20
            else
                if (camera.fieldOfView >20)
                camera.fieldOfView -= 2;
            /* camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 20, 0.1f);
            if (Mathf.Abs(camera.fieldOfView - 20) < 0.1f)
                camera.fieldOfView = 20;*/
        }
    }
}

 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页