Skip to content

Commit 3d90cd6

Browse files
committed
Fix, QOL, documentation
+Fixed velocity rewind not tracking angular velocity +Now tracking the whole transforms (scale is defaultly included) +Cleaned RewindManager script +Updated documentation
1 parent dd357cf commit 3d90cd6

File tree

7 files changed

+101
-85
lines changed

7 files changed

+101
-85
lines changed
-213 KB
Binary file not shown.

Assets/TimeRewinder/ExampleScene/TimeRewindExample 1.unity

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ MonoBehaviour:
265265
m_Script: {fileID: 11500000, guid: b1853014c10a6eb46a8363fdc11758e7, type: 3}
266266
m_Name:
267267
m_EditorClassIdentifier:
268-
trackPositionRotation: 0
268+
trackTransform: 0
269269
trackVelocity: 0
270270
trackAnimator: 0
271271
trackAudio: 1
@@ -634,7 +634,7 @@ Transform:
634634
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
635635
--- !u!95 &184878769
636636
Animator:
637-
serializedVersion: 3
637+
serializedVersion: 5
638638
m_ObjectHideFlags: 0
639639
m_CorrespondingSourceObject: {fileID: 0}
640640
m_PrefabInstance: {fileID: 0}
@@ -647,10 +647,11 @@ Animator:
647647
m_UpdateMode: 1
648648
m_ApplyRootMotion: 0
649649
m_LinearVelocityBlending: 0
650+
m_StabilizeFeet: 0
650651
m_WarningMessage:
651652
m_HasTransformHierarchy: 1
652653
m_AllowConstantClipSamplingOptimization: 1
653-
m_KeepAnimatorControllerStateOnDisable: 0
654+
m_KeepAnimatorStateOnDisable: 0
654655
--- !u!114 &184878770
655656
MonoBehaviour:
656657
m_ObjectHideFlags: 0
@@ -663,7 +664,7 @@ MonoBehaviour:
663664
m_Script: {fileID: 11500000, guid: b1853014c10a6eb46a8363fdc11758e7, type: 3}
664665
m_Name:
665666
m_EditorClassIdentifier:
666-
trackPositionRotation: 0
667+
trackTransform: 0
667668
trackVelocity: 0
668669
trackAnimator: 1
669670
trackAudio: 1
@@ -1185,7 +1186,7 @@ RectTransform:
11851186
m_PrefabAsset: {fileID: 0}
11861187
m_GameObject: {fileID: 574113248}
11871188
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
1188-
m_LocalPosition: {x: 0, y: 0, z: 0.73}
1189+
m_LocalPosition: {x: 0, y: 0, z: 0.5489}
11891190
m_LocalScale: {x: 0.01, y: 0.01, z: 0.01}
11901191
m_Children: []
11911192
m_Father: {fileID: 94201997}
@@ -1194,7 +1195,7 @@ RectTransform:
11941195
m_AnchorMin: {x: 0, y: 0}
11951196
m_AnchorMax: {x: 1, y: 1}
11961197
m_AnchoredPosition: {x: -0.030000687, y: 1.5199999}
1197-
m_SizeDelta: {x: 370.1934, y: 249.6435}
1198+
m_SizeDelta: {x: 370.1934, y: 285.8613}
11981199
m_Pivot: {x: 0.5, y: 0.5}
11991200
--- !u!114 &574113250
12001201
MonoBehaviour:
@@ -1229,9 +1230,7 @@ MonoBehaviour:
12291230
m_HorizontalOverflow: 0
12301231
m_VerticalOverflow: 0
12311232
m_LineSpacing: 1
1232-
m_Text: '-POSITION
1233-
1234-
-ROTATION
1233+
m_Text: '-TRANSFORM
12351234
12361235
-VELOCITY
12371236
@@ -2164,7 +2163,7 @@ MonoBehaviour:
21642163
m_Script: {fileID: 11500000, guid: b1853014c10a6eb46a8363fdc11758e7, type: 3}
21652164
m_Name:
21662165
m_EditorClassIdentifier:
2167-
trackPositionRotation: 1
2166+
trackTransform: 1
21682167
trackVelocity: 1
21692168
trackAnimator: 0
21702169
trackAudio: 0
@@ -3649,7 +3648,7 @@ GameObject:
36493648
m_Component:
36503649
- component: {fileID: 1871628336}
36513650
m_Layer: 0
3652-
m_Name: PositionRotationVelocityRewind----ENABLE THIS OBJECT TO TEST EXAMPLE
3651+
m_Name: TransformVelocityRewind----ENABLE THIS OBJECT TO TEST EXAMPLE
36533652
m_TagString: Untagged
36543653
m_Icon: {fileID: 0}
36553654
m_NavMeshLayer: 0

