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

In order to follow this recipe, please import the basicTerrain package, available in the 0423_02_04_05 folder, into your project. The package includes a basic terrain and a camera that can be rotated via a mouse.

How to do it...

To create a telescopic camera, follow these steps:

  1. Import the Unity package and open the 02_04_05 scene.
  2. 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.
  3. Open your script and replace everything with the following code:
    using UnityEngine;
    using System.Collections;
    
    public class TelescopicView : MonoBehaviour{
        public float ZoomLevel = 2.0f;
        public float ZoomInSpeed = 100.0f;
        public float ZoomOutSpeed = 100.0f;
        private float initFOV;
        //private Vignetting vignette;
        //public float vignetteAmount = 10.0f;
    
        void Start(){
            initFOV = Camera.main.fieldOfView;
    		//vignette = this.GetComponent("Vignetting") as Vignetting;
        }
        void Update(){
            if (Input.GetKey(KeyCode.Mouse0)){
                ZoomView();
            }else{
                ZoomOut();
            }
        }
        void ZoomView(){
            if (Mathf.Abs(Camera.main.fieldOfView - (initFOV / ZoomLevel)) < 0.5f){
                Camera.main.fieldOfView = initFOV / ZoomLevel;
    			// vignette. intensity = vignetteAmount;
            }else if (Camera.main.fieldOfView - (Time.deltaTime * ZoomInSpeed) >= (initFOV / ZoomLevel)){
                Camera.main.fieldOfView -= (Time.deltaTime * ZoomInSpeed);
               // vignette. intensity = vignetteAmount * (Camera.main.fieldOfView - initFOV)/((initFOV / ZoomLevel) - initFOV);
            }
        }
        void ZoomOut(){
            if (Mathf.Abs(Camera.main.fieldOfView - initFOV) < 0.5f){
                Camera.main.fieldOfView = initFOV;
               // vignette. intensity = 0;
            }else if (Camera.main.fieldOfView + (Time.deltaTime * ZoomOutSpeed) <= initFOV){
                Camera.main.fieldOfView += (Time.deltaTime * ZoomOutSpeed);
                //vignette.intensity = vignetteAmount * (Camera.main.fieldOfView - initFOV)/((initFOV / ZoomLevel) - initFOV);
            }
        }
    }
  4. Save your script and apply it to the Main Camera game object.
  5. Play your scene. You should be able to zoom in the camera view by clicking the mouse button and zoom out by releasing it.
  6. The next steps are for only those using Unity Pro. If you are using Unity Pro, please stop the scene.
  7. Import Unity's Image Effects package by navigating to Assets | Import Package | Image Effects (Pro Only).
  8. Select the Main Camera option and apply the Vignette image effect (by navigating to Component | Image Effects | Vignette).
  9. Open the TelescopicView script and uncomment every commented line (these are the ones referencing the vignetteAmount variable).
  10. Save the script and play the level. You should see an animated vignetting effect in addition to the zooming:
    How to do it...

How it works...

The zooming effect is actually caused by the increment made by the script on the value of the camera's Field of View property, while the mouse button is pressed.

There's more...

If you are using Unity Pro, you could 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.146.176.88