Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ultimate Unity Networking project - Add multiplayer to your game today!

Discussion in 'Multiplayer' started by MikeHergaarden, Jan 25, 2011.

  1. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027


    This forum post discusses my Unity asset store package, the "Ultimate Unity Networking Project" which is available on the asset store.

    Download it here

    The project introduces you to networking in Unity and requires no previous networking knowledge. Basic Unity and scripting knowledge is assumed. Next to being a learning resource, the project also contains resources to make implementing networking in your games easier.

    Feel free to add your feedback/suggestions/requests here

    Project overview:
    Actual Unity scenes in the project.
    Documentation
    The key of this project is the pdf documentation, it's contents:


    New features compared to the "Unity Networking: the Zero to Hero guide"
    The zero to hero guide formed the solid basis for this project. The zero to hero guide is my successful 2.X Unity networking tutorial which was released for free August 2009. Besides some general polishment, this what's new compared to the 2.X guide:

    - The project is now 3.X compatible.
    - The project is still in JS, but every file is available in C# as well.
    - new Tutorial: Tut #4, Manually instantiate networkViewIDs
    - new Example 5: Automatic matchmaking in-game (no usage of GUI, see Crashdrive 3D)
    - My custom MultiplayerFunctions class is included. Basically a network wrapper. Contains several timesaving functions/tricks.
    - Better/New documentation on: Masterserver, NAT/Connection troubleshooting, performance/LAG, load data before connecting, tips.
    - The package is maintained for Unity 3.
     
    Last edited: Feb 11, 2011
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Awesome! Hope it gets approved pronto :) I got my asset store account activated finally.

    Does the NAT punchthrough in your new masterserver example work properly out of the box? That seems to be the biggest hurdle that everyone seems to have difficulty overcoming.
     
  3. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Problem with connecting/NAT punctrough come forth from: 1. Unity itself, 2. the master server, 3. your own code implementation, 4.the routers of host and client.
    The main evils are 1,2 and 4.

    Unity 3.1 has some networking bugs so using this project on 3.1 will still not work perfectly, but Unity will have this covered:). I have added my custom network class which wraps the StartServer and Connect functions, these do the connection testing, master server setup and NAT stuff for you properly. So with this package you won't have to care about NAT/connection issues any more: If it still doesn't work(Unity bug/Router issue) it's out of our reach. And I'm keeping the package up to date for you.

    So the short answer is: Yes this package could be an improvement for your connection issues as it takes it out of your hands. But we need to wait for the Unity bugfix for a big improvement.
     
    Last edited: Jan 25, 2011
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    If it make it work anywhere near as good as your master server implementation in CrashDrive3D, I'm sure it will be great :)
     
  5. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    It should be equal/better than Crashdrive and very similar to Cubelands. But as I said, an upcoming bugfix will be of most help for connectivity.
     
  6. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Live on the asset store, that quick!
     
  7. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Wow, that was fast!
     
  8. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Leepo,
    I just finished working through your Zero to Hero guide and it was very enlightening. Great work and Thank You! I am glad your 3.x just came out and am looking forward to using your work and helping you earn the rewards you deserve (money). However, I am new to networking and I am left wondering how I get my game to move from my computers to my house's LAN (that's within our router, right?) and then out into the world. Will you cover that?

    Also, I'm confused by Unity's Master Server idea. I would like my game to be accessible through a controlled Lobby of my own design on my own server. Can I do that with "Built in Unity Networking"? or do I need Smartfox or similar? If I use Smartfox, do the scripts in your tutorials still work, or do I have to learn other things.

    Will these points be covered in your Tutorial or do you have links where I can find the answers I need?

    Thanks,
    V
     
  9. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Purchased and Downloading!


    (... and I still need help with those questions I asked!)
     
  10. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Most isp's will frown heavily on running any sort of permanent server from your account. When they notice the traffic your service will be terminated right away. Not a problem if your game isn't too popular, but if it takes off... Read your customer agreement.

    There's also the factor of your available upstream bandwidth. It might not be enough to support more than 8-10 users depending on your game.

    You can do that with a Unity client in dedicated server mode, but there will be a limit to the scalability. Go read some of the old posts by Jashan in regards to his tron game for metrics and his experiences. He was able to get 70 users or so, iirc, out of one Unity based server. That's an action oriented game.

    As always your mileage will vary, according to the constraints of your own game and it's traffic/load on the server. You can ease things a bit by having a slimed down server, running different code, by using compiler directives to excise most of the client stuff and vice versa.

    If you want a truly scalable server, then you will need to go with Photon/Electroserver or Smartfox, depending on whether you're running an action game or a turn based card game (UDP vs TCP). Either way, even with Unity networking, you will need a dedicated server or a VPS to run the server instance and/or a master/lobby server. Your home LAN isn't going to cut the mustard.

    No they likely aren't covered as they're outside the scope of his document. Setting up the master server yes, photon/smartfox no.
     
  11. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Purchased, and downloaded :) good stuff! Love the new master server example with the wrapper.
     
  12. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    @Legend411: Great, I'm curious what you're working on.

    Here some additional comments next to Quietus' reply :).

    Thanks!

    I do have half a page covering connection issues in the document. Furthermore if you are using my MultiplayerFunctions script to connect and use the master server stuff, then you can;t be doing anything wrong on this area: I keep the class up2date to ensure maximum connectivity. However there currently some bugs regarding NAT/connection success; these will be fixed by Unity soon.

    An RTS like lobby is in the examples. However you probably want more customization? Do note that you can use the master servers comment field to store a lot of custom data/properties (Server is Dedicated, has custom textures, is deathmatch etc.). I do this in Cubelands.
    You can modify the masterserver (source code is available) and host it on your own server. But you can not customize the Unity end regarding registering and fetching the host list so you can't extend that on the server either. In cubelands I register servers to the master list and use this to connect. But to easily enable a server list on my website I also have server register to my custom php/mysql webend (see here).
    You could take that a bit further and fully integrate your own server lobby data in the game via php/mysql.


    Smartfox/Photon or any other networking library all work a bit different and are not covered in this tutorial. I only have a list of networking alternatives naming them. I do plan to use Photon some time and write a similar tutorial for it, but that won't be any time soon just yet.

    Let me know if you still have any open questions after Quietus and my replies.
     
    Last edited: Jan 26, 2011
  13. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    I'm working on a First Person Shooter and I'd like to get networking implemented. I come from a design background, so my knowledge of scripting is limited and I have absolutely no networking knowledge. I was reading through your Documentation overview, and I noticed that one of your examples is a FPS game. What does that entail exactly? Will I be able to use that as a foundation to my game? Was that specifically written to allow testing on one machine, or will I be able to get that working over a LAN with two computers without much trouble?

    Thanks!
     
  14. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Thank you Quietus and Leepo for your patient and detailed (and prompt!) replies.

    After going through this Tutorial, I have found that the Masterserver Example 2 works beautifully, straight out of the box. I put a build up on my web site, connected to a game from two different computers in my house, then had a friend across town connect and that worked also.

    I am up and running the very next day after I started going through your tutorial!
    I started with the 2.x version. It's just dumb luck that you released 3.x the very next day!

    Beautiful work, Leepo. You are a gifted programmer, writer, and teacher.
    Thank you.
     
  15. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I know, right? It's freakin' sweet.
     
  16. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Question Leepo: I noticed you referenced this article when referring to unity fixing issues with the master server, but I don't see anything mentioned about networking in that post.
     
  17. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    @ Vimalakirti
    @ legend411,

    Maybe you guys could give me some insight on the question I asked above?
     
  18. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Oh, sorry, missed that somehow.

    You could absolutely use his FPS example as a jump-off point for developing your own multiplayer FPS. Out of the box it already handles connecting to the public unity master server, creating a server browser gui, establishing a connection between clients and hosts, loading the subsequent game scene, spawning player prefabs for connected players, and some basic examples of network synced movement and shooting systems to get you started.

    It will work over the internet, over your lan, or you could even connect to yourself by building out a standalone, hosting a game in the editor and connecting to it from the standalone via your loopback address (127.0.0.1). That's actually the best way to do all your debugging, as it will operate exactly as it would over a network.
     
  19. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Unless you are testing for latency, lol. ping = 1.
     
  20. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Well yeah, I just meant like making sure all your syncing, RPC's and stuff are working how you want and checking how many bytes per second you're transmitting... real world latency and whatnot you've gotta get some friends online :)
     
  21. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Adam,
    I would suggest working more on your FPS game play first, since that will help you learn more scripting, and networking is all scripting work. On the other hand, if you go ahead and buy this tutorial now, then you'll have it, and you can start altering the FPS example until it's your own and you'll have the networking done too. If you're confident that you will continue with the development of your game and the 60 bucks won't hurt you, then go ahead and get it. But if you're still just experimenting to see if you want to commit, then wait cause you can buy it any time.

    I'm afraid I'm not very much help.
     
  22. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    You will be able to get it working out of the box right away as the projects examples are ready to go right away. However to really customize it to your specific needs you'll have to learn some scripting. But you would need to learn scripting to make the actual game(play) anyway. The FPS example has multiplayered movement/shooting a deaths/kills scoreboard. Picking up ammo and two pickups. You could try tweaking this example. This project does assume you have scripting knowledge so you'll have to learn the actual scripting via other sources (Maybe try my free "C# game examples" project to learn C# coding easily).
     
  23. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    Thanks everyone.

    I understand that I'll need to learn scripting which isn't going to be a problem, I was only looking for a good starting foundation when it comes to multiplayer. I was looking at UDK and Torque 3D since they have a strong multiplayer foundation out-of-the-box, but I decided to use Unity for their strong community since my endeavor is solo for now. My plan is to "rough in" game-play; networking, functionality, features, etc. and then get to the asset development later since that's my specialty. Anyways I look forward to picking it up!
     
  24. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    "and connecting to it from the standalone via your loopback address (127.0.0.1)"
    Can you explain this a little more please?
    Thanks
     
    Last edited: Jan 28, 2011
  25. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Where was that stated? I'll also update the description there.

    Your own computer always has the localhost address (127.0.0.1) Wikipedia.
    But yeah ignore the confusing "loopback" stuff. Next to your normal LAN IP you can always use localhost or 127.0.0.1 to point to your own computer, even if there is no connection.
     
  26. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    Thanks, it was stated in legend411 post.
     
  27. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Sorry, didn't mean to confuse you. Loopback address is just another way of saying localhost, its a network interface that allows a computer to send network messages to itself, so you can test things.
     
  28. TheRedTape

    TheRedTape

    Joined:
    Apr 13, 2009
    Posts:
    73
    Hi! Just bought this to help me with my final exam project :-D (Game Design Development MBO Alfa College in Groningen)

    Just one quick question, in a lot of scenes there is a object called "Persistent" that has a "MultiplayerFunctions" script attached and also one Mono Behaviour that is missing a script. What's up with the missing script? :)

    Thanks!
     
    Last edited: Jan 30, 2011
  29. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I noticed that too... whats up with that?
     
  30. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    I have the same question as legend411.
    I have another question for Leepo, do you think this is suitable for an RTS ? I just need a peer-to-peer connections, a lobby, etc., and offcourse, all the RTS units.
    Thanks
     
    Last edited: Jan 30, 2011
  31. luisanton

    luisanton

    Joined:
    Aug 25, 2009
    Posts:
    325
    I'm pretty excited with all this, because I need a good solution for a RTS multiplayer game. But I still have a big doubt: where to host the master server. Is Google App Engine, for example, a good solution for a non-dedicated host (that is, simply for connecting players)? How much do you pay for your hosting services?
     
  32. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
  33. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Sorry for the lack of reply, I was at a GGJ and was very busy there :p.

    Cool, good luck with that project, I'm curious to your end result. Please let me know when it's finished :).


    @BigB, Legend, TheRedTape: (Persistent missing script)

    Just submitted a minor update to fix that missing script issue:
    "Small bugfix in three example scenes, the "persistent" object missed the MakePersistent script"

    The missing script was the MakePersistent.js. This script simply marks its gameobject as DontDestroyOnLoad to make it persistent: We need it to easily ensure the MultiplayerFunctions.cs is available in all scenes.
    If you first load the main menu, it's "persistent" object (has MP functions + makepersistent attached) will be made persistent, therefore when you load the game scene this gameobject will still be around.

    If you run the game scene directly from the editor the MultiplayerFunctions.cs needs to be present in the game scene. Thats why I -also- added the Multiplayer Functions to the game scenes now.This will not cause any problems. The MultiplayerFunctions script makes sure there's max. only one instance of itself around at all times.

    This small update should be available today/tomorrow I guess.


    @LuisAnton
    The Unity networking + masterserver works great. But whether you will want to switch to a different networking solution (and master server) depends on your own custom/specific needs. Unity can handle an RTS game, but what are your needs? Do you -need- to ensure 100% connectivity? Then you'd need your own dedicated servers running gameservers (e.g. Photon).
    If you want to save costs and are fine with a bit less connectivity you can go for Unity.
    I could see myself using Unity for an RTS game.
     
    Last edited: Jan 31, 2011
  34. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    I have purchased and gone through the tutorial, as well as reading everything I can on the forums but I still have a few things I don't understand.

    First, I don't see a good simple yet complete example of how to NOT use Network.Instantiate. All the tutorials use this, and then the guide says don't use this. Most of the forum posts say not to use it as well, though never really explaining why. When I use it I get working networked objects, but nothing deletes on the server when clients disconnect, so I'm assuming thats the problem with it, but I don't know.

    Has anyone put together a step by step guide of network code only to explain how to do start using the non Network.Instantiate method? I tried remaking the steps from the tutorials without it but my client fails when it receives the RPC I am calling to instantiate my prefab client side because the prefab has no networkview ID yet. (At least I think that is what the error means)

    Basically this is what I am doing, but I must be missing a step client side. (skipping all non network code to make this simple)

    //SERVER
    Network.InitializeServer(32,connectPort,false);

    //CLIENT
    Network.Connect(connectToIP, connectPort);

    //SERVER
    void OnPlayerConnected(NetworkPlayer player)
    {
    NetworkViewID viewID = Network.AllocateViewID();
    //call instantiate on my prefab and add it to a list of players serverside (not Network.Instantiate)
    networkView.RPC("SpawnPlayerPrefab", RPCMode.OthersBuffered, viewID, transform.position); //create the ship on all the clients manually because not using Network.Instantiate
    }

    //CLIENT
    //spits out debug.log receive RPC debug message but my SpawnPlayerPrefab RPC is never called
    //instead I get the following 2 errors;
    View ID SceneID: 1 Level Prefix: 0 not found during lookup. Strange behaviour may occur
    Could't invoke RPC function 'SpawnPlayerPrefab' because the networkView 'SceneID: 1 Level Prefix: 0' doesn't exist

    ---
    It seems I need to create the network view client side first somehow, but I don't know how else to tell the client about the network view ID the server just created other then the RPC I am using now, and I don't know where else I would assign this view id to until I instantiate the prefab on the client which contains the network view for that object.

    Thanks!
     
  35. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    This is probably the key problem:
    You do need at least one networkview to be able to pass and assign the allocated networkview ID. A networkview ID is automatically assigned when:
    1) Using Network.Instantiate (but I indeed disencourage using this)
    2) Adding a networkView to a gameobject in your scene (e.g. on your own GameSetup/GameManager object)

    Your problem is that you are sending an RPC message on a gameobject that has no networkview attached..or it only has a networkview without a networkviewID assigned. You should add an gameobject to your scene with a networkview and your GameManager.cs script(or something along those lines). Then have that script spawn the players and use its networkview to set the networkView IDs.

    This is showcased in Examlple 5 only. I indeed do not have a clear tutorial on this yet(!), but I will add it as Tutorial 4 :)!
    I've started working on it right away.
     
    Last edited: Feb 2, 2011
  36. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    I added a line to my network manager object that is called in the void OnConnectedToServer() method
    m_MyViewID = Network.AllocateViewID();

    This still gets the same errors when I send the RPC.

    I am not exactly sure I am understanding what you are suggesting. What is the sequence you feel is best after a player connects to the server? I would imagine you then want the server to send a RPC to all clients to add another prefab to the game so the server would create the new network view ID, and pass this along and then when the clients get the RPC, then the clients instantiate the prefab and assign the network view ID which is what I believe I am doing here.

    Thanks for the guide and the help BTW! :)
     
    Last edited: Feb 2, 2011
  37. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    If you can wait I might be able to finish the new "tuturoial 4" tomorrow and the update *could* be available on the asset store the same day.
     
  38. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    Awesome! :)
     
  39. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I'm about to finish tutorial 4 and upload the update to the asset store.
    Here's an PM I received I want to share:

    While I could add the resources to tell you how to synch a texture or a physics object..it always depends on your custom needs. You should never synch more then required and the requirements vary per project. What's more important is to get everyone to understand (1) what needs to be synched and (2) how that can be synched. It is vital to gain more understanding of the lowest-level data thats required to be networked.

    Physics can be a pain in multiplayer. How authoritative does it need to be? How accurate? If it doesn't affect gameplay; generally, don't even bother to synch phsyics. What physics properties matter for the game? When networking physics, make sure to not only network the position/rotation but also the rigidbodies velocity and angular velocity(!).
    Textures: Either send an int with a custom defined reference to a certain texture. If it's a user-customized texture you can send the image data in a byte[] (and you might want to zip that).
     
  40. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    @Scorch and everyone else
    I have just submitted an update to the asset store.
    The new Tutorial 4 is added with a clear and cut down example of manual instantiation of networkView IDs.
     
  41. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    Thanks for doing that. Store just has up to 3, might take a few hours for the new one to show up. I'll let you now how it goes.
     
  42. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Question: is it REALLY that important to be manually allocating networkView id's, if you have a well-designed setup for cleaning up player objects and RPC's with groups?
     
  43. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Nah it isn't but I think it's good practice. I made my first few games using Network.Instantiate as well and never had big problems
     
  44. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    What would you consider good practice for cleaning up RPC's? For example, when I call Network.RemoveRPCs(player); unity says there is nothing to delete but when a new player joins they get the old player as if they are still on server, even though server is no longer simulating them. (this is using Network.Instantiate still)
     
  45. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Hm yeah thats one of those annoying bit why I recommend manual allocation. RemoveRPC does not yet work on network.instantiation calls. Maybe Network.Destroy or Network.DestroyPlayerObjects works?
     
  46. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    I call all 3 :) And server side it seems right, but when a new client connects a second object shows up even though it does not exist on the server, at least as an object. It must still be hiding somewhere in the Unity Network code though.

    Well, hopefully when your example shows up in store, I can switch from Network.instantiate and simply rip through your player list any time a new player joins to get them all the players.

    It seems Network.instantiate would work great for something like a race game where you create all the objects at first and then delete the scene when the race is over vs a shooter where you are creating and destroying thousands of bullets, players, etc during one fight.
     
  47. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Yeah, that must be the exact reason for why I forced myself to never use Instantiate again.
    I did file a feature(/bug?) request to be able to delete Network.Instantiation buffered calls a while back :)
     
  48. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    When i want to clean up Network.Instantiated objects, I just have them organized in different groups, and periodically use RemoveRPCsInGroup when necessary. As for Network.Destroying them, since Network.Destroy does not get buffered, I just put the Network.Destroy call inside an RPC function called NetworkDestroy() and buffer that RPC call.
     
  49. Scorch

    Scorch

    Joined:
    Jan 12, 2011
    Posts:
    20
    Lengend411, if there has been thousands of add/deletes before a player connects will there be thousands of buffered RPC calls that they must go through first?
     
  50. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Well if you're using Network.Instantiate for projectiles or something, I would put their Network.Instantiates and NetworkDestroy() buffered RPC's in a group I would deem the "projectile" group, and use RemoveRPCsInGroup every couple seconds.