Home>

Sounds pretty cool,It's actually super simple.It is the fieldofview that controls the camera:

using unityengine;
using system.collections;
public class telesopicview:monobehaviour {
 public float zoomlevel=2.0f;
 public float zoominspeed=100.0f;
 public float zoomoutspeed=100.0f;
 private float initfov;
 void start ()
 {
 //Get the field of view of the current camera
 initfov=camera.main.fieldofview;
 }
 void update ()
 {
 if (input.getmousebutton (0))
 {
 zoominview ();
 }
 else
 {
 zoomoutview ();
 }
 }
 //Enlarge the field of view of the camera
 void zoominview ()
 {
 if (mathf.abs (camera.main.fieldofview-(initfov/zoomlevel))<3.0f)
 {
 camera.main.fieldofview=initfov/zoomlevel;
 }
 else if (camera.main.fieldofview-(time.deltatime * zoominspeed)>= (initfov/zoomlevel))
 {
 camera.main.fieldofview-= (time.deltatime * zoominspeed);
 }
 }
 //Reduce the field of view of the camera
 void zoomoutview ()
 {
 if (mathf.abs (camera.main.fieldofview-initfov)<3.0f)
 {
 camera.main.fieldofview=initfov;
 }
 else if (camera.main.fieldofview + (time.deltatime * zoomoutspeed)<= initfov)
 {
 camera.main.fieldofview +=(time.deltatime * zoomoutspeed);
 }
 }
}

Add the script telescopicview to the main camera. zoom level can adjust how far the view can be zoomed;zoom in speed adjust the zoom speed of the view;zoom out speed Adjust the speed of zoom out.

run game,When the left mouse button is pressed,The view will zoom in,When released, the view will slowly return to its original appearance:

Of course, in order to enhance the visual effect,You can also use the resource pack that comes with Unity.You can also design the telescope view picture yourself:

Tuning parameters,Run the game again.When the left mouse button is pressed,The view effect of the telescope is more realistic (added the effect of halo):

c
  • Previous Detailed implementation of Java array copy clone method
  • Next Analysis of RSA encryption and decryption examples in Python