Zooming a telescopic camera

In this recipe, we will create a telescopic camera that zooms in whenever the left mouse button is pressed. This can be very useful, for instance, if we have a sniper in our game.

Getting ready...

For this recipe, we have prepared the BasicScene Unity package, containing a scene named BasicScene. The package is in the 1362_05_codes folder.

How to do it...

To create a telescopic camera, follow these steps:

  1. Import the BasicScene package into a new Project.
  2. From the Project view, open the BasicScene level. This is a basic scene featuring an animated character and some extra geometry.
  3. Import Unity's Effects package by navigating to Assets | Import Package | Effects.
  4. Select the Main Camera GameObject within the Multipurpose Camera Rig | Pivot GameObject and apply the Vignette image effect (by navigating to Component | Image Effects | Camera | Vignette and Chromatic Aberration).
  5. We need to create a script. In the Project view, click on the Create drop-down menu and choose C# Script. Rename it TelescopicView and open it in your editor.
  6. Open your script and replace everything with the following code:
    using UnityEngine;
    using System.Collections;
    using UnityStandardAssets.ImageEffects;
    
    public class TelescopicView : MonoBehaviour{
      public float zoom = 2.0f;
      public float speedIn = 100.0f;
      public float speedOut = 100.0f;
      private float initFov;
      private float currFov;
      private float minFov;
      private float addFov;
      private VignetteAndChromaticAberration v;
      public float vMax = 10.0f;
    
      void Start(){
        initFov = Camera.main.fieldOfView;
        minFov = initFov / zoom;
        v = this.GetComponent<VignetteAndChromaticAberration>() as VignetteAndChromaticAberration;
      }
      void Update(){
        if (Input.GetKey(KeyCode.Mouse0))
          ZoomView();
        else
          ZoomOut();
        float currDistance = currFov - initFov;
        float totalDistance = minFov - initFov;
        float vMultiplier = currDistance / totalDistance;
        float vAmount = vMax * vMultiplier;
        vAmount = Mathf.Clamp (vAmount,0,vMax);
        v.intensity = vAmount;
      }
    
      void ZoomView(){
        currFov = Camera.main.fieldOfView;
        addFov = speedIn * Time.deltaTime;
    
        if (Mathf.Abs(currFov - minFov) < 0.5f)
          currFov = minFov;
        else if (currFov - addFov >= minFov)
          currFov -= addFov;
    
        Camera.main.fieldOfView = currFov;
      }
    
      void ZoomOut(){
        currFov = Camera.main.fieldOfView;
        addFov = speedOut * Time.deltaTime;
    
        if (Mathf.Abs(currFov - initFov) < 0.5f)
          currFov = initFov;
        else if (currFov + addFov <= initFov)
          currFov += addFov;
    
        Camera.main.fieldOfView = currFov;
      }
    }
  7. Save your script and apply it to the Main Camera GameObject within the Multipurpose Camera Rig | Pivot GameObject.
  8. Play the level. You should see an animated vignette effect in addition to the zooming:
    How to do it...

How it works...

The zooming effect is actually caused by changes to the value of the camera's Field Of View (FOV) property; small values result in closer views of a smaller area, while high values enlarge the FOV.

The TelescopicView script changes the camera's field of view by subtracting from it whenever the left mouse button is pressed. It also adds to the FOV value when the mouse button is not being held, until it reaches its original value.

The zoom limit of the FOV can be deduced from the code minFov = initFov / zoom;. This means that the minimum value of the FOV is equal to its original value divided by the zoom amount. For instance, if our camera features, originally, a FOV of 60, and we set the Telescopic View Zoom amount to 2.0, the minimum FOV allowed will be 60/2 = 30. The difference is shown in the following two screenshots:

How it works...

There's more...

You can also add a variable to control the Blur Vignette level of the Vignette image effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.106.233