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

UNet or Photon for physics based multiplayer?

Discussion in 'UNet' started by dberroa, Jun 23, 2016.

  1. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    If I wanted to make a physics based multiplayer VR game, which networking package should I use? I was leaning towards UNet, but I can't get the physics to sync to everyone properly. I haven't looked into photon but I hear its the most popular option.

    What do you guys think?

    Thanks.
     
  2. jaybdemented

    jaybdemented

    Joined:
    Sep 2, 2013
    Posts:
    112
    Last time I check. You can't use physics with photon. I think you need bolt which is run by those guys.
     
  3. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    So it seems there isn't an easy way to turn my physics based VR game to multiplayer?
     
  4. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    It's possible to do a physics based game in either. You have to "do it yourself" though. The networking libraries are just a way of sending messages / syncing data between peers. You have to build the "physics" stuff on top of that, which is mostly just a matter of what you sync and how you interpolate. UNet does come with the NetworkTransform component though which will sync rigidbodies pretty well. I always end up writing custom syncing code anyway though to get everything to work just how I want.
     
  5. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    For Unity, I made a scene with the default unity third person character asset which has a rigidbody and several spheres with rigidbodies.I made the characters have a network transform rigidbody sync and local authority assigned. I have local authority and network transform on the balls as well. When the host hits the ball, the ball moves and reacts properly. When the client hits the ball, it doesn't move or sometimes barely moves as if there was a super small impact.

    I guess my question is, why does the collisions detect (to me that is physics when I ask these questions) on host and not the clients. The network transform object does keep them in sync, I just can't interact with anything as a client.
     
    ImanIrt likes this.
  6. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    yeah, that's kind of the curse of physics games. the spheres are owned by the server since they are "scene" objects, so the clients can't interact with them in any way on the client. The reason you see the small interaction is probably because of the client players on the server interacting with the server's version of the sphere.

    Depending on the game there are probably a couple of way around this. One would probably be to transfer authority of the spheres as needed.

    For a lot of actions where the interaction is more or less instant I follow a pattern like this:
    Client detects collision with Server owned scene object
    Client sends a command to the server like "hit the ball in this direction"
    Client turns off syncing from the server and applies some force to the ball
    The ball interacts instantly for the client, yay

    The server gets the command from the client
    The server applies the force to the ball
    The server responds to the client letting it know to turn syncing back on

    The client receives the message and turns syncing back on

    I'm simplifying that a bunch and skipping a lot of interpolation and dealing with multiple clients but that's the general idea so that the interaction can appear instant on the client, but the server still controls everything.

    If you've got a bunch of clients all interacting with the same shared networked object rapidly and in a complex way it's just going to be hard to sync it correctly dealing with network latency. In general though, shared stuff is owned by the server, so if clients want to interact with it, they have to either send commands to the server or take ownership of it.
     
  7. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I wish there was some tutorials on this. I feel so lost since I want to do physics. I can easily get a game working that doesn't use physics from the tutorials I read but then I read advanced things like you wrote and I feel so lost :( I really want to make this game though so I won't give up, it will just take me a crap ton longer.

    For what you said above, I'm confused about when you would say/run your steps. Physics from what I know is automatically handled, hence when I make a single player game with ridgibodies, they just work, I don't write any code to add force when something collides, it just happens. Since it just happens, how would I get that to work with what you said? I could detect via code oncollisionenter to do all your commands and such, but I don't know what force and velocity etc to apply to the ball since Unity always handled it itself before.

    Side note, you have been more than helpful responding my emails and forum posts. Don't feel like you are obligated to help me if I'm bogging down your time.
     
  8. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    I worded that a little oddly I guess but once you've disabled syncing from the server the normal unity physics will take over so you shouldn't have to manually apply any forces.

    And yeah, this stuff is kind of advanced. There's really no way to make a networked game without diving into specifics of how everything is synced though. The most basic setup is pretty simple I guess, the client just sends a command to the server, and the shared object doesn't respond on the client until the server says so, it's just not a very responsive experience for the clients, so you start getting into all of this mess to try and make it better.

    And no worries about taking up my time. I enjoy lurking around here to distract myself from the work I should be doing. It's all self inflicted I assure you :)
     
    Last edited: Jun 27, 2016
  9. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    Would disabling syncing be done this way?

    "For setting the authority on objects after they are created, there are the new functions AssignClientAuthority(NetworkConnection conn) andRemoveClientAuthority(NetworkConnection conn) on the NetworkIdentity class."

    I found that from this link:

    http://stackoverflow.com/questions/31666024/unity-unet-calling-command-outside-of-player-object
     
  10. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    That may be an option but I do things a little differently. I think I do something similar to setting the NetworkTransform's sync mode to SyncNone. I'm not really sure though, I've got kind of a complicated setup using a custom script in place of NetworkTransform. It may not even be possible to disable the syncing from the client's side using NetworkTransform, you may need to extend NetworkTransform and override some functionality so that you have a way to tell it to stop listening to updates from the server.
     
  11. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    What does SyncNone do? If it doesn't sync then why have a NetworkTransform on the object at all?
     
  12. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I'm trying to create something like this:

     
  13. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    You would only turn off syncing temporarily on the client so that the object can react immediately. You turn it back on once the server has let the client know that it got the message

    After looking at the toybox video though I think you would need a combination of methods. Any time the player is interacting continuously, like picking something up and holding it, you're going to be better of switching authority via AssignClientAuthority I think. If it's more of an instantaneously reaction like players hitting a ball back and forth you can use the kind of hacky method I described.
     
  14. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    How would you do this part?

    "You turn it back on once the server has let the client know that it got the message"

    I'm guessing high level.

    OnCollisionEnter -> disable networktransform to syncnone.
    ??? (the part you mentioned)
    Once I get an answer from ??? I turn back on the syncing from the object.

    Is that the flow also that would allow me to do the back and forth ball you mentioned?

    I think I get the picking up part, I click the trigger to pick up the object and I would assign the authority to client so I report to the server where the ball is. The moment I release the trigger, I turn it back to server,,,,maybe?


    This is all conjecture right now. My touch controllers have not arrived yet. Hopefully I'll have them by next week.Then I can throw balls and stuff and see how it works.Or maybe not as I'll only have one set so I won' have a second player to see the physics....*cries*
     
  15. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I received my touch controllers from Oculus so I will be experimenting with picking up things and throwing them and then I'll have more specific questions regarding how physics work.
     
  16. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    Just want to bump this before I forget about it.