Assets/TimeRewinder/ExampleScene/TimeRewindExample 2.unity

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,7 +3013,7 @@ GameObject:
30133013
m_PrefabAsset: {fileID: 0}
30143014
--- !u!95 &3843793275252677194
30153015
Animator:
3016-
serializedVersion: 3
3016+
serializedVersion: 5
30173017
m_ObjectHideFlags: 0
30183018
m_CorrespondingSourceObject: {fileID: 0}
30193019
m_PrefabInstance: {fileID: 0}
@@ -3026,10 +3026,11 @@ Animator:
30263026
m_UpdateMode: 1
30273027
m_ApplyRootMotion: 0
30283028
m_LinearVelocityBlending: 0
3029+
m_StabilizeFeet: 0
30293030
m_WarningMessage:
30303031
m_HasTransformHierarchy: 1
30313032
m_AllowConstantClipSamplingOptimization: 1
3032-
m_KeepAnimatorControllerStateOnDisable: 0
3033+
m_KeepAnimatorStateOnDisable: 0
30333034
--- !u!114 &3843793275252677195
30343035
MonoBehaviour:
30353036
m_ObjectHideFlags: 0
@@ -3057,7 +3058,7 @@ MonoBehaviour:
30573058
m_Script: {fileID: 11500000, guid: b1853014c10a6eb46a8363fdc11758e7, type: 3}
30583059
m_Name:
30593060
m_EditorClassIdentifier:
3060-
trackPositionRotation: 1
3061+
trackTransform: 1
30613062
trackVelocity: 0
30623063
trackAnimator: 1
30633064
trackAudio: 0

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/ImplementedObjects/GenericRewind.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public class GenericRewind : RewindAbstract
44
{
5-
[SerializeField] bool trackPositionRotation;
5+
[Tooltip("Tracking Position,Rotation and Scale")]
6+
[SerializeField] bool trackTransform;
67
[SerializeField] bool trackVelocity;
78
[SerializeField] bool trackAnimator;
89
[SerializeField] bool trackAudio;
@@ -14,8 +15,8 @@ public class GenericRewind : RewindAbstract
1415
protected override void Rewind(float seconds)
1516
{
1617

17-
if (trackPositionRotation)
18-
RestorePositionAndRotation(seconds);
18+
if (trackTransform)
19+
RestoreTransform(seconds);
1920
if (trackVelocity)
2021
RestoreVelocity(seconds);
2122
if (trackAnimator)
@@ -28,8 +29,8 @@ protected override void Rewind(float seconds)
2829

2930
protected override void Track()
3031
{
31-
if (trackPositionRotation)
32-
TrackPositionAndRotation();
32+
if (trackTransform)
33+
TrackTransform();
3334
if (trackVelocity)
3435
TrackVelocity();
3536
if (trackAnimator)

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/ImplementedObjects/ScaleRewind.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
using System;
12
using UnityEngine;
23
using UnityEngine.UI;
34

45

6+
/// <summary>
7+
/// In latest version, Scale tracking/rewind was added defaultly to GenericRewind alongside with tracking position and rotation. Script is still here to only show how it internally works in the simplest way possible
8+
/// </summary>
9+
[Obsolete]
510
//This script is showing setup of simple custom variable tracking
611
public class ScaleRewind : RewindAbstract
712
{

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/RewindAbstract.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ protected void Awake()
3232
Debug.LogError("TimeManager script cannot be found in scene. Time tracking cannot be started. Did you forget to put it into the scene?");
3333
}
3434

35-
trackedPositionsAndRotation = new CircularBuffer<PositionAndRotationValues>();
36-
trackedVelocities = new CircularBuffer<Vector3>();
35+
trackedTransformValues = new CircularBuffer<TransformValues>();
36+
trackedVelocities = new CircularBuffer<VelocityValues>();
3737
trackedAnimationTimes = new List<CircularBuffer<AnimationValues>>();
3838
if (animator != null)
3939
for (int i = 0; i < animator.layerCount; i++)
@@ -47,50 +47,63 @@ protected void FixedUpdate()
4747
Track();
4848
}
4949

50-
#region PositionRotation
50+
#region Transform
5151

52-
CircularBuffer<PositionAndRotationValues> trackedPositionsAndRotation;
53-
public struct PositionAndRotationValues
52+
CircularBuffer<TransformValues> trackedTransformValues;
53+
public struct TransformValues
5454
{
5555
public Vector3 position;
5656
public Quaternion rotation;
57+
public Vector3 scale;
5758
}
5859

5960
/// <summary>
6061
/// Call this method in Track() if you want to track object Position and Rotation
6162
/// </summary>
62-
protected void TrackPositionAndRotation()
63+
protected void TrackTransform()
6364
{
64-
PositionAndRotationValues valuesToWrite;
65+
TransformValues valuesToWrite;
6566
valuesToWrite.position = transform.position;
6667
valuesToWrite.rotation = transform.rotation;
67-
trackedPositionsAndRotation.WriteLastValue(valuesToWrite);
68+
valuesToWrite.scale = transform.localScale;
69+
trackedTransformValues.WriteLastValue(valuesToWrite);
6870
}
6971
/// <summary>
7072
/// Call this method in GetSnapshotFromSavedValues() to restore Position and Rotation
7173
/// </summary>
72-
protected void RestorePositionAndRotation(float seconds)
74+
protected void RestoreTransform(float seconds)
7375
{
74-
PositionAndRotationValues valuesToRead = trackedPositionsAndRotation.ReadFromBuffer(seconds);
76+
TransformValues valuesToRead = trackedTransformValues.ReadFromBuffer(seconds);
7577
transform.SetPositionAndRotation(valuesToRead.position, valuesToRead.rotation);
78+
transform.localScale= valuesToRead.scale;
7679
}
7780
#endregion
7881

7982
#region Velocity
80-
CircularBuffer<Vector3> trackedVelocities;
83+
public struct VelocityValues
84+
{
85+
public Vector3 velocity;
86+
public Vector3 angularVelocity;
87+
}
88+
CircularBuffer<VelocityValues> trackedVelocities;
8189
/// <summary>
8290
/// Call this method in Track() if you want to track velocity of Rigidbody
8391
/// </summary>
8492
protected void TrackVelocity()
8593
{
86-
8794
if (body != null)
8895
{
89-
trackedVelocities.WriteLastValue(body.velocity);
96+
VelocityValues valuesToWrite;
97+
valuesToWrite.velocity= body.velocity;
98+
valuesToWrite.angularVelocity = body.angularVelocity;
99+
trackedVelocities.WriteLastValue(valuesToWrite);
90100
}
91101
else if (body2!=null)
92102
{
93-
trackedVelocities.WriteLastValue(body2.velocity);
103+
VelocityValues valuesToWrite;
104+
valuesToWrite.velocity = body2.velocity;
105+
valuesToWrite.angularVelocity = new Vector3(body2.angularVelocity,0,0);
106+
trackedVelocities.WriteLastValue(valuesToWrite);
94107
}
95108
else
96109
{
@@ -104,11 +117,15 @@ protected void RestoreVelocity(float seconds)
104117
{
105118
if(body!=null)
106119
{
107-
body.velocity = trackedVelocities.ReadFromBuffer(seconds);
120+
VelocityValues valuesToRead= trackedVelocities.ReadFromBuffer(seconds);
121+
body.velocity = valuesToRead.velocity;
122+
body.angularVelocity = valuesToRead.angularVelocity;
108123
}
109124
else
110125
{
111-
body2.velocity = trackedVelocities.ReadFromBuffer(seconds);
126+
VelocityValues valuesToRead = trackedVelocities.ReadFromBuffer(seconds);
127+
body2.velocity = valuesToRead.velocity;
128+
body2.angularVelocity = valuesToRead.angularVelocity.x;
112129
}
113130
}
114131
#endregion

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/RewindManager.cs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@ public class RewindManager : MonoBehaviour
3131

3232
float rewindSeconds = 0;
3333

34-
private void OnEnable()
35-
{
36-
HowManySecondsAvailableForRewind = 0;
37-
}
38-
private void Awake()
39-
{
40-
RewindManager[] managers= FindObjectsOfType<RewindManager>();
41-
42-
if (managers.Length>1) //Check if each scene contains only one script with RewindManager
43-
{
44-
Debug.LogError("RewindManager cannot be more than once in each scene. Remove the other RewindManager!!!");
45-
}
46-
}
47-
48-
4934
/// <summary>
5035
/// Variable defining how much into the past should be tracked, after set limit is hit, old values will be overwritten in circular buffer
5136
/// </summary>
@@ -80,53 +65,20 @@ public void RewindTimeBySeconds(float seconds)
8065
/// <returns></returns>
8166
public void StartRewindTimeBySeconds(float seconds)
8267
{
83-
if (seconds > HowManySecondsAvailableForRewind)
84-
{
85-
Debug.LogError("Not enough stored tracked value!!! Reaching on wrong index. Called rewind should be less than HowManySecondsAvailableForRewind property");
86-
return;
87-
}
88-
if (seconds < 0)
89-
{
90-
Debug.LogError("Parameter in StartRewindTimeBySeconds() must have positive value!!!");
91-
return;
92-
}
68+
CheckReachingOutOfBounds(seconds);
9369

9470
rewindSeconds = seconds;
9571
TrackingStateCall?.Invoke(false);
9672
IsBeingRewinded = true;
9773
}
98-
private void FixedUpdate()
99-
{
100-
if (IsBeingRewinded)
101-
{
102-
RewindTimeCall?.Invoke(rewindSeconds);
103-
}
104-
else if (HowManySecondsAvailableForRewind != howManySecondsToTrack)
105-
{
106-
HowManySecondsAvailableForRewind+=Time.fixedDeltaTime;
107-
108-
if (HowManySecondsAvailableForRewind > howManySecondsToTrack)
109-
HowManySecondsAvailableForRewind = howManySecondsToTrack;
110-
}
111-
}
11274

11375
/// <summary>
11476
/// Call this method to update rewind preview while rewind is active (StartRewindTimeBySeconds() method was called before)
11577
/// </summary>
11678
/// <param name="seconds">Parameter defining how many seconds should the rewind preview move to (Parameter must be >=0)</param>
11779
public void SetTimeSecondsInRewind(float seconds)
11880
{
119-
if (seconds > HowManySecondsAvailableForRewind)
120-
{
121-
Debug.LogError("Not enough stored tracked value!!! Reaching on wrong index. Called rewind should be less than HowManySecondsAvailableForRewind property");
122-
return;
123-
}
124-
125-
if (seconds < 0)
126-
{
127-
Debug.LogError("Parameter in SetTimeSecondsInRewind() must have positive value!!!");
128-
return;
129-
}
81+
CheckReachingOutOfBounds(seconds);
13082
rewindSeconds = seconds;
13183
}
13284
/// <summary>
@@ -139,4 +91,45 @@ public void StopRewindTimeBySeconds()
13991
RestoreBuffers?.Invoke(rewindSeconds);
14092
TrackingStateCall?.Invoke(true);
14193
}
94+
private void CheckReachingOutOfBounds(float seconds)
95+
{
96+
if (seconds > HowManySecondsAvailableForRewind)
97+
{
98+
Debug.LogError("Not enough stored tracked value!!! Reaching on wrong index. Called rewind should be less than HowManySecondsAvailableForRewind property");
99+
return;
100+
}
101+
if (seconds < 0)
102+
{
103+
Debug.LogError("Parameter in StartRewindTimeBySeconds() must have positive value!!!");
104+
return;
105+
}
106+
}
107+
108+
private void Awake()
109+
{
110+
RewindManager[] managers = FindObjectsOfType<RewindManager>();
111+
112+
if (managers.Length > 1) //Check if each scene contains only one script with RewindManager
113+
{
114+
Debug.LogError("RewindManager cannot be more than once in each scene. Remove the other RewindManager!!!");
115+
}
116+
}
117+
private void OnEnable()
118+
{
119+
HowManySecondsAvailableForRewind = 0;
120+
}
121+
private void FixedUpdate()
122+
{
123+
if (IsBeingRewinded)
124+
{
125+
RewindTimeCall?.Invoke(rewindSeconds);
126+
}
127+
else if (HowManySecondsAvailableForRewind != howManySecondsToTrack)
128+
{
129+
HowManySecondsAvailableForRewind += Time.fixedDeltaTime;
130+
131+
if (HowManySecondsAvailableForRewind > howManySecondsToTrack)
132+
HowManySecondsAvailableForRewind = howManySecondsToTrack;
133+
}
134+
}
142135
}

0 commit comments

Comments
 (0)