相机变焦统一

Camera Zoom unity

我正在使用放大和缩小相机的脚本,但我已将其他相机添加到游戏中 POVs,因此我正在尝试使脚本适用于当前选定的相机。

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

public class cameraZoomController : MonoBehaviour
{
    private Camera cam;
    private float targetZoom;
    private float zoomFactor = 2f;
    [SerializeField] private float zoomLerpSpeed = 10;

    void Start()
    {
        cam = Camera.main;
        targetZoom = cam.orthographicSize;
    }

    // Update is called once per frame
    void Update()
    {
        float scrollData;
        scrollData = Input.GetAxis("Mouse ScrollWheel");
        //debug.log(scrollData);

        targetZoom = targetZoom - scrollData * zoomFactor;
        targetZoom = Mathf.Clamp(targetZoom, 0f, 10f);
        cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * zoomLerpSpeed);
    }
}

此脚本更新当前相机。 运行 它在 start() 和切换游戏相机时。

void UpdateCurrent() => cam = Camera.allCameras[Camera.allCamerasCount - 1];