Physics/Character Controller Determinism

Hi,

I’m running in little trouble here. I try to create a little puzzle game based on physics and cloning.
The thing I want to do is to be able to replay the action of any object based on the user input. To be a little more specific, I have a character that move around a map using the default (but tweaked) 3rd person controller. Then when the player triggered a specific script, the game instantiate a clone of my character and give the control to the user. Then, The firsts characters (because this can be repeated over and over) has to redo what the user did before.

So far I’ve isolated the input into a new class (I’m coding just with c#) and save them into a dictionary (sort of hash map). The key is the frame number and the value is an object representing input for this particular frame. Then when I have to replay the “action of the user” I simulated input for each frame with the corresponding one that was saved in the dictionary. As you can expect I have reset the position and the rotation of the character at each beginning of the redo.

That technique works but is far from being perfect. The longer I run the simulation the more precision I lost. For example, if I move around in a 30m diameter circle for 1 minute, I get an error of about 1m on the final position. This can be problematic for my game, as I need position to trigger scripts for my game. Moreover, even a very small lost of precision can be very bad. I experienced this, when I walked on a edge and when I replayed the “simulation” the “clone” fell of the edge because he was only 10cm further.

About the code (I can post every lines of it if you want) I used FixedUpdate() everywhere and use FixedDeltaTime for the math.

Do I miss something or Is Unity (physics and character controller) unable to get the same result on the same hardware based on the input ?

Thanks in Advance,

I’m not sure if this will help on unity but i’ve done this before in another game and it worked out well.

Use the command pattern for sending messages and the times at which the messages came in. Then replay the messages back. This only works well if your actor doesn’t know the difference between the player message and a “simulated” message.
I’m assuming that the physics is particularly whats getting to your loss in precision. So it may be beneficial to record key positions as well and ensure those match up along the way, specifically start motion and end motion positions.

Thanks for the answer. For the “concept” I’m quite sure it can work (At least in a fully deterministic engine) . As you say you successfully implemented it and I see no reason why it can’t work.

About recording position (and more generally consequences instead of causes) I don’t want to do that because that would imply that the clone will not be able to interact with the world when “we replay” the record. For example, if you put a crate on the path of a clone it will go through the crate if you use recorded positions. With input recording the clone will act in a dumb way trying to go through the crate. But I’m totally ok with that.

For now, I will do more test with my code to see if I have not missed something.