This is a very simple code. Just create a new C# script named as CameraInOut, place this code in it, and add it to the main camera. It will start zooming in on the left click and zooming out on right-click.
using
System.Collections;
using
System.Collections.Generic;
using UnityEngine;
public class
CameraInOut : MonoBehaviour
{
public float[] FOVS = {45, 30, 15};
public float speed = 2f;
public bool zoomIn, zoomOut;
Camera myCamera;
int FOVpos = 0;
void Start()
{
myCamera =
GetComponent<Camera>();
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
ZoomIn();
}
if (Input.GetMouseButtonDown(1))
{
ZoomOut();
}
if(zoomIn)
{
myCamera.fieldOfView =
Mathf.Lerp(myCamera.fieldOfView, FOVS[FOVpos], speed * Time.deltaTime);
if(Mathf.Approximately(myCamera.fieldOfView, FOVS[FOVpos]))
{
zoomIn = false;
}
}
if (zoomOut)
{
myCamera.fieldOfView =
Mathf.Lerp(myCamera.fieldOfView, FOVS[FOVpos], speed * Time.deltaTime);
if
(Mathf.Approximately(myCamera.fieldOfView, FOVS[FOVpos]))
{
zoomOut = false;
}
}
}
void ZoomIn()
{
if (FOVpos >= FOVS.Length - 1)
{
FOVpos = FOVS.Length - 1;
}
else
{
FOVpos += 1;
}
zoomIn = true;
}
void ZoomOut()
{
if (FOVpos <= 0)
{
FOVpos = 0;
}
else
{
FOVpos -= 1;
}
zoomOut = true;
}
}
Comments
Post a Comment
Please feel free to comment. I would love to hear feedback.