Zooming a telescopic camera
In this recipe, we will create a telescopic camera that zooms in whenever the left mouse button is pressed.
Getting ready...
This recipe adds to the scene created in the first recipe of this chapter, so make a copy of that project folder and do your work for this recipe with that copy.
How to do it...
To create a telescopic camera, follow these steps:
- Create a new C# script-class named
TelescopicView
, and add an instance-object as a component to the Main Camera (child ofMsLazer
):
using UnityEngine; 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; void Start() { initFov = Camera.main.fieldOfView; minFov = initFov / zoom; } void Update() { if (Input.GetKey...