unity 相机平滑跟随游戏角色
来源:互联网 发布:怎么用淘宝买东西支付 编辑:程序博客网 时间:2024/05/21 07:34
这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。
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
using
UnityEngine;
using
System.Collections;
public
class
SmoothFollowerObj {
private
Vector3 targetPosition;
private
Vector3 position;
private
Vector3 velocity;
private
float
smoothingTime;
private
float
prediction;
public
SmoothFollowerObj(
float
smoothingTime) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
prediction = 1;
}
public
SmoothFollowerObj(
float
smoothingTime,
float
prediction) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
this
.prediction = prediction;
}
// Update should be called once per frame
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime) {
Vector3 targetVelocity = (targetPositionNew-targetPosition)/deltaTime;
targetPosition = targetPositionNew;
float
d = Mathf.Min(1,deltaTime/smoothingTime);
velocity = velocity*(1-d) + (targetPosition+targetVelocity*prediction-position)*d;
position += velocity*Time.deltaTime;
return
position;
}
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime,
bool
reset) {
if
(reset) {
targetPosition = targetPositionNew;
position = targetPositionNew;
velocity = Vector3.zero;
return
position;
}
return
Update(targetPositionNew, deltaTime);
}
public
Vector3 GetPosition() {
return
position; }
public
Vector3 GetVelocity() {
return
velocity; }
}
public
class
NoGravityCamera : MonoBehaviour {
// 这里的NoGravityCamera修改成你的脚本名
public
GameObject character;
public
Vector3 positionVector;
public
Vector3 lookVector;
private
SmoothFollowerObj posFollow;
private
SmoothFollowerObj lookFollow;
private
Vector3 lastVelocityDir;
private
Vector3 lastPos;
// Use this for initialization
void
Start () {
positionVector=
new
Vector3(0,2,4);
lookVector=
new
Vector3(0,0,1.5f);
posFollow =
new
SmoothFollowerObj(0.5f,0.5f);
lookFollow =
new
SmoothFollowerObj(0.1f,0.0f);
posFollow.Update(transform.position,0,
true
);
lookFollow.Update(character.transform.position,0,
true
);
lastVelocityDir = character.transform.forward;
lastPos = character.transform.position;
}
// Update is called once per frame
void
LateUpdate () {
lastVelocityDir += (character.transform.position-lastPos)*8;
lastPos = character.transform.position;
lastVelocityDir += character.transform.forward*Time.deltaTime;
lastVelocityDir = lastVelocityDir.normalized;
Vector3 horizontal = transform.position-character.transform.position;
Vector3 horizontal2 = horizontal;
Vector3 vertical = character.transform.up;
Vector3.OrthoNormalize(
ref
vertical,
ref
horizontal2);
if
(horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;
transform.position = posFollow.Update(
character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
Time.deltaTime
);
horizontal = lastVelocityDir;
Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);
transform.rotation = Quaternion.FromToRotation(transform.forward, look-transform.position) * transform.rotation;
}
}
这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。
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
using
UnityEngine;
using
System.Collections;
public
class
SmoothFollowerObj {
private
Vector3 targetPosition;
private
Vector3 position;
private
Vector3 velocity;
private
float
smoothingTime;
private
float
prediction;
public
SmoothFollowerObj(
float
smoothingTime) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
prediction = 1;
}
public
SmoothFollowerObj(
float
smoothingTime,
float
prediction) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
this
.prediction = prediction;
}
// Update should be called once per frame
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime) {
Vector3 targetVelocity = (targetPositionNew-targetPosition)/deltaTime;
targetPosition = targetPositionNew;
float
d = Mathf.Min(1,deltaTime/smoothingTime);
velocity = velocity*(1-d) + (targetPosition+targetVelocity*prediction-position)*d;
position += velocity*Time.deltaTime;
return
position;
}
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime,
bool
reset) {
if
(reset) {
targetPosition = targetPositionNew;
position = targetPositionNew;
velocity = Vector3.zero;
return
position;
}
return
Update(targetPositionNew, deltaTime);
}
public
Vector3 GetPosition() {
return
position; }
public
Vector3 GetVelocity() {
return
velocity; }
}
public
class
NoGravityCamera : MonoBehaviour {
// 这里的NoGravityCamera修改成你的脚本名
public
GameObject character;
public
Vector3 positionVector;
public
Vector3 lookVector;
private
SmoothFollowerObj posFollow;
private
SmoothFollowerObj lookFollow;
private
Vector3 lastVelocityDir;
private
Vector3 lastPos;
// Use this for initialization
void
Start () {
positionVector=
new
Vector3(0,2,4);
lookVector=
new
Vector3(0,0,1.5f);
posFollow =
new
SmoothFollowerObj(0.5f,0.5f);
lookFollow =
new
SmoothFollowerObj(0.1f,0.0f);
posFollow.Update(transform.position,0,
true
);
lookFollow.Update(character.transform.position,0,
true
);
lastVelocityDir = character.transform.forward;
lastPos = character.transform.position;
}
// Update is called once per frame
void
LateUpdate () {
lastVelocityDir += (character.transform.position-lastPos)*8;
lastPos = character.transform.position;
lastVelocityDir += character.transform.forward*Time.deltaTime;
lastVelocityDir = lastVelocityDir.normalized;
Vector3 horizontal = transform.position-character.transform.position;
Vector3 horizontal2 = horizontal;
Vector3 vertical = character.transform.up;
Vector3.OrthoNormalize(
ref
vertical,
ref
horizontal2);
if
(horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;
transform.position = posFollow.Update(
character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
Time.deltaTime
);
horizontal = lastVelocityDir;
Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);
transform.rotation = Quaternion.FromToRotation(transform.forward, look-transform.position) * transform.rotation;
}
}
这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。
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
using
UnityEngine;
using
System.Collections;
public
class
SmoothFollowerObj {
private
Vector3 targetPosition;
private
Vector3 position;
private
Vector3 velocity;
private
float
smoothingTime;
private
float
prediction;
public
SmoothFollowerObj(
float
smoothingTime) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
prediction = 1;
}
public
SmoothFollowerObj(
float
smoothingTime,
float
prediction) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
this
.prediction = prediction;
}
// Update should be called once per frame
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime) {
Vector3 targetVelocity = (targetPositionNew-targetPosition)/deltaTime;
targetPosition = targetPositionNew;
float
d = Mathf.Min(1,deltaTime/smoothingTime);
velocity = velocity*(1-d) + (targetPosition+targetVelocity*prediction-position)*d;
position += velocity*Time.deltaTime;
return
position;
}
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime,
bool
reset) {
if
(reset) {
targetPosition = targetPositionNew;
position = targetPositionNew;
velocity = Vector3.zero;
return
position;
}
return
Update(targetPositionNew, deltaTime);
}
public
Vector3 GetPosition() {
return
position; }
public
Vector3 GetVelocity() {
return
velocity; }
}
public
class
NoGravityCamera : MonoBehaviour {
// 这里的NoGravityCamera修改成你的脚本名
public
GameObject character;
public
Vector3 positionVector;
public
Vector3 lookVector;
private
SmoothFollowerObj posFollow;
private
SmoothFollowerObj lookFollow;
private
Vector3 lastVelocityDir;
private
Vector3 lastPos;
// Use this for initialization
void
Start () {
positionVector=
new
Vector3(0,2,4);
lookVector=
new
Vector3(0,0,1.5f);
posFollow =
new
SmoothFollowerObj(0.5f,0.5f);
lookFollow =
new
SmoothFollowerObj(0.1f,0.0f);
posFollow.Update(transform.position,0,
true
);
lookFollow.Update(character.transform.position,0,
true
);
lastVelocityDir = character.transform.forward;
lastPos = character.transform.position;
}
// Update is called once per frame
void
LateUpdate () {
lastVelocityDir += (character.transform.position-lastPos)*8;
lastPos = character.transform.position;
lastVelocityDir += character.transform.forward*Time.deltaTime;
lastVelocityDir = lastVelocityDir.normalized;
Vector3 horizontal = transform.position-character.transform.position;
Vector3 horizontal2 = horizontal;
Vector3 vertical = character.transform.up;
Vector3.OrthoNormalize(
ref
vertical,
ref
horizontal2);
if
(horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;
transform.position = posFollow.Update(
character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
Time.deltaTime
);
horizontal = lastVelocityDir;
Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);
transform.rotation = Quaternion.FromToRotation(transform.forward, look-transform.position) * transform.rotation;
}
}
这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。
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
using
UnityEngine;
using
System.Collections;
public
class
SmoothFollowerObj {
private
Vector3 targetPosition;
private
Vector3 position;
private
Vector3 velocity;
private
float
smoothingTime;
private
float
prediction;
public
SmoothFollowerObj(
float
smoothingTime) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
prediction = 1;
}
public
SmoothFollowerObj(
float
smoothingTime,
float
prediction) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
this
.prediction = prediction;
}
// Update should be called once per frame
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime) {
Vector3 targetVelocity = (targetPositionNew-targetPosition)/deltaTime;
targetPosition = targetPositionNew;
float
d = Mathf.Min(1,deltaTime/smoothingTime);
velocity = velocity*(1-d) + (targetPosition+targetVelocity*prediction-position)*d;
position += velocity*Time.deltaTime;
return
position;
}
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime,
bool
reset) {
if
(reset) {
targetPosition = targetPositionNew;
position = targetPositionNew;
velocity = Vector3.zero;
return
position;
}
return
Update(targetPositionNew, deltaTime);
}
public
Vector3 GetPosition() {
return
position; }
public
Vector3 GetVelocity() {
return
velocity; }
}
public
class
NoGravityCamera : MonoBehaviour {
// 这里的NoGravityCamera修改成你的脚本名
public
GameObject character;
public
Vector3 positionVector;
public
Vector3 lookVector;
private
SmoothFollowerObj posFollow;
private
SmoothFollowerObj lookFollow;
private
Vector3 lastVelocityDir;
private
Vector3 lastPos;
// Use this for initialization
void
Start () {
positionVector=
new
Vector3(0,2,4);
lookVector=
new
Vector3(0,0,1.5f);
posFollow =
new
SmoothFollowerObj(0.5f,0.5f);
lookFollow =
new
SmoothFollowerObj(0.1f,0.0f);
posFollow.Update(transform.position,0,
true
);
lookFollow.Update(character.transform.position,0,
true
);
lastVelocityDir = character.transform.forward;
lastPos = character.transform.position;
}
// Update is called once per frame
void
LateUpdate () {
lastVelocityDir += (character.transform.position-lastPos)*8;
lastPos = character.transform.position;
lastVelocityDir += character.transform.forward*Time.deltaTime;
lastVelocityDir = lastVelocityDir.normalized;
Vector3 horizontal = transform.position-character.transform.position;
Vector3 horizontal2 = horizontal;
Vector3 vertical = character.transform.up;
Vector3.OrthoNormalize(
ref
vertical,
ref
horizontal2);
if
(horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;
transform.position = posFollow.Update(
character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
Time.deltaTime
);
horizontal = lastVelocityDir;
Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);
transform.rotation = Quaternion.FromToRotation(transform.forward, look-transform.position) * transform.rotation;
}
}
这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。
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
using
UnityEngine;
using
System.Collections;
public
class
SmoothFollowerObj {
private
Vector3 targetPosition;
private
Vector3 position;
private
Vector3 velocity;
private
float
smoothingTime;
private
float
prediction;
public
SmoothFollowerObj(
float
smoothingTime) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
prediction = 1;
}
public
SmoothFollowerObj(
float
smoothingTime,
float
prediction) {
targetPosition = Vector3.zero;
position = Vector3.zero;
velocity = Vector3.zero;
this
.smoothingTime = smoothingTime;
this
.prediction = prediction;
}
// Update should be called once per frame
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime) {
Vector3 targetVelocity = (targetPositionNew-targetPosition)/deltaTime;
targetPosition = targetPositionNew;
float
d = Mathf.Min(1,deltaTime/smoothingTime);
velocity = velocity*(1-d) + (targetPosition+targetVelocity*prediction-position)*d;
position += velocity*Time.deltaTime;
return
position;
}
public
Vector3 Update(Vector3 targetPositionNew,
float
deltaTime,
bool
reset) {
if
(reset) {
targetPosition = targetPositionNew;
position = targetPositionNew;
velocity = Vector3.zero;
return
position;
}
return
Update(targetPositionNew, deltaTime);
}
public
Vector3 GetPosition() {
return
position; }
public
Vector3 GetVelocity() {
return
velocity; }
}
public
class
NoGravityCamera : MonoBehaviour {
// 这里的NoGravityCamera修改成你的脚本名
public
GameObject character;
public
Vector3 positionVector;
public
Vector3 lookVector;
private
SmoothFollowerObj posFollow;
private
SmoothFollowerObj lookFollow;
private
Vector3 lastVelocityDir;
private
Vector3 lastPos;
// Use this for initialization
void
Start () {
positionVector=
new
Vector3(0,2,4);
lookVector=
new
Vector3(0,0,1.5f);
posFollow =
new
SmoothFollowerObj(0.5f,0.5f);
lookFollow =
new
SmoothFollowerObj(0.1f,0.0f);
posFollow.Update(transform.position,0,
true
);
lookFollow.Update(character.transform.position,0,
true
);
lastVelocityDir = character.transform.forward;
lastPos = character.transform.position;
}
// Update is called once per frame
void
LateUpdate () {
lastVelocityDir += (character.transform.position-lastPos)*8;
lastPos = character.transform.position;
lastVelocityDir += character.transform.forward*Time.deltaTime;
lastVelocityDir = lastVelocityDir.normalized;
Vector3 horizontal = transform.position-character.transform.position;
Vector3 horizontal2 = horizontal;
Vector3 vertical = character.transform.up;
Vector3.OrthoNormalize(
ref
vertical,
ref
horizontal2);
if
(horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;
transform.position = posFollow.Update(
character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
Time.deltaTime
);
horizontal = lastVelocityDir;
Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);
transform.rotation = Quaternion.FromToRotation(transform.forward, look-transform.position) * transform.rotation;
}
}
这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1
0 0
- unity 相机平滑跟随游戏角色
- Unity平滑相机跟随
- Unity中SmoothDamp 平滑阻尼--相机跟随角色移动的示例
- unity游戏相机跟随效果
- Unity 2D游戏相机跟随
- Unity 摄像机平滑跟随
- Unity3D 摄像机平滑跟随角色
- 小怪平滑跟随角色
- unity 相机跟随
- Unity中相机跟随
- Unity基础,相机跟随
- Unity3d相机跟随角色移动
- Unity中实现相机平滑且流畅的跟随玩家(或其他对象)
- 【Unity闲谈】2D游戏的角色跟随,角色队列的实现
- unity相机跟随人物移动
- unity之--相机跟随移动
- Unity摄像机跟随角色移动
- 转载他人:角色移动和相机跟随
- Vector小记
- MYSQL下载安装配置卸载图文教程
- Windows下用脚本批量重命名文件
- 《Windows核心编程》之“内核对象同步”
- Java四种内部类
- unity 相机平滑跟随游戏角色
- 替换默认mac ctags
- 架构设计:负载均衡层设计方案(2)——Nginx安装
- C++函数参数传递--值传递和地址传递
- IOS-自动打包Shell脚本(支持目录带空格)
- 动态链接库的显示加载和隐式加载的区别
- centos命令行安装MySQL
- mysql 备份相关
- Protocol Buffer技术详解(C++实例)