[Unity 3D]用鼠标滚轮实现镜头放大和缩放,并添加距离限制
1 public class CameraMove : MonoBehaviour {
2 private Camera c;
3 private float mouseCenter;
4 private int maxView = 120;
5 private int minView = 10;
6 private float slideSpeed = 20;
7
8
9
10 private void Start() {
11 //获取摄像头组件
12 c = this.GetComponent<Camera>();
13
14 }
15
16
17 private void Update() {
18
19 //获取虚拟按键(鼠标中轴滚轮)
20 float mouseCenter = Input.GetAxis("Mouse ScrollWheel");
//鼠标滑动中键滚轮,实现摄像机的镜头放大和缩放
//mouseCenter < 0 = 负数 往后滑动,缩放镜头
21 if (mouseCenter <0 ) {
//滑动限制
24 if (c.fieldOfView <= maxView) {
25 c.fieldOfView += 10 * slideSpeed*Time.deltaTime;
26 if (c.fieldOfView >= maxView) {
27 c.fieldOfView = minView;
28 }
29 }
30 //mouseCenter >0 = 正数 往前滑动,放大镜头
31 } else if (mouseCenter >0 ) {
//滑动限制
32 if (c.fieldOfView >= minView) {
33 c.fieldOfView -= 10 * slideSpeed*
34 Time.deltaTime;
35 if (c.fieldOfView <= minView) {
36 c.fieldOfView = maxView;
37 }
38 }
39 }
40 }
41 }
注意看Field of View值的变化
时间若流水,恍惚间逝去