【Unity】通过实时记录模拟时间倒退效果回放
来源:互联网 发布:解决json包含html标签 编辑:程序博客网 时间:2024/06/05 18:23
有一些游戏提供了回退的功能,那么在游戏开发中如何实现时间倒退这个效果呢,相信会有很多人想知道,为此下面就给大家介绍下实现时间倒退效果的方法,一起来看看吧。
一个简单的思路就是用Stack来记录物体的Position和Rotation,当需要时间回退的时候就Pop出来,赋值到物体上。不过为了可以进行拓展,比如只能回退到某段时间内的,而不是一下子回退到最开始的地方,我们需要剔除太久之前的信息。如下图:
因此我选择使用List而不是Stack。
代码(完整代码在最下面)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Pos
Vector3 pos =
this
.transform.position;
float
horizontal = Input.GetAxis(
"Horizontal"
);
float
vertical = Input.GetAxis(
"Vertical"
);
if
(Mathf.Abs(horizontal) > 0.0001f)
//左右移动
{
pos.x += Time.deltaTime * horizontal * Speed;
}
if
(Mathf.Abs(vertical) > 0.0001f)
//上下移动
{
pos.y += Time.deltaTime * vertical * Speed;
}
this
.transform.position = pos;
HistoryPos.Add(pos);
这里HistoryPos就是我们用来存储历史位置的List,我们每帧都存储物体的位置。
当我们需要时间回退时,可以每帧调用下面的代码:
1
2
3
4
5
6
if
(HistoryPos.Count > 0)
{
int
index = HistoryPos.Count - 1;
this
.transform.position = HistoryPos[index];
HistoryPos.RemoveAt(index);
}
这就是每次取出最后的位置(即最新的),赋值到物体上
当我们需要限制时间回退的时间跨度,可以在HistoryPos.Add后加上下面这些代码:
1
2
3
4
5
6
HistoryPos.Add(pos);
if
(ShouldLimit && HistoryPos.Count > Limit)
{
HistoryPos.RemoveAt(0);
}
因为旋转是雷同的,因此就不贴代码出来了。
改进
1.这里我们是每帧都记录信息,这样List的大小很容易暴走,因此我们可以每隔一段时间来记录,然后要时间回退的时候就进行插值。
2.通常我们的物体都带有动画,这时倒播动画就行。如果在时间回退过程中存在多个动画,我们就需要自己设计数据结构来保存某个时刻对应的动画和动画状态。
完整代码
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
using
UnityEngine;
using
System.Collections;
using
System.Collections.Generic;
///
/// 就是利用Stack的原理来获取历史位置
/// 如果同时有动画,把动画倒放就是
///
public
class
TBPlayer : MonoBehaviour {
public
int
Speed = 3;
public
int
RotateSpeed = 100;
public
bool
ShouldLimit =
false
;
public
int
Limit = 100;
//可以存放的坐标上限
private
List HistoryPos;
private
List HistoryRot;
private
bool
_IsTimeBack =
false
;
void
Start () {
HistoryPos =
new
List();
HistoryRot =
new
List();
}
void
Update () {
if
(_IsTimeBack)
TimeBack();
else
ControlPos();
}
void
ControlPos()
{
//Pos
Vector3 pos =
this
.transform.position;
float
horizontal = Input.GetAxis(
"Horizontal"
);
float
vertical = Input.GetAxis(
"Vertical"
);
if
(Mathf.Abs(horizontal) > 0.0001f)
//左右移动
{
pos.x += Time.deltaTime * horizontal * Speed;
}
if
(Mathf.Abs(vertical) > 0.0001f)
//上下移动
{
pos.y += Time.deltaTime * vertical * Speed;
}
this
.transform.position = pos;
HistoryPos.Add(pos);
//Rotation
Quaternion rot =
this
.transform.rotation;
Vector3 rotv = rot.eulerAngles;
float
rotate = Input.GetAxis(
"Fire1"
);
if
(Mathf.Abs(rotate) > 0.0001f)
{
rotv.z += Time.deltaTime * rotate * RotateSpeed;
}
rot = Quaternion.Euler(rotv);
this
.transform.rotation = rot;
HistoryRot.Add(rot);
if
(ShouldLimit && HistoryPos.Count > Limit)
{
HistoryPos.RemoveAt(0);
HistoryRot.RemoveAt(0);
}
}
void
TimeBack()
{
if
(HistoryPos.Count > 0)
{
int
index = HistoryPos.Count - 1;
this
.transform.position = HistoryPos[index];
HistoryPos.RemoveAt(index);
}
if
(HistoryRot.Count > 0)
{
int
index = HistoryRot.Count - 1;
this
.transform.rotation = HistoryRot[index];
HistoryRot.RemoveAt(index);
}
}
void
OnGUI()
{
if
(GUILayout.Button(
"时间倒流"
))
{
_IsTimeBack =
true
;
}
if
(GUILayout.Button(
"Reset"
))
{
HistoryRot.Clear();
HistoryPos.Clear();
_IsTimeBack =
false
;
}
}
}
效果
1 0
- 【Unity】通过实时记录模拟时间倒退效果回放
- Unity中的时间控制 - 时间倒退
- Unity UGUI模拟打字效果
- 通过百度API回放经纬度记录的信息(DEMO)
- Android 实时录音和回放,边录音边播放 (KTV回音效果)
- Android 实时录音和回放,边录音边播放 (KTV回音效果)
- Android 实时录音和回放,边录音边播放 (KTV回音效果)
- 使用OpenAL打开麦克风录音并实时回放(类似K歌效果)
- JS实现实时时间显示效果
- Unity模拟弹幕效果(一)
- 通过线程实时获取当前时间
- unity 通过摄像机模拟实现小地图
- android回放记录
- ROS 记录与回放
- Unity 中实现子弹时间效果
- 超宽带射频信号高速记录回放系统——模拟带宽3GHZ,记录回放速度高达6000MB/S!
- unity coc 战斗回放总结
- RTSP回放时如何通过RTP的timestamp计算npt时间
- 【项目管理】如何定义开放性问题和如何在项目中跟踪开放性问题
- JavaEE项目如何引入jar
- 专利文档一般有哪些内容
- SpringMVC深度探险(四) —— SpringMVC核心配置文件详解
- MFCC语音识别特征
- 【Unity】通过实时记录模拟时间倒退效果回放
- JDBC连接数据库步骤
- 简单的angular实例
- cocosjs 使用XMLhttpRequest 跨域问题
- nvm 管理不同版本的 node 与 npm
- bfs
- linux 下用freetds 连接sqlserver
- 微信公众号文章采集 爬取微信文章 采集公众号的阅读数和点赞数?
- 认识beta和dirichlet分布