How To Add Camera Zoom In Unity: Best Guide

Today we’ll see how we can add camera zoom in unity using the scroll wheel. Its pretty easy to do it, you just have to change the camera component properties according to the player’s input.

I’ve recognized the impact that dynamic camera controls can have on player experience. Unity camera controls, particularly Unity zoom functionality, are pivotal in creating immersive and interactive worlds.

My objective with this Unity camera zoom tutorial is to teach you to enhance your game development in Unity by mastering the art of camera zoom. 

Difference Between Camera Modes In Unity

As we dive deeper into the intricacies of Unity’s camera system, it’s essential to understand its components and properties. You should also know the difference between camera types, namely perspective and orthographic.

Each aspect plays a critical role and understanding them is key for any game developer to create a visually stimulating experience in their game.

What Is Perspective Mode?

Perspective mode is the default mode for Unity cameras. It simulates the way the human eye perceives depth and distance, making objects closer to the camera appear larger and It is mainly used in 3D games.

What Is Orthographic Mode?

Orthographic mode projects the scene onto the camera’s view without simulating perspective. In this mode, all objects appear the same size regardless of their distance from the camera and It is used in 2D games.

It is essential to understand and choose Unity’s camera options carefully if you want your project to have the right gameplay and visual style. The kind of game, the intended visual aesthetics, and the particular project needs all affect the camera mode selection.

Implementing Camera Zoom In Unity

We will use the scroll wheel to control the zoom level, clamp the values between minimum and maximum limits, and apply the SmoothDamp function to achieve a polished and visually appealing result.

Camera Zoom In Unity For 2D

So, let’s start by implementing camera zoom using scroll wheel for 2D environment or you can say for Orthographic camera.

And if you have not created player controller for your platformer make sure to create one.

Step 1: Create Variables

Create a new C# script, name it “CameraZoom”, and attach it to the main camera in the scene.

In your script, create variables to store the minimum and maximum zoom levels, as well as the speed of the zoom. Here is an example in C#:

[SerializeField] private Camera cam;
public float zoomMultiplier = 5f;
public float minZoom = 2f;
public float maxZoom = 10f;
public float velocity = 0.5f;
public float smoothTime = 0.3f;
private float targetZoom;

Step 2: Initializing Things

First we should store our camera’s current zoom value in the targetZoom variable and also reference our camera in the start function.

void Start()
{
    if (cam == null)
    {
        cam = Camera.main;
    }

    targetZoom = cam.orthographicSize;
}

Step 3: Handling Input

Let’s take input from the player using unity’s legacy input system and store that input in a float variable as the input will be from the scroll wheel.

And set the targetZoom variable negative equal to scrollInput variable multiplyed by zoomSpeed.

Also do clamp the targetZoom variable’s value between minZoom and maxZoom so that we can zoom to infinity.

Up until now the targetZoom is not set equal to the camera’s actual zoom, we’ll do that in a moment.

void Update()
{
    float scrollInput = Input.GetAxis("Mouse ScrollWheel");
    targetZoom -= scrollInput * zoomMultiplier;
    targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);
}

Step 4: Apply Smooth Damp

Now, use the SmoothDamp function to smoothly interpolate between the current camera orthographic size and the targetZoom. This helps to avoid sudden jumps in zoom levels and creates a more visually pleasing effect.

And, do all this in the LateUpdate() because we’re dealing with camera operations.

void LateUpdate()
{
    float currentZoom = Mathf.SmoothDamp(Camera.main.orthographicSize, targetZoom, ref velocity, smoothTime);
    Camera.main.orthographicSize = currentZoom;
}

Now, try and test the code you should be able to zoom in and out of the scene’s camera view using the scroll wheel.

Camera Zoom In Unity For 3D

Now, if you have a perspective camera then replace the orthographicSize with fieldOfView in the script and adjust the values of the variables at the top.

camera zoom in unity

Change the value of the variables too:

change the value of the variables too

Here is the complete code for 3D:

using UnityEngine;

public class CameraZoom : MonoBehaviour
{
    [SerializeField] private Camera cam;
    public float zoomMultiplier = 5f;
    public float minZoom = 30f;
    public float maxZoom = 90f;
    public float velocity = 0.5f;
    public float smoothTime = 0.3f;
    private float targetZoom;
    // Start is called before the first frame update
    void Start()
    {        
        if (cam == null)
        {
            cam = Camera.main;
        }

        targetZoom = cam.fieldOfView;
        
    }

    // Update is called once per frame
    void Update()
    {
        float scrollInput = Input.GetAxis("Mouse ScrollWheel");
        targetZoom -= scrollInput * zoomMultiplier;
        targetZoom = Mathf.Clamp(targetZoom, minZoom, maxZoom);
    }

    void LateUpdate()
    {
        float currentZoom = Mathf.SmoothDamp(Camera.main.fieldOfView, targetZoom, ref velocity, smoothTime);
        Camera.main.fieldOfView = currentZoom;
    }


}

FAQs

How can I prevent the camera from zooming beyond certain limits?

The “minZoom” and “maxZoom” variables define the minimum and maximum zoom levels, respectively. The script uses these values to clamp the zoom within the specified range.

Why is the LateUpdate method used for applying the zoom?

LateUpdate is used to ensure that the camera’s zoom is updated after all other Update functions, preventing any interference with other scripts that might manipulate the camera’s properties during the frame.

What happens if I don’t assign a camera in the Unity Editor?

If no camera is assigned in the Unity Editor, the script defaults to using the main camera (Camera.main).

How can I integrate this script into an existing Unity project?

Simply attach the script to any GameObject in your scene. The script automatically handles the camera, and you can tweak the variables in the Inspector to suit your project’s needs.

What is SmoothDamp in Unity?

SmoothDamp is a function in Unity used for smooth interpolation between two values over time. Specifically, it is commonly employed to create smooth and gradual transitions in movement or size adjustments.

Conclusion

Now, by following these steps, you can implement the feature of camera zoom in Unity 2D and 3D using the scroll wheel, clamp the values between specified limits, and utilize the SmoothDamp function for a more refined user experience. Experiment with different values to achieve the desired level of smoothness and responsiveness to have a smooth camera zoom in unity for your game.