Skip to content

Commit 68052a1

Browse files
committed
Rigidbody 2D
+System now correctly tracks velocities of both Rigidbody and Rigidbody 2D
1 parent 73a06a4 commit 68052a1

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/RewindAbstract.cs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public abstract class RewindAbstract : MonoBehaviour
99
public bool IsTracking { get; set; } = false;
1010

1111
Rigidbody body;
12+
Rigidbody2D body2;
1213
Animator animator;
1314
AudioSource audioSource;
1415

@@ -20,6 +21,7 @@ protected void Awake()
2021
if (rewindManager != null)
2122
{
2223
body = GetComponent<Rigidbody>();
24+
body2 = GetComponent<Rigidbody2D>();
2325
animator = GetComponent<Animator>();
2426
audioSource = GetComponent<AudioSource>();
2527

@@ -81,17 +83,33 @@ protected void RestorePositionAndRotation(float seconds)
8183
/// </summary>
8284
protected void TrackVelocity()
8385
{
84-
if (body == null)
85-
Debug.LogError("Cannot find Rigidbody on the object, while TrackVelocity() is being called!!!");
8686

87-
trackedVelocities.WriteLastValue(body.velocity);
87+
if (body != null)
88+
{
89+
trackedVelocities.WriteLastValue(body.velocity);
90+
}
91+
else if (body2!=null)
92+
{
93+
trackedVelocities.WriteLastValue(body2.velocity);
94+
}
95+
else
96+
{
97+
Debug.LogError("Cannot find Rigidbody on the object, while TrackVelocity() is being called!!!");
98+
}
8899
}
89100
/// <summary>
90101
/// Call this method in GetSnapshotFromSavedValues() to velocity of Rigidbody
91102
/// </summary>
92103
protected void RestoreVelocity(float seconds)
93104
{
94-
body.velocity = trackedVelocities.ReadFromBuffer(seconds);
105+
if(body!=null)
106+
{
107+
body.velocity = trackedVelocities.ReadFromBuffer(seconds);
108+
}
109+
else
110+
{
111+
body2.velocity = trackedVelocities.ReadFromBuffer(seconds);
112+
}
95113
}
96114
#endregion
97115

@@ -108,19 +126,22 @@ public struct AnimationValues
108126
protected void TrackAnimator()
109127
{
110128
if(animator == null)
129+
{
111130
Debug.LogError("Cannot find Animator on the object, while TrackAnimator() is being called!!!");
112-
131+
return;
132+
}
133+
113134
animator.speed = 1;
114135

115-
for (int i=0;i<animator.layerCount;i++)
136+
for (int i = 0; i < animator.layerCount; i++)
116137
{
117138
AnimatorStateInfo animatorInfo = animator.GetCurrentAnimatorStateInfo(i);
118139

119140
AnimationValues valuesToWrite;
120141
valuesToWrite.animationStateTime = animatorInfo.normalizedTime;
121142
valuesToWrite.animationHash = animatorInfo.shortNameHash;
122143
trackedAnimationTimes[i].WriteLastValue(valuesToWrite);
123-
}
144+
}
124145
}
125146
/// <summary>
126147
/// Call this method in GetSnapshotFromSavedValues() to restore Animator state
@@ -151,16 +172,18 @@ public struct AudioTrackedData
151172
protected void TrackAudio()
152173
{
153174
if(audioSource==null)
175+
{
154176
Debug.LogError("Cannot find AudioSource on the object, while TrackAudio() is being called!!!");
155-
177+
return;
178+
}
156179

157180
audioSource.volume = 1;
158181
AudioTrackedData dataToWrite;
159182
dataToWrite.time = audioSource.time;
160183
dataToWrite.isEnabled = audioSource.enabled;
161184
dataToWrite.isPlaying = audioSource.isPlaying;
162185

163-
trackedAudioTimes.WriteLastValue(dataToWrite);
186+
trackedAudioTimes.WriteLastValue(dataToWrite);
164187
}
165188
/// <summary>
166189
/// Call this method in GetSnapshotFromSavedValues() to restore Audio
@@ -258,28 +281,37 @@ protected void TrackParticles()
258281
Debug.LogError("Particles not initialized!!! Call InitializeParticles() before the tracking starts");
259282
return;
260283
}
284+
if(particleSystemsData.Count==0)
285+
Debug.LogError("Particles Data not filled!!! Fill Particles Data in the Unity Editor");
261286

262-
for (int i = 0; i < particleSystemsData.Count; i++)
287+
try
263288
{
264-
if (particleSystemsData[i].particleSystem.isPaused)
265-
particleSystemsData[i].particleSystem.Play();
289+
for (int i = 0; i < particleSystemsData.Count; i++)
290+
{
291+
if (particleSystemsData[i].particleSystem.isPaused)
292+
particleSystemsData[i].particleSystem.Play();
266293

267-
ParticleTrackedData lastValue = trackedParticleTimes[i].ReadLastValue();
268-
float addTime = lastValue.particleTime + Time.fixedDeltaTime;
294+
ParticleTrackedData lastValue = trackedParticleTimes[i].ReadLastValue();
295+
float addTime = lastValue.particleTime + Time.fixedDeltaTime;
269296

270-
ParticleTrackedData particleData;
271-
particleData.isActive = particleSystemsData[i].particleSystemEnabler.activeInHierarchy;
297+
ParticleTrackedData particleData;
298+
particleData.isActive = particleSystemsData[i].particleSystemEnabler.activeInHierarchy;
272299

273-
if ((!lastValue.isActive) && (particleData.isActive))
274-
particleData.particleTime = 0;
275-
else if (!particleData.isActive)
276-
particleData.particleTime = 0;
277-
else
278-
particleData.particleTime = (addTime > particleTimeLimiter) ? particleResetTimeTo : addTime;
300+
if ((!lastValue.isActive) && (particleData.isActive))
301+
particleData.particleTime = 0;
302+
else if (!particleData.isActive)
303+
particleData.particleTime = 0;
304+
else
305+
particleData.particleTime = (addTime > particleTimeLimiter) ? particleResetTimeTo : addTime;
279306

307+
trackedParticleTimes[i].WriteLastValue(particleData);
308+
}
309+
}
310+
catch
311+
{
312+
Debug.LogError("Particles Data not filled properly!!! Fill both the Particle System and Particle System Enabler fields for each element");
313+
}
280314

281-
trackedParticleTimes[i].WriteLastValue(particleData);
282-
}
283315
}
284316
/// <summary>
285317
/// Call this method in GetSnapshotFromSavedValues() to Particles

0 commit comments

Comments
 (0)