Our replay system does not record the velocity

I know this isn’t ideal but I would like to see how to record the velocity of the player.
It may seem pretty simle: just do :

keyFrames[frame] = new MyKeyFrame(time, transform.position, transform.rotation, m_Rigidbody.velocity);

and

m_Rigidbody.velocity = keyFrames[frame].velocity;

while ofcourse adding the velocity field to our struct.
But I can’t seem to get it to work. Maybe I’m just forgetting something stupid.

Hi @GrandBOOM,

You’ve probably already figured this out, but on the off chance you haven’t…


The velocity of the Rigidbody can certainly be recorded that way, but you won’t see any effects during playback because we decided to make the Rigidbody Kinematic during Playback.
Velocity, being a force, can’t affect the Rigidbody in this state. This is definitely a good thing, because the physics will muddy everything up (colliding with things, moving against/with Gravity, etc.).

However, there’s nothing to stop you from adding the appropriate velocity to the Rigidbody when you resume recording.

As an example, consider when you jump (ever), or “ramp off” the cube (run into cube and jump so you get launched), then replay. If you stop the Replay while you’re in the air, the ball just… drops. That’s kinda dumb. Instead, keep track of the “replayedVelocity” (Vector3) in the last frame, and add that to the RigidBody when you get out of record mode and allow the Rigidbody to react to physics when you stop playback.

If you have the replay system rewinding everything instead of (or as well as!) replaying, you can even invert the velocity with “[velocity variable here] * -1”.

Hopes this helps someone, and sorry for the Necro / Text wall. :wink:

Adding on to this. Do we even need to store the velocity? Between the previous keyframe and the current one we have the position and time. The delta of the positions is the distance traveled over the time which would be your velocity.

Vector3 distance = Frame[n].position - Frame[n-1].position;
Float timeBetween = Frame[n].time- Frame[n-1].time;
Vector3 velocity = distance / timeBetween;

Privacy & Terms