UnityGameProjectsCode / AbandondedGame / CameraZoom.cs
CameraZoom.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class CameraZoom : MonoBehaviour
{
    public float zoomAmt;
    private float timeSinceKeyPress;
    private float timeSinceKeyRelease;
    private bool zoomingIn;
    private bool zoomingOut;
    private float baseFOV;
    private float zoomFOV;
    private Camera playerCam;

    private void Start()
    {
        playerCam = GetComponent<Camera>();
        baseFOV = playerCam.fieldOfView;
        zoomFOV = baseFOV - zoomAmt;
    }

    private void Update()
    {
        if (playerCam.fieldOfView != zoomFOV)
        {
            if (Input.GetKey(KeyCode.Mouse1))
            {
                zoomingOut = false;

                if(!zoomingIn)
                {
                    timeSinceKeyPress = 0.0f;
                    zoomingIn = true;
                }

                timeSinceKeyPress += Time.deltaTime;

                playerCam.fieldOfView = Mathf.Lerp(playerCam.fieldOfView, zoomFOV, timeSinceKeyPress);
            }
        }

        if(playerCam.fieldOfView != baseFOV)
        {
            if (!Input.GetKey(KeyCode.Mouse1))
            {
                zoomingIn = false;

                if (!zoomingOut)
                {
                    timeSinceKeyPress = 0.0f;
                    zoomingOut = true;
                }

                timeSinceKeyPress += Time.deltaTime;

                playerCam.fieldOfView = Mathf.Lerp(playerCam.fieldOfView, baseFOV, timeSinceKeyPress);
            }
        }
    }

    public void SetNewFOV(float newFOV)
    {
        baseFOV = newFOV;
        zoomFOV = baseFOV - zoomAmt;
    }
}