Search Unity

Question Looking for approaches for implementing Client Prediction

Discussion in 'Multiplayer' started by MrBigly, Jan 14, 2023.

  1. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    I am looking for materials to study on how client prediction can be implemented for a basic FPS. Any pointers to resources would be appreciated.

    For example, my first problem is how client A's gravity is interfering with updating the position of a pawn driven by client B.
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    FishNet actually has a FPS example(FPSLand) of this on the Pro tier https://fish-networking.gitbook.io/docs/master/pro
    Note there is a buy-in of $10 for Pro.

    FishNet also has CSP built-in the free version as well. This way you don't have to go through the struggles of learning how to implement client side prediction yourself.

    If you're looking to create your own client-side prediction solution I unfortunately do you not have one specific article to point you towards. When learning how to do it myself I had browsed dozens of articles and spent 50 or so hours on trial and error because the information is so segmented. And that was just to implement basic client side prediction for something like a charactercontroller or transform controller. Ridgidbodies are significantly more complex. You are welcome to hop into my server if you want to ask questions. But keep in mind the likeliness of having code handed to you is slim; you would rather receive more technical information on how it works.
     
  3. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221

    I noticed you have a component Predictive Object. Do you use this on replicated pawns? if not, how do you share ownership with a pawn that has a conflict between authoritative state position and local gravity?


    (BTW I tried to post on your discord, but you didn't appear to see my comments.)
     
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    I don't always acknowledge questions right away if I'm really busy. I think I did get to your question later on, right?
     
  5. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    I am sorry to say, no you did not. My question didn't have to do with assets in general, but replica pawns specifically.

    I realize you can take ownership of assets, but in general they are not owned by other players, so you won't have conflicts. But a player's pawn is different.

    How can Client A share ownership of a replica pawn on Client A that is owned by a player on Client B?

    Without sharing ownership even temporarily, B could be standing on A's head, and when A moved out from under B, B should immediately drop. But with significant lag, until client A receives state indicating B is falling, A will see B float in the air.

    I would think that in general Client predictive algorithms should make the local experience responsive, not just for the local pawn's motion.
     
  6. qNaN

    qNaN

    Joined:
    Dec 29, 2015
    Posts:
    26
    Go with Photon Fusion instead, they have everything, so you don't have to do a thing for Clientside Prediction and Reconcilliation. It works for physics as well.

    Photon Fusion is the most professional thing you will get for Unity and up to par with AAA solutions.
    In comparison, Netcode is still lying in the crib.

    If you delve into this thing yourself, I hope you are aware how big a job it will be, and I hope you asked yourself what it is you want to do: Be an engineer or a game developer.
    And also, you might get overtaken by development, both by Netcode itself or other solutions like FishNet, if it continues on the current trajectory.
     
  7. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Thank you for your thoughts, your points sound reasonable.

    How does Fusion handle the scenario I mentioned above?
     
  8. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    This is relatively easy. You just simulate things such as gravity locally like you would in a single player game. If there are any desynchronizations FishNet's PredictedObject component will sort it out.

    Fusion is indeed another choice that has CSP which I'd trust to use. Just be sure to research expected fees before committing to Fusion.
     
    MrBigly likes this.
  9. qNaN

    qNaN

    Joined:
    Dec 29, 2015
    Posts:
    26
    Not entirely sure, do you mean how you get Clientside Prediction and Reconcilliation to work?
    If you go through the Fusion 100 tutorial, it will show you the basics.
    https://doc.photonengine.com/en-us/fusion/current/fusion-100/overview

    Basically it will take care of it itself if you set the right settings. Through NetworkTransform (similar to Netcode's) everything will be handled automatically. For physics there is a NetworkRigidBody, which extends Unity's own RigidBody. You will just use that instead of RigidBody, and with the right settings, it will take care of everything for you.
    If you already have experience with Netcode, this should be a very easy transition.

    To be frank, I don't understand why Unity didn't just buy Photon and outright added it, instead of Netcode.
    Maybe the company wasn't for sale or was prohibitively expensive.
    But now Unity has a huge problem, because Unreal has a stellar networking solution while Unity has something that is very immature and moving at a snail's pace.
     
    Last edited: Jan 25, 2023
    mishakozlov74 likes this.
  10. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    No, what I was asking is how does Fusion behave or demonstrate sharing of ownership of another client's pawn when it interacts with it. I don't think I was getting that question across clearly at all in this thread, and I apologize for that.

    I looked at AAA titles and it surprised me to see some just hang in mid air, while others showed no respect for collision at all (the replicas are authoritative driven exclusively, no client side physics at all). At this point, I don't think it matters. I don't think there is a right way to do this, So I am just going to keep rolling my own.

    If anyone does know how fishnet or fusion behaves when owned pawn A moves out from under replica pawn B on client A, does replica pawn B drop or stay suspended in mid air until told by the authority to drop? But now this is just for curiosity, not a need to know to move forward with my designing at this point.
     
  11. qNaN

    qNaN

    Joined:
    Dec 29, 2015
    Posts:
    26
    Fusion will have Clientside Prediction, which means that the Client always will simulate what is happening. It will make a prediction. If that prediction is wrong, it will get overruled by the Server, which will then force the Client to Reconcillate (put misaligned objects in their right place in a neat way that avoids a violent pop - and then re-simulate all Predictions after the corrected Tick).
    This means that the Client will run the same simulation as the Server, but since it is not possible to predict player actions, stuff will misalign - and often.

    Since the Client runs the same simulation as the Server, I can't really see how something would stay hanging? That would mean that something is wrong with the simulation, and will hang on both Client and Server (as it is the same simulation).
    If something is misaligned, then the Server will force the Client to update properly. So if indeed something hangs on a Client, it will be corrected and forced to Reconcilliate by the Server.
    If the ping to the Server is, say, 50 ms, then that would be around the duration a misalignment could be around. But as I said, something is wrong with your simulation if something hangs, and that should be dealt with.
    There is no way Fusion would just lose contact with some objects during the simulation.
     
    Last edited: Jan 26, 2023
  12. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Simulate massive lag on client A. If pawn A moves out from under pawn B, it will take a while for the server to receive the new position of pawn A, then calculate pawn B should fall, then report it back to client A. In the mean time, pawn B is hanging due to the last authoritative position that was in the air.

    OR...

    pawn B will fall to the ground due to local gravity, which now means that client A is owning its copy of pawn B. How to make this behavior work clean is the question.

    The more lag, the more it is obvious. Of course. But the proper way to handle it should make lag irrelevant to client A's player.

    I cannot even begin to tell you how much fun I am having with client prediction coding. This is the part I have been looking forward to.
     
  13. qNaN

    qNaN

    Joined:
    Dec 29, 2015
    Posts:
    26
    There is no way that an object will be able to change ownership like that in Fusion.

    Im not sure you completely understand the system yet. It seems to me that you still have some serious reading up to do.

    But anyway, good luck to you. It is a big area you are delving into. If you have success, you should really consider making an asset for the asset store out of it. I bet many people will be very interested. A free alternative to Fusion would be extremely attractive.
     
  14. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Good to know.
     
  15. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    168
  16. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    This does not change what I mentioned. You just simulate constant forces on objects, owned or not. If there's any error FishNet will figure it out. Fusion probably corrects in a similar way.
     
    Last edited: Jan 29, 2023
  17. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Thanks ep1s0de, I appreciate the list... EDIT: but it turns out they don't come close to touching on the issues I am raising here. They are pretty basic in content. However, I will say I did learn something about the Source engine and how it pushes state out periodically. So when I lowered the rate of pushing out state on my netcode to roughly a period of 80ms, it didn't surprise me that I began to notice the degradation on the other end and it reminded me of Half Life's MP experience.

    In one AAA title I tested this scenario, pawn B hung in the air, jittering up and down by just a few percent of the height of the pawn. Based upon my own work, I can only assume it was jittering as it hung because the authoritative position is fighting gravity, until the authoritative position updates appropriately and it drops as it should. That was my first experience with my own netcode and the reason for this thread. My quest is to avoid it hanging at all and just drop smoothly even though the authoritative position states indicate it should still be in the air.

    With your framework, what would be the resulting behavior? If Client A will see pawn B smoothly drop to the ground (best scenario), then can you tell us (1) how you decide to allow Client A to override the authoritative position of pawn B until the authoritative position catches up? and (2) how you decide to cease overriding the authoritative position for pawn B? What triggers do you use for those two decisions?
     
    Last edited: Feb 4, 2023
  18. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    So I did a little more research with some AAA titles. I am increasingly convinced that the industry definition of client prediction is relative to local Pawns only. If the Pawn picks up a weapon on the board, it is only because the authority said it could. It seems that without authority approval, the local Pawn can't perform the visual elements of a pickup. And if the weapon isn't picked up, it doesn't move locally without authority approval.

    If this is truly the industry's way of dealing with client prediction, then client prediction fell into place on my netcode with no effort on my part.

    Am I understanding what client prediction means here?