Search Unity

Feedback needed! New Doc for Getting Started with Unity Multiplayer

Discussion in 'Multiplayer' started by Erik-Juhl, Nov 19, 2015.

  1. Erik-Juhl

    Erik-Juhl

    Unity Technologies

    Joined:
    Jul 11, 2012
    Posts:
    59
    Hello all
    We've been putting together a new doc for getting started with unity multiplayer. We hope this better helps everyone but we want to hear from you if this was indeed helpful. Please check out the doc linked here and let us know what you think. We ask that you look past the current formatting, placeholder screenshots, misspellings, bad grammar, etc. This is a rough first draft to see if we are going about this the right way for you guys and actually making it easier to get started with Unity Multiplayer.
     

    Attached Files:

  2. RikuTheFuffs

    RikuTheFuffs

    Joined:
    Sep 9, 2013
    Posts:
    15
    Hi Erik,
    I really appreciated this document. I gave a fast look at it, and the main idea looks great. Please go on!
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hi Erik,

    While I worked all that out myself, I can see how much of a massive, massive time saver it would have been to have a digestible doc that just says how it is, so that would have helped me a great deal - certainly more helpful than trying to pick part demos which all have different styles and no real overview of explaining why decisions were made (fairly important to explain why given the number of multiple ways to skin this fish).

    The doc needs work for formatting but I think you know that already. Please keep it up for tackling different areas of networking!

    So I think it's a winner. More please!
     
    alex_jasper and chiapet1021 like this.
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    EDIT: Note that this version is to be included for those developers who wanted to just "scan" or "give a glance" to find relevant information/references without needing to read the entire doc. Please understand.

    Can you also add an ELI5 condensed version of the code (given below) to that doc, like an Appendix or, SSCCE version?

    Code (CSharp):
    1. using UnityEngine.Networking;
    2.  
    3. public class CustomScript : NetworkBehaviour {
    4.     [ServerCallback]
    5.     public void ServerStuff(GameObject newObject, Vector3 newObjectPosition){
    6.         //Do server-side stuff. Initializing all game stuffs can be used here. Syncing objects can also be used here.
    7.  
    8.         //Let's pretend "newObject" has NetworkTransform attached.
    9.         newObject.transform.position = newObjectPosition;
    10.  
    11.         //Now newObject's NetworkTransform will sync the new position across all connected clients. The values are updated on the server
    12.         //side, all clients will have their old positions marked as Dirty() and will update to the server's values.
    13.     }
    14.  
    15.     [ClientCallback]
    16.     public void ClientStuff(){
    17.         //Do client-side stuff.
    18.  
    19.         //Client may have this registered prefab from the Network Manager, but usually it's better to have it available in the Hierarchy before continuing. Usually, it's assigned directly through the Inspector, but let's go with this.
    20.         GameObject prefabObject = GameObject.FindGameObjectWithTag("SamplePrefab");
    21.         if (prefabObject != null){
    22.             //Always check for null. In UNET, there may be times where the object is not spawned through NetworkServer, thus the object will be missing.
    23.             CmdDoAction(prefabObject);
    24.         }
    25.     }
    26.  
    27.     [Command]
    28.     public void CmdDoAction(GameObject objectToUse){
    29.         //This is where you would do stuffs that the client wants the server to do.
    30.         GameObject obj = Instantiate<GameObject>(objectToUse);
    31.         NetworkServer.SpawnWithClientAuthority(obj, this.connectionToClient);
    32.  
    33.         //Or just have the server do something for the client.
    34.         obj = Instantiate<GameObject>(objectToUse);
    35.         NetworkServer.Spawn(obj);
    36.         ServerStuff(obj, new Vector3(0f, 10f, 0f));
    37.  
    38.         //Once the server has done whatever it needs to do, you can choose if you want to have all clients do other actions.
    39.         RpcDoAction();
    40.     }
    41.  
    42.     [ClientRpc]
    43.     public void RpcDoAction(){
    44.         //Do broadcasting stuffs. Stuffs where all clients (local clients = LAN host, and remote clients = LAN clients) will do.
    45.  
    46.         //Because there exists a game object with client authority (meaning the game object's NetworkIdentity has "Local Player Authority" property
    47.         //enabled), it means it's better to check for NetworkBehaviour.hasAuthority, rather than NetworkBehaviour.isLocalPlayer.
    48.         if (!this.hasAuthority){
    49.             //Clients with no authority do stuffs here.
    50.  
    51.             //Or if you want, you can do something that all other clients without authority of this game object can do.
    52.             Ping(new Vector3(0f, 10f, 0f), Color.red);
    53.         }
    54.         else {
    55.             //Do something in which the client itself has control over the game object of local authority.
    56.  
    57.             //Then do whatever you need to do that tells the player the unit has spawned, and all other enemies have taken notice of it.
    58.             Ping(new Vector3(0f, 10f, 0f), Color.green);
    59.         }
    60.         return;
    61.     }
    62.  
    63.     public void Ping(Vector3 position, Color color){
    64.         //Pretend this is a function which pings a colored ! symbol on a minimap.
    65.     }
    66.  
    67.     public void Update(){
    68.         //Because this is a custom NetworkBehaviour script, this is attached to a game object that the client may use.
    69.         if (Input.GetMouseButtonUp(0)){
    70.             //When something occurred...
    71.             ClientStuff();
    72.         }
    73.     }
    74. }
     
    Last edited: Nov 23, 2015
  5. woodsynzl

    woodsynzl

    Joined:
    Apr 8, 2015
    Posts:
    156
    I have some scripts I've been working on for my game here.

    I built it for the new UI, if you know a bit about coding and linking buttons with functions you can have a look here.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkHelper : MonoBehaviour {
    6.  
    7.     public NetworkManager manager;
    8.  
    9.     public void StartHost()
    10.     {
    11.         manager.StartHost();
    12.     }
    13.  
    14.     public void StartClient()
    15.     {
    16.         manager.StartClient();
    17.     }
    18.  
    19.     public void StartServer()
    20.     {
    21.         manager.StartServer();
    22.     }
    23.  
    24.     public void Stop()
    25.     {
    26.         manager.StopHost();
    27.     }
    28.  
    29. }
    ^SInce the functions are public you can directly tie them to button functions. Easy!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkPanel : MonoBehaviour {
    6.  
    7.     [Tooltip("The main NetworkManager you have in your scene")]
    8.     public NetworkManager manager;
    9.     [Tooltip("The panel the player sees before they are in-game")]
    10.     public GameObject startPanel;
    11.     [Tooltip("The panel the player sees whilst in the game (usually a stop button and some information)")]
    12.     public GameObject inGamePanel;
    13.  
    14.     void Update()
    15.     {
    16.         if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
    17.         {
    18.             startPanel.SetActive(true);
    19.  
    20.             inGamePanel.SetActive(false);
    21.         }
    22.  
    23.         if (NetworkServer.active || NetworkClient.active)
    24.         {
    25.             startPanel.SetActive(false);
    26.  
    27.             inGamePanel.SetActive(true);
    28.         }
    29.     }
    30. }
    ^Another script I made for you to enable and disable UI panels, so it isn't as cluttered!

    If you like these it would be awesome if I can get some feedback on them. I'm a student learning coding for a hobby and potential career.
     
    Mikejmc likes this.
  6. Harald_Heide

    Harald_Heide

    Joined:
    Jul 22, 2015
    Posts:
    81
    This is a very nice start with networking i think might refer to the new beta standard asset network using the new UI as lobbymanager as well
     
  7. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    I did this tutorial this morning. Itd very good. Nice job. The only thing that needs rework is the last page. I could not find the package to import for your lobby. Still the people at photon have much better tutorials, but this is one of the better tutorials I have seen from unity. I hate tutorials where they just give you the code and your left to reverse engineer it.
     
  8. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    100
    Would love a javascript version
     
    infinitypbr likes this.
  9. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    I wish you would write something like this but for setting up match making service for a game
     
  10. Erik-Juhl

    Erik-Juhl

    Unity Technologies

    Joined:
    Jul 11, 2012
    Posts:
    59
    Are you asking for a Getting Started with Matchmaking and Relay from the Editor components perspective or from the API perspective?
     
  11. olonge

    olonge

    Joined:
    Sep 22, 2014
    Posts:
    16
    Thanks for this.
    Looking forward to the lobby updates.

    Secondly, a lot of the example on the web, including this one are for FPS, but what if I'm writing a board game where each player (and there can be more than 2), gets a turn to select their own pawns and select which tile they should move to.
    Not sure if a concept like that could be touched upon in the docs.
    I'm guessing that the pawns should be created on the server only and spawned, then hit detection made from the client, but acknowledged on the server.
    Hence, the pawns should have a network identity and network transport component.

    Anyway, Thanks again, and eagerly awaiting the updates
     
  12. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Not assuming anything, but for both, you can cover all bases. Unless you have other thoughts that the "Manual" has already covered?
     
  13. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    I'm not completely sure I know what you are asking. But I think the answer is from the editor. Maybe this can better explain it. The cube shooting game, if you could finish that off by doing step by step lobby and Match Making, that would be awesome. And include how to create a matchmaking project Id.
     
  14. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Thanks, this is a large improvement. Just lay it all out to us with examples.
     
  15. Erik-Juhl

    Erik-Juhl

    Unity Technologies

    Joined:
    Jul 11, 2012
    Posts:
    59
    Yes, that was the intention. The end of the doc should be thought of as placeholder for this content. We wanted to get a rough draft out sooner to get your feedback in order to make that last bit more to your liking. A new version will be available as soon as we can.
     
    hippocoder likes this.
  16. blockimperium

    blockimperium

    Joined:
    Jan 21, 2008
    Posts:
    452
    Before I start asking a ton of questions over the course of this thread, let me say THANK YOU for making this.

    Okay, got a bunch of questions on this subject and a lot of it is really because much of uNet doesn't answer the "why" questions. :)

    Some understanding/explanation of the NetworkTransform bits would be very useful including why one would want to use it. Many of the other tutorials out there pretty much ignore the NetworkTransform because its not clear what all the bits and switches are and subsequently just implement a series of SyncVar to sync transform state around. Similarly within the documentation, it suggests doing things like setting the sendInterval to 0 (for bullets) and using SetDirtyBits() to cause the updates to be sent. Why would I choose one over the other?

    Why would I set the snapThreshold and what are some good values for it?
     
  17. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    ^ this, extrapolated across UNet.
     
    chiapet1021 likes this.
  18. Acid-NN-9

    Acid-NN-9

    Joined:
    Apr 29, 2015
    Posts:
    10
    Thank you for this good material!

    The only thing I miss is the explanation of how the animations work in unet
     
  19. Mikejmc

    Mikejmc

    Joined:
    Mar 2, 2015
    Posts:
    21
    Good stuff, thank you guys. Although it will be a starter guide it would be good to see a matchmaking setup that includes a custom UI demonstrating a few overrides, as I imagine that will be the direction most will be heading.
     
  20. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    I'm currently picking apart the uNet asset on the asset store that comes with pong as a demo.

    It's kinda hard to pick apart because there's several areas / scripts that hard wire it to 2 players only. As soon as you want more than 2 players it's a lot of work to make sense of where things are going wrong (using a byte value for player numbers was a bit inflexible to put it mildly)

    So a comprehensive doc is very very much appreciated!
     
  21. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Page 13:
    This only makes bullets fire for the Host (any player), any connected clients don't see the networked bullets they are firing. Unity 5.2.1f1
     
  22. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    1. Check to see if the bullet prefab is registered.
    2. Check to see if it's spawned on Server (host) side, aka, the spawning occurs in the [Server] only functions.
     
  23. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    1. registered.
    2. Bullets only spawn in host, and don't display or get spawned in clients.
     
    Last edited: Dec 6, 2015
  24. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Try NetworkServer.SpawnWithClientAuthority().
     
    enhawk likes this.
  25. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    ah needed to add NetworkServer.Spawn(bullet) properly, bit of a gotcha there!
     
  26. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    I'm finding that following this tutorial to the letter, sometimes when you start up a server and players join, the player object will get stuck where it spawned for some clients, other clients it's moving around fine, the bullets will move around and spawn fine. On both Online and localhost. Results are different every time, but happens all the time.

    If there is some way of checking that the player is moving around and make it move around for everyone, it may help add stability to this demo. Is this kind of stalling of player objects due to processor speeds?

    Or maybe Lobby is the only way to really synchronize this? If everyone is joining at the same time?
     
  27. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Hmm,

    Code (CSharp):
    1. [SyncVar]
    2.     public int health = maxHealth;
    I get "UNetWeaver error: SyncVar [System.Int32 Combat::health] cannot be static."

    Code (CSharp):
    1. // Draw health bar amount
    2.         GUI.color = Color.green;
    3.         GUI.backgroundColor = Color.green;
    4.         GUI.Box (new Rect (pos.x - 25, Screen.height - pos.y + 21, [U]Combat.health[/U] / 2, 5), ".", healthStyle);
    How do i fix this in UNET?
     
  28. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    1. Your health should be inside a class. Anything outside of a class is considered static.

    2. To get health, you need to obtain a reference to your class containing the Health variable. Then you access the class member from the referenced class.

    For more info on how to use C#, check out a few StackOverflow questions.
     
  29. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    it's not directly related to this tutorial but, when running several clients on the same machine and switiching between them, the player object will freeze up on some clients.

    Is there a way to force this from not happening? dirty is fine.
     
  30. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    FYI, for the Multiple Players section I had to set 'Local Player Authority' in the Network Identity script on the player game object in order for the host to see the client move (Unity 5.3).
     
  31. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    There's a change made in 5.2.3f1 where if the Unity Editor loses focus, the whole Unity Editor will pause whatever you are playing until it regains focus. This is happening on my end, as well as 5.3.0f4, so I don't know if that is what you're experiencing.
     
  32. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    You can fix that by checking "run in background" on your network manager object.
     
  33. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I have this enabled already, I think it is something else. It's not the network manager not running in the background that is causing it.

    When the Unity editor loses focus, it pauses all game objects. This includes game objects where you put your network manager script onto. Your network manager is keeping the connection open between the server and the client, but because all the updates are not happening, there is no data going through the connections.

    That is what I am seeing when I'm doing local LAN testing.
     
  34. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Can you add something about syncing physics over unet. Like client trying to push a ball. Right now it seems to stick and I can not find much info on it.

    Thanks
     
  35. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    The Lobby asset now has an asteroids game which uses physics, might be worth checking that out.
     
    FreeGameDev likes this.
  36. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Checking it out. Thanks for the reply.

    [Edit]
    Hmm, blue player still has some problems with physics. But it is a step in the right direction.
    On the client the blue player can travel right through asteroids and the blue player position is off from the host at times, but it is definitely closer than anything I have seen so far. At least I can get an idea of how it is done. Who would have thought a network game where you kick a ball around would be so difficult. :)

    Thanks, very, very much!
    You have no idea how much I appreciate your reply.
     
    Last edited: Dec 12, 2015
  37. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
  38. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    There's just too many changes going on, that they couldn't keep up with pace. The APIs are changing so often, I would be guessing these manual articles will update when 5.4 or 5.5 is released next year.
     
  39. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    It is very nice to see that you actively work on improving the UNET documentation.
    I would like to suggest an addition to the docs which is rather small but significant:

    In the docs, the general object creation flow is described, including networked spawning and object destruction for non-player objects. In addition, the spawn flow is described for player objects which are special networked objects. However, the list for the player objects does NOT include the flow of callbacks when a client disconnects and a player object is removed. This should be added to the list as it is very important to know for any custom NetworkManager implementations.
     
    Last edited: Dec 15, 2015
    isidro02139, asperatology and seanr like this.
  40. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Sound constructive feedback. I never played with custom NetworkManager implementations before, so I couldn't give much. Hopefully, your feedback guarantees a response.
     
  41. kleanthis

    kleanthis

    Joined:
    Nov 24, 2013
    Posts:
    2
    Very good tutorial thank you.
    One request:
    Can you please explain the best/easiest way to connect two different projects or two different scenes with unet? For example on the host pc will have the game and on client mobile devices to send triggers (e.g., controlling avatar animations etc.)
     
  42. Erik-Juhl

    Erik-Juhl

    Unity Technologies

    Joined:
    Jul 11, 2012
    Posts:
    59
    Funny, we have been discussing improvements to this over the past month with Joachim. When we have some experimental stuff to play with we will post it here, like we always have.
     
    isidro02139 likes this.
  43. Flx24

    Flx24

    Joined:
    Sep 19, 2015
    Posts:
    6
    Excellent!
    One little thing: players still respawn on vector zero?
     
  44. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    Something has changed in unity. I see this behavior in my game and I see it in your demo. At the bottom of page 17, this statement is incorrect.

    Save the project  Build and Run the game and see health bar on the player  If a player shoots another player now, the health goes down on that particular client, but not on other clients.
    Player State (Networked Health)


    This is your section where you add the NON - networked health, but the health acts networked, I see the health change on both clients. On my game, last night I added a treasure chest. When you bump into it; it plays the open animation and stays open. I have a variable "isOpen" I only play the open animation if its false (the chest is not already open). So, on one client I bumped into it and watched it open and to my surprise it also animated on the other client and the public variable was in sync. Yet there is not network indentity on the chest, no network transform and no sync variables. The only thing about the chest is it is not spawned, it's in place at design time and doesnt need to be spawned at run time.
     
  45. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    For all graphics use the same style with light background.
     
  46. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I also would suggest that you have two versions: One using the standard and one doing it all in code.
     
  47. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I would as well. However, since I want to learn C# (as Unity wants us to do...), I'd prefer the javascript version to be more of a translating-guide. So that while the end result will be Javascript, the process will help us learn some of the C#, which would make it easier for us in the future to pick up more and finally switch over.
     
  48. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    Eric,
    When are you going to finish this tutorial?
     
    Chom1czek likes this.
  49. ckrin

    ckrin

    Joined:
    Oct 8, 2013
    Posts:
    36
    please continue writing this document!
    looks like it'll be the recommended start for unet beginners in the future.
     
  50. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    You got to actually "tag" Erik like so in order to get his attention:

    @Erik Juhl

    But yeah, I'm also curious to know.
     
    Steve-Merritt and isidro02139 like this.