01
02
03
04
05
06
07
08
09
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
|
using
UnityEngine;
using
System.Collections;
public
class
MoveController : MonoBehaviour {
Transform camera;
Animator ani;
NavMeshAgent nav;
float
h, v;
Vector3 moveVec;
// Use this for initialization
void
Start () {
camera = Camera.main.transform;
ani = GetComponent
nav = GetComponent
}
// Update is called once per frame
void
Update () {
h = Input.GetAxis(
"Horizontal"
);
v = Input.GetAxis(
"Vertical"
);
moveVec =
new
Vector3(h, 0, v);
if
(h != 0 || v != 0)
{
ani.SetBool(
"Run"
,
true
);
// 根据摄像机方向 进行移动
moveVec = Quaternion.Euler(0, camera.eulerAngles.y, 0) * moveVec;
nav.Move(moveVec * Time.deltaTime * 5);
RotatePlayer();
}
else
{
ani.SetBool(
"Run"
,
false
);
}
}
private
void
RotatePlayer()
{
Vector3 vec = Quaternion.Euler(0, 0, 0) * moveVec;
Quaternion qua = Quaternion.LookRotation(vec);
transform.rotation = Quaternion.Lerp(transform.rotation, qua, Time.deltaTime * 100);
}
}
|
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
using
UnityEngine;
public
class
ThirdCamera : MonoBehaviour
{
public
Transform target =
null
;
// 目标玩家
[SerializeField]
[Range(0, 360)]
float
horizontalAngle = 270f;
// 水平角度
[SerializeField]
[Range(0, 20)]
float
initialHeight = 2f;
// 人物在视野内屏幕中的位置设置
[SerializeField]
[Range(10, 90)]
float
initialAngle = 40f;
// 初始俯视角度
[SerializeField]
[Range(10, 90)]
float
maxAngle = 50f;
// 最高俯视角度
[SerializeField]
[Range(10, 90)]
float
minAngle = 35f;
// 最低俯视角度
float
initialDistance;
// 初始化相机与玩家的距离 根据角度计算
[SerializeField]
[Range(1, 100)]
float
maxDistance = 20f;
// 相机距离玩家最大距离
[SerializeField]
[Range(1, 100)]
float
minDistance = 5f;
// 相机距离玩家最小距离
[SerializeField]
[Range(1, 100)]
float
zoomSpeed = 50;
// 缩放速度
[SerializeField]
[Range(1f, 200)]
float
swipeSpeed = 50;
// 左右滑动速度
float
scrollWheel;
// 记录滚轮数值
float
tempAngle;
// 临时存储摄像机的初始角度
Vector3 tempVector =
new
Vector3();
void
Start()
{
InitCamera();
}
void
Update()
{
ZoomCamera();
SwipeScreen();
}
void
LateUpdate()
{
FollowPlayer();
RotateCamera();
}
///
/// 初始化 相机与玩家距离
///
void
InitCamera()
{
tempAngle = initialAngle;
initialDistance = Mathf.Sqrt((initialAngle - minAngle) / Calculate()) + minDistance;
initialDistance = Mathf.Clamp(initialDistance, minDistance, maxDistance);
}
///
/// 相机跟随玩家
///
void
FollowPlayer()
{
float
upRidus = Mathf.Deg2Rad * initialAngle;
float
flatRidus = Mathf.Deg2Rad * horizontalAngle;
float
x = initialDistance * Mathf.Cos(upRidus) * Mathf.Cos(flatRidus);
float
z = initialDistance * Mathf.Cos(upRidus) * Mathf.Sin(flatRidus);
float
y = initialDistance * Mathf.Sin(upRidus);
transform.position = Vector3.zero;
tempVector.Set(x, y, z);
tempVector = tempVector + target.position;
transform.position = tempVector;
tempVector.Set(target.position.x, target.position.y + initialHeight, target.position.z);
transform.LookAt(tempVector);
}
///
/// 缩放相机与玩家距离
///
void
ZoomCamera()
{
scrollWheel = GetZoomValue();
if
(scrollWheel != 0)
{
tempAngle = initialAngle - scrollWheel * 2 * (maxAngle - minAngle);
tempAngle = Mathf.Clamp(tempAngle, minAngle, maxAngle);
}
if
(tempAngle != initialAngle)
{
initialAngle = Mathf.Lerp(initialAngle, tempAngle, Time.deltaTime * 10);
initialDistance = Mathf.Sqrt((initialAngle - minAngle) / Calculate()) + minDistance;
initialDistance = Mathf.Clamp(initialDistance, minDistance, maxDistance);
}
}
float
Calculate()
{
float
dis = maxDistance - minDistance;
float
ang = maxAngle - minAngle;
float
line = ang / (dis * dis);
return
line;
}
bool
isMousePress =
false
;
Vector2 oldMousePos;
Vector2 newMousePos;
Vector2 mousePosOffset;
///
/// 滑动屏幕 旋转相机和缩放视野
///
public
void
SwipeScreen()
{
if
(Input.GetMouseButtonDown(0))
{
oldMousePos = Vector2.zero;
isMousePress =
true
;
}
else
if
(Input.GetMouseButtonUp(0))
{
mousePosOffset = Vector2.zero;
isMousePress =
false
;
}
if
(!isMousePress)
return
;
newMousePos = Input.mousePosition;
if
(oldMousePos != Vector2.zero)
{
mousePosOffset = newMousePos - oldMousePos;
}
oldMousePos = newMousePos;
}
///
/// 获取缩放视野数值 1.鼠标滚轮 2.屏幕上下滑动
///
///
float
GetZoomValue()
{
float
zoomValue = 0;
// 使用鼠标滚轮
if
(Input.GetAxis(
"Mouse ScrollWheel"
) != 0)
{
zoomValue = Input.GetAxis(
"Mouse ScrollWheel"
);
}
else
if
(mousePosOffset != Vector2.zero)
{
zoomValue = mousePosOffset.y * Time.deltaTime * zoomSpeed * 0.01f;
}
return
zoomValue;
}
float
xVelocity = 0;
///
/// 旋转相机
///
void
RotateCamera()
{
horizontalAngle = Mathf.SmoothDamp(horizontalAngle, horizontalAngle + mousePosOffset.x * Time.deltaTime * swipeSpeed,
ref
xVelocity, 0.1f);
}
}
|