Search Unity

Best way to sync movement of objects on udp server?

Discussion in 'Multiplayer' started by KennethGames2020, Apr 3, 2020.

  1. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    I understand some basic concepts behind how networking works. I have set up a basic chat system where you can enter text on a phone (acting as the client) and send it to the server console (stationed on a computer) so that it can be broadcasted to other connectes clients. All this done with udp. With that in mind, what methods do game developers use to sync player movement with clients and sync objects with rigidbodies such as vehicles?

    In other words, how do I make sure that the position of each moving object is the same between clients?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Syncing the positions is easy, just send a message to all the clients with the new position. The rest though basically depends on what you need. The trickiest to implement is probably client side prediction of server authoritative movement. There's entire threads on client side prediction, and it is not needed for every game.

    Getting smooth movement client side of other objects can also be tricky. Likely you've got a server running at a higher frame rate than you're actually sending position updates, and the nature of the internet is that arrival times vary. So you can't just set the position of the transform on the client for every object you get an update for because you will have extremely choppy movement. You have to implement some form of interpolation. The way I do it is I receive messages from the server with the new position and include a time stamp in the message. When I receive the next position update I subtract one time stamp from the other to get how long on the server was between each position. The I lerp on the client for that amount of time between the two positions. I do this basically with a queue of new positions. If the client starts getting a little behind I speed up the interpolation a bit, if I get ahead (queue is empty when it should have the next position) I slow it down just a bit. YMMV

    As far as rigidbodies, if you don't specifically need them on the clients I just disable them (not really disable, but there's some settings you can do so they don't really do anything). Doing all the physics on the server and just syncing the positions on the clients is a lot less complicated. Though you'll probably need them if you do the previously mentioned client side prediction.
     
    joanpescador likes this.