镜头拉远拉近

镜头拉远拉近

镜头拉远拉近,类似狙的镜头缩放

  1. 在场景中创建一个cube,默认fieldOfView为60
  2. 在main camera中添加一个脚本 CameraZoom
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using UnityEngine;

public class CameraZoom : MonoBehaviour
{
    private Camera mainCamera;

    public int[] zoomLevel = {60, 40, 20};

    private int currentLevel;


    private void Start()
    {
        mainCamera = GetComponent<Camera>();
    }


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            currentLevel += 1;
            if (currentLevel >= zoomLevel.Length)
            {
                currentLevel = 0;
            }
        }

        int currentFieldOfView = zoomLevel[currentLevel];

        mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, currentFieldOfView, 0.1f);
    }
}
  1. 此脚本的功能是可以在编辑器中自定义缩放等级,不设置的话默认是60,40,20共3个等级。当按下鼠标左键的时候缩放等级+1,到了最大缩放等级回到0
  2. 效果如下
署名 - 非商业性使用 - 禁止演绎 4.0