The following scripts, Ghoster.cs and Ghost.cs, were written as part of the Unity hoverboard racing simulator G-Cruiser. Ghoster.cs records time, transform, and checkpoint data as the player plays through a race, and Ghost.cs plays the recorded information back in the form of a visible but intangible racing ghost. A ghost was recorded for each course, to be played back when the player started a race, giving the player something to race against.
Ghoster.cs is fairly straightforward. Recording starts when the player either presses a recording hotkey, or passes through a trigger with the tag "Record Trigger". Recording stops when the player presses the recording hotkey again, or passes through a trigger with the tag "Record Trigger" (because the player model had multiple colliders, a timer was needed to prevent a single interaction with the trigger from registering multiple times.) The current time, remaining checkpoint count, player position, and player rotation are written to the ghost file at a polling interval set in the inspector.
Ghost.cs plays back the recorded information by linearly interpolating between the recorded transform snapshots. One complication was the fact that the player could initiate a race by passing through any one of the checkpoints designating the course; the ghost recording needed to loop, with playback beginning at the checkpoint that the player initiated the race from. The StartGhost function accepts a checkpoint index as a parameter, and the recorded data is first searched for the frame corresponding to the provided checkpoint index. Playback begins from that point.
Ghoster.cs
Ghost.cs