本文实例为大家分享了Unity3D实现相机跟随控制的具体代码,供大家参考,具体内容如下
跟随算法
要实现3D摄像机的控制第一步就是先实现摄像机跟随物体移动。
要想让相机跟随物体移动,就要明白在一定角度下相机与物体的位置关系。
首先设置相机与物体之间的距离distance,相机与xz平面的角度为roll
所以根据三角关系可以求得映射在xz平面的距离d为distancecos(rool),相机高度为distancesin(roll)。
如下图
现在就可以确定相机的高度了即y轴的坐标相机的y轴坐标应该为 Camera.Main.y=物体.y+height
在xz平面中,设相机与物体的距离为d(就是上面说的那个d,distance映射在xz平面的长度),相机的旋转角度为rot。根据下图可以看到,相机与物体的连线与x轴的角度为rot-180.根据三角函数,既可以得出x轴的位移为d*sin(rot) ,z轴的位移为d*cos(rot) 。
所以说开始的时候指定distance和rot和roll就可以实现跟随了。实现跟随的代码如下
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
|
public class CameraFollow : MonoBehaviour { //距离 public float distance = 15; //横向角度 public float rot = 0; //纵向角度 30d度 public float roll = 30f * Mathf.PI * 2 / 360; //目标物体 public GameObject target; private void Start() { target = GameObject.Find( "Black Track" ); } private void LateUpdate() { if (target == null ) return ; if (Camera.main == null ) return ; //目标的坐标 Vector3 targetPos = target.transform.position; //用三角函数计算相机的位置 Vector3 cameraPos; float d = distance * Mathf.Cos(roll); float height = distance * Mathf.Sin(roll); cameraPos.x = targetPos.x + d * Mathf.Cos(rot); cameraPos.z = targetPos.z + d * Mathf.Sin(rot); cameraPos.y = targetPos.y + height; Camera.main.transform.position = cameraPos; Camera.main.transform.LookAt(target.transform); } } |
在跟随的时候我们可以在要跟随的物体下放置一个子物体命名为cameraPoint使相机对准这个子物体从而方便的更改摄像机的视角。
所以在物体下添加一个cameraPoint的子物体
并且添加代码
1
2
3
4
5
6
7
8
|
//设置目标 public void SetTarget(GameObject target) { if (target.transform.Find( "cameraPoint" ) != null ) this .target = target.transform.Find( "cameraPoint" ).gameObject; else this .target = target; } |
如果准的物体有名为cameraPoint的子物体,那么相机对准cameraPoint子物体。
横向与纵向旋转摄像机
当鼠标向左移动时,相机随之左转,当鼠标向右移动时,相机随之右转。
Unity的输入轴Mouse X 和 Mouse Y 代表着鼠标的移动增量,也就是说当鼠标向左移动时,Input.GetAxis(“Mouse X”)的值会增大,向右则减少。只要让旋转角度rot与Mouse X成正比关系,便能通过鼠标控制摄像机的角度。
代码如下
1
2
3
4
5
6
7
8
|
//横向旋转速度 public float rotSpeed=0.1f; //横向旋转 public void Rotate() { float w = Input.GetAxis( "Mouse X" ) * rotSpeed; rot -= w; } |
同理对于纵向旋转我们需要设定一个范围 所以代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//纵向旋转角度 public float maxRoll = 70f * Mathf.PI * 2 / 360; public float minRoll = 0f * Mathf.PI * 2 / 360; //纵向旋转速度 private float rollSpeed = 0.1f; //纵向旋转 public void Roll() { float w = Input.GetAxis( "Mouse Y" ) * rollSpeed; roll -= w; if (roll > maxRoll) roll = maxRoll; if (roll < minRoll) roll = minRoll; } |
滚轮调节距离
通过鼠标滚轮调整相机与物体之间的距离
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//距离范围 public float maxDistance = 22f; public float minDistance = 5f; //距离变化速度 public float zoomSpeed = 0.2f; //调整距离 public void Zoom() { if (Input.GetAxis( "Mouse ScrollWheel" ) >0) { if (distance > minDistance) distance -= zoomSpeed; } else if (Input.GetAxis( "Mouse ScrollWheel" ) < 0) { if (distance < maxDistance) distance += zoomSpeed; } } |
全部代码
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
public class CameraFollow : MonoBehaviour { //距离 public float distance = 15; //横向角度 public float rot = 0; //纵向角度 30d度 public float roll = 30f * Mathf.PI * 2 / 360; //目标物体 public GameObject target; //横向旋转速度 public float rotSpeed=0.1f; //纵向旋转角度 public float maxRoll = 70f * Mathf.PI * 2 / 360; public float minRoll = 0f * Mathf.PI * 2 / 360; //纵向旋转速度 private float rollSpeed = 0.1f; //距离范围 public float maxDistance = 22f; public float minDistance = 5f; //距离变化速度 public float zoomSpeed = 0.2f; private void Start() { target = GameObject.Find( "Black Track" ); SetTarget(target); } private void LateUpdate() { if (target == null ) return ; if (Camera.main == null ) return ; //横向旋转 Rotate(); //纵向旋转 Roll(); //缩放 Zoom(); //目标的坐标 Vector3 targetPos = target.transform.position; //用三角函数计算相机的位置 Vector3 cameraPos; float d = distance * Mathf.Cos(roll); float height = distance * Mathf.Sin(roll); cameraPos.x = targetPos.x + d * Mathf.Cos(rot); cameraPos.z = targetPos.z + d * Mathf.Sin(rot); cameraPos.y = targetPos.y + height; Camera.main.transform.position = cameraPos; Camera.main.transform.LookAt(target.transform); } //设置目标 public void SetTarget(GameObject target) { if (target.transform.Find( "cameraPoint" ) != null ) this .target = target.transform.Find( "cameraPoint" ).gameObject; else this .target = target; } //横向旋转 public void Rotate() { float w = Input.GetAxis( "Mouse X" ) * rotSpeed; rot -= w; } //纵向旋转 public void Roll() { float w = Input.GetAxis( "Mouse Y" ) * rollSpeed; roll -= w; if (roll > maxRoll) roll = maxRoll; if (roll < minRoll) roll = minRoll; } //调整距离 public void Zoom() { if (Input.GetAxis( "Mouse ScrollWheel" ) >0) { if (distance > minDistance) distance -= zoomSpeed; } else if (Input.GetAxis( "Mouse ScrollWheel" ) < 0) { if (distance < maxDistance) distance += zoomSpeed; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44512151/article/details/107102239