Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Client Side Prediction and "Resimulations"

Discussion in 'Multiplayer' started by DaUr3, Jun 19, 2023.

  1. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    Just to make sure people know what I'm talking about (in case I called it the wrong name). What I am doing to sync player inputs is whenever I enter an action/input/command, I send that command over to the server. When that command arrives at the server after a small delay, the server runs it and then sends back the resulting command to my client. I compare the resulting positions, and check how "off." If its super off, then I attempt to correct my own position.

    The bolded part is what I'm stuck on. From what I understand, I am supposed to place move my player back to correct position received from the server, then re-simulate all of the inputs that have happened between that input and where I am now. How do I do that seamlessly without messing up all of the other physics based objects' positions? Has anyone implemented client-side prediction using the Unity engine?
     
  2. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    If you want to learn more about client side prediction I recommend Gabriel's blog: https://www.gabrielgambetta.com/client-server-game-architecture.html

    Essentially you are correct, you need to rollback and resimulate. If there are physics involved this makes things much more complicated. Most games (for instance FPS games) do not run a full physics simulation but rather just move their own player characters using Kinematic Character Controllers which makes predict / rollback much easier as you simply have to run the KCC move function again multiple times with the correct inputs to rollback.

    For physics, you usually want to fully predict all dynamic bodies. To do that you need to run the physics simulation in both the prediction and reconciliation simulation steps. You can achieve that by deactivating auto simulation of physics in Unity `(Physics.autoSimulate = false)` and calling `Physics.Simulate` manually each time you simulate a simulation step.

    There are plenty of edge cases and micro optimizations in making client side prediction feel well which makes it one of the hardest parts of a game to implement properly.

    Yes I've done so many times myself and we have a library called Fusion which comes with full client side prediction built in and abstracts away all the difficult technicalities behind a framework.
     
  3. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    Does messing around with Physics.autoSimulate only simulate for the current rigidbody? I'm a little bit lost here because if I did Physics.Simulate, I could mess up the synchronization for all the bodies no, unless you are supposed to save the state of ALL physics objects in the world?
     
    Last edited: Jun 20, 2023
  4. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    It does apply to the whole physics simulation. You cannot just simulate a single rigidbody that would not be physically accurate. This is not a limitation of the physics engine but rather fundamentally needed to give accurate results. If you have rigidbodies that are not part of your multiplayer simulation e.g. offline and for visual presentation only such as particle effects etc. you can move them to a separate PhysicsScene (not the same as Unity scene) and then call simulate on each scene accordingly.
     
  5. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    Oh neat. So to understand this better, say I am assuming that 5 players in a match and I want to perform rollback on one of them (say player 1). The general procedure would be to disable auto-physics, move player 1 back to the authoritative position, then replay every single command from the point I rolled back to now for ALL players? And this is all done in a single frame or spaced out over time?

    Also I did not know that a PhysicsScene was a thing thanks for that!
     
  6. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    I think you might be confusing something here. In general prediction rollback models work like this:
    1. The server only runs a forward simulation with a fixed tick rate. It never rolls anything back. (there's some exceptions such as lag compensated raycasts but even those are not done via state rollbacks usually)
    2. Clients run this same simulation but they predict forward and then rollback and resimulate again in case of a misprediction to align themselves again with the server. And yes if you resimulate you need to replay all the commands at the correct tick and run the full simulation for that player (or the full world simulation if physics are involved). So this is very costly performance wise.
     
  7. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    Are there alternatives to doing this? Or is it just an inevitable thing that most/all physics based multiplayer games have to deal with.
     
  8. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    Most physics based games don't implement client side prediction and thus end up with subpar gameplay. It is very rare to see games that do physics interactions right. Rocket League for instance is one of the games who did this properly and it feels very nice to play or another example would be Stumble Guys that has been made with Quantum. Quantum is what I generally recommend to people building multiplayer physics based games since you will not run into those kinds of problems and in fact can just code your game as if it were single player. But it is a very different way of coding since it is deterministic which is a different networking model so it has a bit of a steeper learning curve.
     
    DaUr3 likes this.
  9. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    Hmm I see. I've used Photon in the past, but for some reason I just felt like implementing prediction by myself. From reading articles online I didn't think it'd be too bad, but this is certainly really hard. I guess I have a little bit more respect for the guys who implemented this system for Totally Accurate Battlegrounds. That must've been tough.
     
  10. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    If you do want starting grounds you're welcome to take a look at Fish-Networking. It already offers physics prediction and the prediction code is getting a nice improvement(currently in experimental) as well. You are free to examine the code, modify, and use your modifications, so long as you are not doing so with the intents to release your own networking solution for the public. It's essentially a clause to protect the work put into Fish-Networking, but otherwise by all means have a look.
     
  11. DaUr3

    DaUr3

    Joined:
    Nov 30, 2016
    Posts:
    29
    I'm just curious since I did not know about the PhysicsScene thing until yesterday, did you also use that to handle physics predictions or is there a different system?