Hello, everyone. Welcome to the Unity3D Game Development Series articles that I bring to you. My blog address is http://blog.csdn.net/qinyuanpei.
Today, we will share with you the following:
Next we will write a script code:
using UnityEngine;using System.Collections;public class CameraZoom : MonoBehaviour {void Start () {}void Update () {//Zoom outif (Input.GetAxis("Mouse ScrollWheel") <0) { if(Camera.main.fieldOfView<=100) Camera.main.fieldOfView +=2; if(Camera.main.orthographicSize<=20) Camera.main.orthographicSize +=0.5F; }//Zoom in if (Input.GetAxis("Mouse ScrollWheel") > 0) { if(Camera.main.fieldOfView>2)Camera.main.fieldOfView-=2; if(Camera.main.orthographicSize>=1) Camera.main.orthographicSize-=0.5F; }}}
From the code above, we can see that we can zoom in and out the scene by changing the camera's field of view, and we use the GetAxis () method for mouse wheel monitoring, the following shows the demo Animation: