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

Third Party PUN - How do i handle the physics?

Discussion in 'Multiplayer' started by Compressed, Feb 15, 2015.

  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Hi,
    I started implementing multiplayer into my game (it's an fps of sorts). I had no problems getting object positions synced across the screen and syncing their animations and such, but then I encountered a hurdle. The physics aren't working correctly over the network. Well they are, the problem is, if there are 20 clients connected they are all running their own physics simulation, so let's say i have a tin can spawned in the game. If the player who spawned the tin can kicks it, it will move but it will be jerky on other connected clients because their physics engines are all moving their copy of the can differently and the owner client of the can keeps "fixing" its position according to his own physics engine which is authoritative over the can.
    I can set the can's rigidbody as kinematic, for everyone except the owner of the can and this completely fixes the stuttering, the can moves perfectly in sync and fluently too, but this has a big downside. The can is glued down for everyone but the owner client. So other players can't kick or move the can at all.
    As you guessed, this is a problem caused by not using an authoritative server, which means that the server is owner of all physics objects in the game and is the only one who moves them around in his own physics simulation and then tells everyone where they are.
    This makes me assume that the only solution to my problem is to use authoritative server.
    But how can I solve my issue with photon unity networking? Photon is basically a packet router, it has no idea that unity physics exists, it just relays messages between the connected players.
    Is it then possible to use the PUN as an authoritative server with some plugin or something?
    Could i for example randomly select one connected client in the multiplayer game session and set him as owner of all objects and then redirect input for players to this player's game, so other players would basically moved their character around on his computer..? This would of course be problematic if that master player's computer was slow, or had a bad connection. But is that solution even feasible?

    I also researched other options different that PUN, for example the uLink or Bolt seemed up to the task, but they both cost money which I don't want to spend since this game is a hobby project.
     
  2. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Photon Cloud uses an authoritative server (from the maker of PUN). Not sure if it integrates in regular PUN (instead of PUN + which costs money) but it's worth checking out if it does.

    In general though (authoritative server or not) I'm not sure why you are running the physics simulation on all the clients? Generally you should check if the object isMine and only do the physics simulation if it is. The other clients would simply be getting the position from the client that is the owner. In general this will still have stutter so if you want to smooth it out also send velocity so the other clients can update each frame on last known position + (velocity * timestamp drift). There are also more advanced techniques if this still isn't good enough if your objects change direction suddenly or other things.
     
  3. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    The player is a rigidbody moved by applied force, if i don't run physics on all clients, then only the master client will be able to move (if i don't go with the way where i transmit client input to the master client, but that is not so good due to input lag, even if the master client is a server)
    To be more precise, i don't have problems with cheating detection (as i don't need to have any) so i don't mind doing letting clients handling collisions and such i just don't know how to correctly handle it so that all player can kick the can around and see it move in the same way.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If the can is not integral to gameplay, then just send an rpc with vector force, and don't sync this object.

    If it's integral to gameplay then sync transform / rigidbody position for it. See Photon docs.

    Solving beef jerky:

    1. use interpolation on rigidbodies
    2. refer to the photon examples
     
  5. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    Physics is extremely tricky with simple syncing of positions and rotations because information takes different times to get to different computers. Often, you'll get different results so you'll have to send even more data for keeping everyone on the same page. The best method is probably using an authoritative server, which, unfortunately, is not something PUN can do easily. Instead, you should use an authoritative server where all physics, bullets, etc. are simulated on 1 machine (the server) and the results (positions, health, etc.) are sent back to everyone. Forge is a decent and easy to use solution that allows for authoritative servers.
     
  6. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,797
    You can't sync physics over the network because of that exact reason - each client will run their own physics simulation.

    The best solution I have found is to have an authoritative server and a networking solution such as Forge or uLink whose server runs in a Unity instance and is physics aware, and it does the physics simulation and sends the resulting transforms to all the clients, so all clients have the same 'physics result'.

    Also these kind of solutions makes it really cool as you can debug your server and client instances in seperate editor windows.
     
  7. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Problem is that all those solutions you suggested such as forge or ulink cost money, which i don't want to spend on a hobby project. Or do they have free licenses?
    I was thinking about going authoritative with pun as i mentioned above where i would let one client run the physics and all other clients would only get transform position and rotation updates, there would be a bit of a input lag i suppose, but that will be there with all authoritative solutions, but it is better than having jerky physics.