How to zoom a camera in Unity
The 3 main methods for zooming a camera in unity are Perspective Zoom, Movement Zoom, and 2D orthographic Zoom. Each zoom has its own individual purpose.
- Perspective Zoom works to change a camera’s field of view. This method creates a telescopic effect.
- Movement Zoom physically moves the camera object closer to the subject. This method is used to adjust the position of a third-person camera and a top-down camera angle.
- 2D orthographic zoom increases or decreases the viewable area of a 2D camera by changing its orthographic size. Let’s cover the basics first.
The foundation of zooming a camera in Unity is quite simple. To create a key zoom effect, all that is needed is to reduce the camera’s Field of View property. By doing this, the field of view creates a telescopic view as if the player is looking at their environment through a magnifying glass. While this helps create a sniper effect in most games, this effect is not the same as moving closer to an object. It’s important to keep in mind the players’ needs when creating and manufacturing the zoom functions. For many reasons, this is why games usually use a default setting so that the players are able to configure what suits them best. ( as motion sickness may be an issue)
There are two main ways to smoothly change the value in Unity. The first being by a time where movement takes place over a fixed duration, and by speed, where the duration varies but the speed of movement is consistent. When smoothing a value by speed, this involved the Move Towards function. This allows for increments to be set at a value towards a target at a constant rate. However, instead of passing at a fixed speed you can calculate the amount of angle change needed for the full movement and divide this by the duration wanted. This will give you a speed value. Why is this important? This creates a formula where the speed of the transition is relative to the amount of angle change required. The duration at full zoom stays constant.
2D Orthographic Zoom
There are certain scenarios where you may want to control the camera’s distance from a player in the third person or a top-down system in an RTS ( Real-Time Strategy) game. You will need two objects for this process a parent object and a child object. While the parent object will be moved around, the child object ( the camera) will be rotated down towards the scene to create a simple zoom effect. Now that you are able to maneuver the camera, you can zoom in closer to the world by using the local forward direction of the camera object. Multiplying its forward direction vector by a zoom-float value will return a new vector set forward by a number of units that are used to set the camera’s zoomed position. With this, you are now able to set this motion to control, for example, a mouse scroll wheel. Gamedevbeginner states that Using the Input Class, Unity’s default method of getting input from devices, I can access the Mouse Scroll Delta, which is a Vector 2 value that returns the vertical and horizontal scroll amounts as values between -1 and 1. In this case, I only want to detect vertical scroll data, which is the Y value of the Vector 2 value. I can then use the delta value to increase or decrease the zoom amount.“ They also explain that adding a sensitivity monitor can help control the reactions in the environment.