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); |
1
2
3
4
5
6
|
if (HistoryPos.Count
> 0) { int index
= HistoryPos.Count - 1; this .transform.position
= HistoryPos[index]; HistoryPos.RemoveAt(index); } |
1
2
3
4
5
6
|
HistoryPos.Add(pos); if (ShouldLimit
&& HistoryPos.Count > Limit) { HistoryPos.RemoveAt(0); } |
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 ; } } } |