Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Note:
- Attach this script to the player or object you want to zoom in on.
- You can go into the inspector and change the camera zoom to a different numerical value if you like.
- Left click is to zoom in, then left click again to zoom out.
- */
- using UnityEngine;
- public class UnityCameraZoom : MonoBehaviour {
- // We made a public int that will set the new field of view for the camera.
- public int cameraZoom = 20;
- // We made a public int that is the current field of view for the camera.
- private int cameraNormal = 60;
- // How fast the camera will zoom in and out.
- private float cameraSmooth = 5f;
- // A bool to declare if the camera is zoomed.
- private bool cameraZoomed = false;
- // Declareing the camera
- private Camera playerCamera;
- private void Start() {
- // on start we get the componenet camera.
- playerCamera = GetComponent<Camera>();
- }
- private void Update() {
- // If the player presses left mouse button
- if (Input.GetMouseButtonDown(0))
- {
- // then flip the boolean, so if its true set false, if false then set it to true..
- cameraZoomed = !cameraZoomed;
- }
- // if the camera zoom is set to true
- if (cameraZoomed) {
- // set the players camera field of view equal to:
- // the camera fov value, then get the value of the zoom, and find the difference and change it slowly.
- // The speed at which we change it, is called cameraSmooth.
- // We use Time.DeltaTime so the zoom doesn't happen instantly. If you was to remove it, it would do it in 1 frame.
- // instead of multiple frames which would remove the smooth like feature when zooming in..
- playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, cameraZoom, Time.deltaTime * cameraSmooth);
- } else /*else if its not true*/ {
- // players camera fov is equal to:
- // the current fov of the players camera, then we set it back to noraml which is 60, then change the difference slowly
- // with cameraSmooth again.
- playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, cameraNormal, Time.deltaTime * cameraSmooth);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement