Creating a Replay System in Unity
A replay system is a great way to let players relive their best (or worst) moments of gameplay, improve their strategy, and more! In this tutorial, you’ll build a simple state-based replay system and learn about how replay systems work. By Teddy Engel.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Creating a Replay System in Unity
25 mins
- Getting Started
- What is a Replay System?
- Choosing the Approach: State-based or Input-based
- What is State-based Replay?
- Pros and Cons of State-Based Replay
- What is Input-based Replay?
- Pros and Cons of Input-Based Replay
- Deterministic vs Non-Deterministic Game Engines
- Setting Up to Record
- Finding the Transforms
- Recording Action Frames
- What is a MemoryStream?
- Setting up a MemoryStream
- Starting at the Beginning
- Writing Transform Positions to the Stream
- Replaying Action Frames
- Starting at the Beginning
- Reading Positions From the Stream
- Recording the Direction
- Optimizing Memory Usage
- Capping Replay Duration
- Challenge: Skipping Frames
- Counting Frames
- Using the Counter
- Where to Go From Here?
- Reference
Using the Counter
Look in both StartReplaying
and StartRecording
and add this after the call to ResetReplayFrame
:
StartReplayFrameTimer();
Next, find UpdateRecording
and replace the call to SaveTransforms
with this:
if (replayFrameTimer == 0)
{
SaveTransforms(transforms);
ResetReplayFrameTimer();
}
--replayFrameTimer;
This code checks if replayFrameTimer
is ready for the next save, at zero. If so, you save the transforms and set the timer back to the total length, two.
Now you’ll do the same in UpdateReplaying
. Replace the call to LoadTransforms
in that method with this:
if (replayFrameTimer == 0)
{
LoadTransforms(transforms);
ResetReplayFrameTimer();
}
--replayFrameTimer;
Run the scene, record some gameplay and play it back. Notice the result is still pretty smooth. Update the Replay Frame Length on the Replay Manager component to five and try again. A lot less smooth!
[/spoiler]
Congratulations on completing the tutorial! You now have the power to control time itself!
Where to Go From Here?
You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.
In this tutorial, you’ve learned how to implement a simple state-based replay system along with some small optimizations. There are many interesting places to go from here. You could store your replay information in a file, implement an input-based system or try integrating other components like the Animator.
It’s up to you!
Here are some resources that may help you on your way:
- Saving and Loading in Unity
- Unity Games by Tutorials: To learn more about all things Unity!
- Braid: To see a state-based replay system in action.
Feel free to comment below if you have any comments or questions! :]
Reference
Replay System Using Unity
Implementing a replay system in Unity and how I’d do it differently next time
Developing Your Own Replay System