Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Rigidbody client-side prediction using Physics.Simulate

Discussion in 'Multiplayer' started by teremy, Feb 24, 2022.

  1. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    88
    Hi.
    I am making a multiplayer game and try to sync rigidbodies.
    I want to implement client-side prediction, so the client sends the server a move-request while applying the movement locally (addForce etc.).
    The server at some point will send a response and the client possibly has todo some error correction.
    When the client receives the last valid position of the rigidbody it has already predicted some more move-requests, so the clients sets the valid position and now I want to apply the predicted move-requests again to get to the correct current state.
    My problem is: I can't just call Physics.Simulate multiple times, because this would also affect all the other rigidbodies, but an extra physics scene also doesn't make sense, because I still have to consider collision with other objects. What I want is for Physics.Simulate to just move one specific game object, while checking collisions with other objects, but not moving them also.
    Since this won't work, do you have any ideas what I could do instead?
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    389
    Welcome to the problem even experienced devs struggle with.
    You did not say what networking solution you were using, so I'm going to recommend to you Fish-Networking.
    It has CSP built in among other wonderful features, all free. There's a rigidbody CSP demo as well.
    Info + links: https://fish-networking.gitbook.io/docs/
     
  3. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    88
    Thanks for your answer. I have my own network solution with nat punchthrough and everything. But this always bugged me, because I haven't been able to figure out this problem. I like to make physics based games, but without client-side prediction I'm not happy.
     
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    389
    Ah, well it seems like you might have to adventure down that road solo then I'm afraid. There's a lot of articles on CSP. Unfortunately most if not all lack the full picture. If I had any tutorials to recommend I would point you to them, but I learned through a lot of trial and error.
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    This is less about CSP and more about trying to get PhysX to simulate a scene in a way that fits your requirements. It sounds like you only want to predict the player character's RB, while all other bodies only get their state from the server? One solution would be to set all non-predicted bodies to kinematic for the client's local physics step. PhysX in Unity doesn't have any other way of partially simulating scenes, afaik.

    ...and regarding PhysX in Unity for client side prediction, I wrote a long post here on the subject. Tl;dr, it's not a great tool for networking in general, but isn't entirely unusable, either.
     
    teremy likes this.
  6. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    88
    Thanks for your reply. That might actually work. I will probably keep a list of rigidbodies, that are close to the rigidbody that I want to predict, so I only have to change those few rigidbodies to kinematic. I have concerns about performance, but I will just try it.
     
  7. InevitableDev

    InevitableDev

    Joined:
    Apr 6, 2021
    Posts:
    3
  8. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    389
    I've seen this in the past, and didn't recommend it because it's an incomplete solution.
    While this git is certainly an okay start, it's missing several fundamentals for CSP to work in the wild.

    Another thing to note that I think very much applies to the thread creator, the video does not demonstrate rigidbodies interacting with each other. I can say with certainty that the code as of this date does not support rigidbody interactions properly.

    If you are however looking for a place to start the git is fine, but still very far from complete.
     
  9. scottalexgray

    scottalexgray

    Joined:
    May 14, 2013
    Posts:
    2
    If you haven't done client side prediction before, I would start by developing a non-rigidbody solution first to get the hang of client side prediction and make sure you know very well in what order things need to happen so you can start thinking about how to make a rigidbody version.
     
  10. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Sorry, going to briefly revive this thread because only a half answer was given.

    You indeed need to set other rigidbodies to kinematic, but you also need to store the velocity and angular velocity. After you rollback, set your rigidbodies back to dynamic and re-apply the forces that were lost once you set to kinematic. After that, you're good to go.

    I use this method to locally simulate dead player ragdolls on the client with no synchronization from the server, since it's not needed in my game.

    Not sure if this is what OP is talking about, but sure sounds like it.
     
    Kobix likes this.
  11. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    389
    This stops you from colliding with other objects during rollback which means any desyncs will continue after a rollback. That defeats the whole purpose of rolling back.

    Such a solution could be an option if only the server can move the rigidbodies but their interactions will also be delayed based on server and client ping.
     
  12. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    168
  13. D0C_

    D0C_

    Joined:
    Apr 20, 2021
    Posts:
    1
    Mate you are a GOD, the idea of setting the rigidbodies to kinematic and enabling them when simulating saved me, i tried everything even using multiple physics scenes, and now with your answer i got my game movement to feel smooth after 2 months with bad client side prediction
     
  14. DanielZub

    DanielZub

    Joined:
    Oct 18, 2017
    Posts:
    1
    Hey mate, do you have maybe some small piece of code with that idea implementation?
    I'm struggling with it and unfortunately does not work.