Unity中摄像机的重要性毋庸置疑,关于摄像机的操作也有很多,比如第一人称跟随,第二人称跟随,摄像机的拉近拉远等等,下面就暂时实现摄像机的拉近拉远和旋转:
创建新的场景,场景中添加一个cube,然后给cube添加新的脚本,脚本内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using
UnityEngine;
using
System.Collections;
public
class
TestCube : MonoBehaviour {
private
GameObject rotateObj;
private
GameObject camera;
public
float
speed = 3f;
private
bool
rotate =
false
;
public
float
maxView = 90;
public
float
minView = 10;
void
Start()
{
rotateObj = GameObject.Find(
"Rotate"
);
camera = GameObject.Find(
"Main Camera"
);
}
void
Update () {
//滑动鼠标滑轮控制视角的大小
float
offsetView = Input.GetAxis(
"Mouse ScrollWheel"
) * speed;
float
tmpView = offsetView + Camera.main.fieldOfView;
tmpView = Mathf.Clamp(tmpView, minView, maxView);
Camera.main.fieldOfView = tmpView;
//绕主角旋转摄像机
if
(rotate)
{
camera.transform.RotateAround(transform.position, Vector3.up, speed * Input.GetAxis(
"Mouse X"
));
}
if
(Input.GetMouseButtonDown(0))
{
rotate =
true
;
}
if
(Input.GetMouseButtonUp(0))
{
rotate =
false
;
}
}
}
当然啦其实围绕旋转的功能本身并不难:
|