Home > front end >  Camera Zoom unity
Camera Zoom unity

Time:05-15

I am using a script which zooms camera in and out, but I have added other cameras povs to the game, so I am trying to make the script work for the current selected camera.



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);

    }
}

CodePudding user response:

This script updates the current camera. Run it at start() and when switching game cameras.

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