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 how to create single player and multiplayer in same scene?

Discussion in 'UNet' started by dberroa, Jul 13, 2016.

  1. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    If I wanted to make a level that could be played single player or the same level coop with a friend, what is the best practice for doing this in UNet? Could I code the level purely with commands and network functions and then in single player have it host a server on lan to act like its playing single player? Or should I go through the effort of making single player and multiplayer scripts that do the same thing but one of the network and one not?

    Any other ideas, tips, tricks, etc?

    Thanks.
     
  2. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    Go with your first idea, as it'd make it so, so much more simpler and less taxing. Plus, you'll also have the ability to add in drop-in/drop-out functionality if you so desire without forcing the game to go through a load screen for the player who was joined. And because you're connected to yourself, there's no lag and no chances of dropped packets so it'd play pretty much the same regardless.

    The only real problem I can think of (that's easy enough to work around), is that all of your interactables need to be able to be interacted with by both characters and that'd require precise setup of Commands/RPCs to work right. Not that difficult, just annoying the first time you think you get it working but then it turns out you're sending Commands from an object that player doesn't have authority over.
     
  3. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    Do you know of an example tutorial or video that can show what you mean by the easy work around to getting the interactables usable by both characters? I'm not familiar with the object authority you mentioned. The tutorial from unity I believe only mention authority for the players, not for the environment, so the players can report their position to the server when they move.
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Have you considered using Photon Unity Networking instead of UNET?

    With PUN you can set the networking system to Offline Mode, and your existing networking code will work exactly the same except it doesn't require internet access and doesn't actually try to connect to anything.

    From their docs.
    Offline mode can be set to re-use your multiplayer code in singleplayer game modes. When this is on PhotonNetwork will not create any connections and there is near to no overhead. Mostly usefull for reusing RPC's and PhotonNetwork.Instantiate

    It makes it trivial to implement both single player and multiplayer in the same game.
     
  5. KnightPista

    KnightPista

    Joined:
    May 18, 2015
    Posts:
    40
    I'm using the first solution - single player is just Local host without NetworkDiscovery. Works great.
     
  6. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I looked at Photon but I asked around and apparently it may be easier to use Unity Networking that Photon. So far it seems hard to find tutorials and get help for UNet however.

    I'm also worried about pricing for photon.
     
  7. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Personaly i use Action.

    An action when offline and i spawn an object which replace the Action for the online.

    It's seems so bad, but the only solution i found on the moment =/ .
     
  8. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    What is Action?
     
  9. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    An Action<> in C#
    A delegate in some case.


    For the local game i did this sort of code:
    delegate x = LocalFunction;

    And for the online the same variable is set like this :
    delegate x = NetworkedFunction;



    And in your code, if you call x(); you call Local or Networked function. It was transparent, but i don't know if playing with LAN was not better =/
     
  10. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I've been using C# for several years and never heard of actions. It seems I'm not using the language to it's full potential.

    I think your method is a valid way to handle it. Your way cuts the code very clean so there is definitely a single player code base and a multiplayer code base. Might be better than the lan method as I assume the networking stuff would be happening in the background.

    I'll keep this method in mind. So far I'm leaning towards the lan host option.
     
  11. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    My solution was etablish because i have had some Local plug-in on my company : Like state machine, characterController2D....

    And it was used in multiple project, not only online.
    So i did this. The principle idea was function pointer, but C# doesn't has it ...
     
  12. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    I've used both fairly extensively, and I've found that Photon far easier to use, not least because at the moment UNET is so buggy.

    But even once they've fixed the bugs, the pricing of UNET will still be far higher than Photon.
     
  13. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    I heard somewhere that Unity is better for physics and ridgidbodies over Photon. Is that true or false?
     
  14. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Photon doesn't have a sync rigidbody option for it's transformview component, however unlike UNET the Photon transformview interpolation works.

    The upshot of this is that currently, unless you want to roll your own interpolation code, with UNET the only way to get smooth movement on remote clients is to have a rigidbody and synchronise that. This means you also need to leave the rigidbody enabled on the remote clients, which can present complications.

    With Photon the tranformview interpolation works very well, so you can just let the rigidbody physics move the local object, and then, with the rigidbody disabled on remote clients, their position will be synchronised smoothly by the transfornview component.

    Personally I found with the UNET HLAPI I was constantly fighting with it's quirks and limitations and a lot of my code was a compromise as a result, whilst with Photon whatever I want to do just works (as long as what I'm trying to do isn't based on flawed logic).

    If there's any disadvantage of Photon vs UNET when it comes to using rigidbodies and physics I haven't encountered it.
     
    Last edited: Jul 15, 2016
  15. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    Have you been able to sync physics easily with photon? I made a topic asking about it here: http://forum.unity3d.com/threads/unet-or-photon-for-physics-based-multiplayer.412863/ awhile back.

    I'm trying to make a networked VR multiplayer physics game involving balls, but so far in UNet, the balls do not stay perfectly synced across all clients. I was just using the network transform with ridgidbody sync. If Photon can do something like the toybox demo from Oculus
    easily then I should probably use that.

    This thread here makes it sound like photon can't do physics well:

    http://forum.unity3d.com/threads/pun-how-do-i-handle-the-physics.298207/
     
    Last edited: Jul 15, 2016
  16. anamta

    anamta

    Joined:
    Jun 13, 2016
    Posts:
    1
    I'm looking an answer to the same question but no success :/
    m also using unet not photon
     
  17. dberroa

    dberroa

    Joined:
    Dec 16, 2012
    Posts:
    146
    If you find anything, report it back here.