1 public float moveSpeed = 1;//物体旋转速度
2 public GameObject target;
3
4 private Vector2 oldPosition;
5 private Vector2 oldPosition1;
6 private Vector2 oldPosition2;
7
8
9 private float distance = 0;
10 private bool flag = false;
11 //摄像头的位置
12 private float x = 0f;
13 private float y = 0f;
14 //左右滑动移动速度
15 public float xSpeed = 250f;
16 public float ySpeed = 120f;
17 //缩放限制系数
18 public float yMinLimit = -360;
19 public float yMaxLimit = 360;
20 //是否旋转
21 private bool isRotate = true;
22 //计数器
23 private float count = 0;
24
25 public static CamCtrl _instance;
26 //初始化游戏信息设置
27 void Start()
28 {
29 _instance = this;
30 Vector3 angles = transform.eulerAngles;
31 x = angles.y;
32 y = angles.x;
33 if (GetComponent<Rigidbody>())
34 GetComponent<Rigidbody>().freezeRotation = true;
35 }
36
37
38 void Update()
39 {
40
41 if (isRotate)
42 {
43
44 target.transform.Rotate(Vector3.down, Time.deltaTime * moveSpeed, Space.World);
45
46 }
47 if (!isRotate)
48 {
49 count += Time.deltaTime;
50 if (count > 5)
51 {
52 count = 0;
53 isRotate = true;
54 }
55 }
56
57 //触摸类型为移动触摸
58 if (Input.GetMouseButton(0))
59 {
60 //根据触摸点计算X与Y位置
61 x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
62 y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
63 isRotate = false;
64 }
65 //判断鼠标滑轮是否输入
66 //float temp = Input.GetAxis("Mouse ScrollWheel");
67 //if (temp != 0)
68 //{
69 // if (temp > 0)
70 // {
71 // // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
72 // if (distance > -15)
73 // {
74 // distance -= 0.5f;
75 // }
76 // }
77 // if (temp < 0)
78 // {
79 // // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
80 // if (distance < 20)
81 // {
82 // distance += 0.5f;
83 // }
84 // }
85 //}
86
87 }
88
89 //计算距离,判断放大还是缩小。放大返回true,缩小返回false
90 bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
91 {
92 //old distance
93 float oldDistance = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
94 //new distance
95 float newDistance = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
96
97 if (oldDistance < newDistance)
98 {
99 //zoom+
100 return true;
101 }
102 else
103 {
104 //zoom-
105 return false;
106 }
107 }
108
109 //每帧执行,在Update后
110 void LateUpdate()
111 {
112 if (target)
113 {
114 //重置摄像机的位置
115 y = ClampAngle(y, yMinLimit, yMaxLimit);
116 var rotation = Quaternion.Euler(y, x, 0);
117 var position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.transform.position;
118
119 transform.rotation = rotation;
120 transform.position = position;
121 }
122 }
123 float ClampAngle(float angle, float min, float max)
124 {
125 if (angle < -360)
126 angle += 360;
127 if (angle > 360)
128 angle -= 360;
129 return Mathf.Clamp(angle, min, max);
130
131 }