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

UniKnowledge entry: Unity Networking the Zero to Hero guide

Discussion in 'Multiplayer' started by MikeHergaarden, Aug 23, 2009.

  1. Dano

    Dano

    Joined:
    Nov 7, 2009
    Posts:
    39
    I think your document is an amazing resource, I have only gone over it topline, but it has helped me get a rough understanding of what is possible.

    Just two very quick questions,

    I've only followed the instructions in the tutorial on my local machine, when I create a project and host it on my webspace do I have to make changes to code and settings etc?

    Sorry for a lame question, but I'm not a network person...

    Secondly, would it be possible to add a player name flag to each of the sporned peopel that join, that would appear less confussing when lots of people had joined.

    I really only plan to use this for a online chat room.

    Thanks
    Dan
     
  2. Peterbjornx

    Peterbjornx

    Joined:
    Nov 8, 2009
    Posts:
    5
    Maybe you could add "Write your own server" to the list at the end, this is what im doing
     
  3. Dano

    Dano

    Joined:
    Nov 7, 2009
    Posts:
    39
    Thanks, could you expand on what you mean? do you have an example?
     
  4. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    No, you don't need to make changes. Running the webplayer from a website is the exact same thing as running a standalone application on your own computer (from a networking perspective).


    Of course, all up to you. Use an RPC call to set the right name on the new prefab you instantiated on the network.
     
  5. Dano

    Dano

    Joined:
    Nov 7, 2009
    Posts:
    39
    Leepo, thanks. Thanks also for the useful document that I'm getting stuck into.

    I just have one other queston you might be able to help with...

    I'm trying to work out the correct camera to use in my project, as a user spornes into the game I want them to have a 1st person view of the scene, but see other players (As objects) as well. I don't seem to be able to get the right camera.

    Do I need a camera on the prefab sporne ogject or a certain type of scene camera?

    Thank you

    Dan
     
  6. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Spawns ;).

    This isn't networking related but rather gameplay logic/design.

    I'd use a level camera, and attach cameras to every prefab. Disable the level camera as soon as an user spawns his object. And only enable an objects camera if that object is yours.

    Please continue any further questions in another topic so that your questions get more attention, and to keep this thread on it's own track.
     
  7. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    I don't know how I missed this. Really good work. Especially at the end, where you discussed limitations and possible workarounds of Unity's built in networking you personally experienced.

    Here's hoping you continue and update the tutorial, perhaps expanding on some of the more intermediate level topics you discussed.

    Bump for the tutorial of the year.
     
  8. Dano

    Dano

    Joined:
    Nov 7, 2009
    Posts:
    39
    Does this project let me connect up to friends computers, or is it only going to work from instances on my own pc?

    eg, can i create the project, but it onto my website and invite friends to join a project?

    Thanks for your help.
     
  9. Deleted User

    Deleted User

    Guest

  10. gamenut30111

    gamenut30111

    Joined:
    Nov 7, 2009
    Posts:
    399
    hallelujah, unity's networking messiah has come :)
    this will help me a lot when i start making the multiplayer version of my fps.
     
  11. Diablo1399

    Diablo1399

    Joined:
    Jan 25, 2010
    Posts:
    5
    I have a query about tutorial 3: Authoritative server.

    Why does the Tutorial_3_Prefab have a Network View property? I though the server was supposed to send the position of the object through the OnSerializeNetworkView function. . . .or is this fn part of Network View??

    Thanks
     
  12. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    You always ened a networkview to Serialize or to send RPCs.
    "OnSerializeNetworkView" uses the attached networkView.
     
  13. Diablo1399

    Diablo1399

    Joined:
    Jan 25, 2010
    Posts:
    5
    Thanks for the helpful reply :)
     
  14. gameproducer

    gameproducer

    Joined:
    Jan 23, 2010
    Posts:
    25
    Very nice tutorial, although I'd have a basic question.

    On tutorial_2B_spawnscript, there's the Spawnplayer() function that gets called by the server, and the client (I assume).

    You mention that "Network.Instantiate" makes only the caller to be the author. In this case, to me it looks like the Spawnplayer is called by everybody - and everybody is the owner?

    (I do have previous network understanding experience in coding, but I'm very, very new to Unity. I checked the reference and Network.instantiate function but fail to understand why this works).
     
  15. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    this has been driving me nuts as well. i think i found the issue -- Network.Instantiate gives ownership, but ownership means little more than a title. you have to specify what that ownership entails. in the player script, you'll find stuff like this:

    Code (csharp):
    1.  
    2. function Awake(){
    3.     if(!networkView.isMine){
    4.         //We aren't the network owner, disable this script
    5.         //RPC's and OnSerializeNetworkView will STILL get trough!
    6.         enabled=false; 
    7.     }
    8. }
    9.  
    10. function Update () {
    11.  
    12. if(networkView.isMine){
    13.         //Only the owner can move the cube!        
    14.         var moveDirection : Vector3 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    15.         var speed : float = 5;
    16.         transform.Translate(speed * moveDirection * Time.deltaTime);
    17.     }
    18.    
    19. }
    i think this will make it to where only the owner controls their object. i haven't confirmed yet because my players have their own camera and i'm trying to get camera ownership fixed right now.

    edit: yeah that code above should work. i also got cameras figured out so i'm all good now :)

    here's my code that makes sure players can only see from their camera -- i think i'll be executing this every time a new camera is created (when another player spawns):

    Code (csharp):
    1. if(networkView.isMine){
    2.             //find all cams and disable
    3.             allCams = FindObjectsOfType (Camera);
    4.             for (var cam : Camera in allCams) {
    5.                 cam.enabled = false;
    6.             }
    7.             //turn on own cam
    8.             var myCam = gameObject.GetComponentInChildren (Camera);
    9.             myCam.enabled = true;
    10. }
     
  16. Rodrigo G.

    Rodrigo G.

    Joined:
    Dec 9, 2009
    Posts:
    7
    Nice Tutorial Leepo thank you.

    And thanks JTown, the script for the cameras was very useful. I had to add that for me the trick was ataching the camera directly on the object by selecting the object and inserting a camera via component/rendering/camera. Then modify the code a little because in this way it didn't work:

    Code (csharp):
    1.  
    2.          //turn on own cam
    3.          var myCamO = this.gameObject;
    4.          var myCam = myCamO.camera;
    5.          myCam.enabled = true;
    6.  
    7.  

    Thanks again...

    :wink:
     
  17. UVRadiation

    UVRadiation

    Joined:
    Jul 21, 2008
    Posts:
    183
    Awesome Tutorial!
    I found it real helpful in the GGJ to create a network game - Thanks!
    http://www.nitako.com/files/ObjectSpace/web.html

    Anyway now I want to add MasterServer and lobbies, I compiled the MasterServer - Lobbies example and I can create servers when I play the from my editor and the web player client can see it and connect.

    but when I register a MasterServer from the web-player, the editor or a seconds web player cant see it at all.

    You have any clue why?
     
  18. kiwi

    kiwi

    Joined:
    Nov 13, 2008
    Posts:
    11
    I just want to say a quick thanks for this - it has been incredibly useful. I appreciate you sharing it with us.
     
  19. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Hm if a editor server does show up for a webplayer client then the code should be fine.. It shouldn't be a firewall issue or so?
    Maybe try debugging the webplayer with the OnMasterServerEvent function:
    http://unity3d.com/support/documentation/ScriptReference/MasterServerEvent.html
     
  20. venice

    venice

    Joined:
    Oct 29, 2009
    Posts:
    3
    thanks for your great tutorial. i've enjoyed reading it. after that i wanted to test the tutorials, but when i start unity does nothing - neither GUI elements nor errors are shown (in unity itself and as standalone). only as webplayer text and buttons are shown, but i can't push the buttons.
    i'm using latest (free) unity version. has anyone know that problem to? could it be, converting the project failed without reporting any errors?

    thanks jan.
     
  21. Moredice_legacy

    Moredice_legacy

    Joined:
    Jan 31, 2010
    Posts:
    101
    Many thanks for a great tutorial, Leepo, this has also helped me with my rather fresh experience with Unity. Not a newcomer to coding, but there's one question I have (which I have not found an example to):

    I am using the authorative server example. In my project, the server is the instatiator (owner) of every game object spawned on request by a player entering the game environment. I have trouble trying to locate and/or change the code appropriately, that allows the server to move a player's object once a player sends input requests to the server. I know this sounds like something covered in your tut, but I have not found it - I can't figure out how the server "knows" which player object is sending the movement request, and how the server code for movement execution would look in this example?

    In other words, my server only runs the server script and has access to all the same assets as the clients, there is no local player on the server.
     
  22. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Thanks!

    I believe that's exactly what Tutorial 3 does?
    Clients can only send their movement input, the server owns everything and executes it.
     
  23. Moredice_legacy

    Moredice_legacy

    Joined:
    Jan 31, 2010
    Posts:
    101
    That's what I thought as well, but I simply can not break up the code and figure out what goes where. The tutorial scripts have both client and server elements in them mixed together.

    Here's one example:
    Code (csharp):
    1.  
    2. if (Network.isServer) {
    3.    var moveDirection : Vector3 = new Vector3(hInput, 0, vInput);
    4.    var speed : float = 5;
    5.    transform.Translate(speed * moveDirection * Time.deltaTime);
    6.     }
    7.  
    This is obviously server-side code, but it also says //Also enable this on the client itself: "|| Network.player==owner){", which means that this code should also, obviously, be in the client script - but, my question is, exactly where in the process does the server accept the movement request and actually move the player's piece? The way I read this, that code allows the client to move his object without the authorative server's approval?

    I've tested your code with three players, and obviously it works the way I'm asking, I simply have some trouble understanding the code structure here.

    Again, I'm only trying to understand the code (better). Copy-pasting without knowing/understanding what the different functions do and what's supposed to be server-side only and client-side is not my method, and it is unwise.
     
  24. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Nah, this part can be left away, i left the comment there to point out that if you also let the clients run their own input..this can help reduce laggy movement..as the clients do not need to wait for the server and can start predicting their movement.


    The server gets the movement via the RPC(SendMovementInput), and executes this in the piece you quoted (...//Server movement code if(Network.isServer){...).
    This movement is applied on the server side, and via OnSerializeNetworkView automatically send (back) to the clients.

    Again, this only happens if you ADD the commented piece of code (OR if client..)
     
  25. Moredice_legacy

    Moredice_legacy

    Joined:
    Jan 31, 2010
    Posts:
    101
    Right, OK! A lightbulb just got turned on above my head - sorry for the rather "dumb" questions, Unity is, again, new to me. That helps alot, thank you very much Leepo. :) I'll give it a go and see how it turns out.
     
  26. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15
    Hello. I'm having troube downloading the .zip file...

    apart from that, looks like a great resource. I look forward to working through it :)
     
  27. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15
    ignore that last post... I guess there were jsut gremlims on the net.. the zip is downloading fine now :)
     
  28. ashwan

    ashwan

    Joined:
    Mar 3, 2010
    Posts:
    21
    Your tutorial was really helpful. I am planning to develop a game based on the smartfox server in unity. I searched lot but i couldnt find any good tutorial based on smartfox unity except the Island demo and tic tac toe example . if you can try to provide a networking tutorial based on smartfox server it would be really helpful
     
  29. oem

    oem

    Joined:
    Jan 8, 2010
    Posts:
    12
    Thanks for this! I have no networking knowledge at all so this will be perfect.
     
  30. fogsight

    fogsight

    Joined:
    Apr 6, 2010
    Posts:
    65
    Great tutorial! It was invaluable for me in helping to understand unity networking and integrating it into my first game.

    One thing you should add though, if you get a chance to revise it. Being new to the whole concept of creating a multiplayer handling code I've had a difficulty understanding what Master Server is.

    It is not as apparent to the beginner how stuff works, and your tutorial was the first one that I've studied. And only after digging in the documents on the main site (I know RTFM) I've found out an answer, that Unity provides all purpose Master Server for our needs, and I don't have to rent a server for testing. :)

    Cheers, keep up the good work!
     
  31. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Agreed.

    Also, maybe make the master server example into a tutorial. It took me a few hours to understand how it works, instead of a few minutes with the tutorial.

    On the other hand, it is usually better to learn yourself, that way you will remember it more... Well, I still think a complete master server tutorial would be awesome.
     
  32. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Aubrey, you may remember me. My name is Grady Lorenzo. It is unfortunate that player attendance has dropped so low. Like I said, Mars Explorer is one of the best kept secrets on the internet. It has changed quite a bit since I started playing a year and a half ago, and many useful and fun things have been added in that time.

    Well done. I know you must be pleased with your work, and I know you still have many ideas in the making. Keep it coming!!
     
  33. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Excellent tutorial.

    [answered own question]
     
  34. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    I'm trying to augment tutorial 3 with the chat script, it works brilliantly but I'm having trouble figuring out how to pass the player name reliably from the connection screen.

    Edit: Sorted - had to pass it to playerpref via PlayerPrefs.SetString beforehand. (is this the 'correct' way?)
     
  35. nono946

    nono946

    Joined:
    Jun 22, 2010
    Posts:
    2
    okay well let me start off by saying thanks a ton for this awesome tutorial! i just have one question. i have set up a server using smart fox from a different tutorial and i set up a basic script that modifies the position of an item if you stand on it. however if lets say player 1 stands on it he sees the gun as being equipped (childed to him its position modified by a vector 3) while player 2 sees the gun still lieng on the ground X_X

    in other words it doesn't sink the position of the gun and i want it to. do you have any code or suggestions on how to do this :p all help is awesomely appreciated thanks again for this tut i actually ( very barely) understand networking now. woop woop (so far i have tried copying and pasting the
    lastPosition=transform.position;

    networkView.RPC("SetPosition", RPCMode.Others, transform.position);
    }


    @RPC
    function SetPosition(newPos : Vector3){
    //This RPC is in this case always called by the server,
    // but executed on all clients

    transform.position=newPos;
    }

    onto a script i then applied to the gun. no worky.
    thanks in advance!!
     
  36. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
    Hello everybody!
    Excuse me for my english; i read and undestand a lot, but i'm horrible writing :p

    I have to say it: the tutorial is awesome!... it's very helpful for beggier like me :D

    Thanks Leepo!

    Kokumo
     
  37. drazil austin

    drazil austin

    Joined:
    Dec 16, 2009
    Posts:
    236
    i am having problems connecting. i can connect fine if i leave the ip the way it is but i cant connect between 2 computers. if i try to set the ip to my ip it cant connect at all. are you suposed to be able to connect to your own ip? maybe i am just not understanding how connecting works. (and i cant get any of my friends to help me test it)
     
  38. macfinch

    macfinch

    Joined:
    Aug 24, 2010
    Posts:
    139
    Hey Leepo.
    I know this is a late post as I've just started on Unity, but great tutorial :D

    @all - I've downloaded the pdf no problem but im having trouble getting hold of the zip file. Could someone please post on their page / send to me through a file send site, it would be so so much appreciated :)

    My email is macfinch 'a t' hotmail .co .uk

    thanks to everyone else here, great forum.

    -M
     
  39. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Temporary fixed this for Unity 3. However the whole NAT stuff is messed up out of date. I'll rewrite the tutorial soon and add some great new functions and tools that are easily reusable in your own projects.
     
  40. Agrios

    Agrios

    Joined:
    Aug 21, 2009
    Posts:
    50
    Leepo,
    if you dont help us with networking, who will?
    I think we need new Masterserver and new Conntester as well right?
     
  41. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Hehe yeah, seems like I'm the unity networking ambassador ;). Yes 3.0 needs the new masterserver+connectiontester. You can use the unity MS by not filling in any custom masterserver IP.

    There seem to be some bugs with unhandled messages and connect(GUID) not working.. see: http://forum.unity3d.com/threads/61710-Unhandled-message-67-70-from-Network.Connect Connect(HOSTDATA) seems to be fine, but might not be using NAT.

    I'm contacting Unity about this (bug ticket, etc)
     
    Last edited: Sep 28, 2010
  42. kiwi

    kiwi

    Joined:
    Nov 13, 2008
    Posts:
    11
    Leepo I have an error that I feel stupid asking about as it's probably nothing to do with your tutorial - but maybe you can help anyway. I downloaded the examples and everything works great. I then went through and converted everything to C# and I'm getting stuck with this:

    void Start ()
    {
    // We are probably not the owner of this object: disable this script.
    // RPC's and OnSerializeNetworkView will STILL get trough!
    // The server ALWAYS run this script though

    if(Network.isClient){
    enabled=false; // disable this script (this enables Update());
    }
    }


    [RPC]
    void SetPlayer(NetworkPlayer player)
    {
    owner=player;
    if(player==Network.player){
    //Hey thats us! We can control this player: enable this script (this enables Update());
    enabled=true;
    }
    }

    In a nutshell, SetPlayer gets called *and* player==Network.player but for some reason the script is *never* enabled. Have you seen this before in C#? If I stick enabled=true into the OnSerializeNetworkView() function the script magically gets enabled but not in the SetPlayer RPC
     
  43. kiwi

    kiwi

    Joined:
    Nov 13, 2008
    Posts:
    11
    Well I never did get the "enable" to work, but it was pretty easy to code around anyway.
     
  44. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Bump~~Good
     
  45. agenda.

    agenda.

    Joined:
    May 8, 2010
    Posts:
    21
    cannot download pdf?

    "Free Unity resources/projects M2HCulling, Networking tutorial and more" link takes me to a site where i click pdf tut!
    then nothing?
     
  46. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Robur, I know a familiar issue with your game is the "cannot connect to remote server". Why not make a server with a static IP, that all players connect to. Theoretically, that issue should no longer happen...
     
  47. ohad11

    ohad11

    Joined:
    Nov 9, 2010
    Posts:
    2
    I was just working through your tutorial and noticed that in the example with the boxes moving around if one box pushes another they start to shake. Is this because the server is always fixing the position of the other box? How would can I change this so collisions work properly?

    Thanks for the help and the great tutorial!
     
  48. Mex

    Mex

    Joined:
    Nov 11, 2010
    Posts:
    5
    Hi !

    First of all, thanks for that awesome tutorial :)

    I only got a little question:
    When I build the project into a webplayer, and try it locally, all is working (I can test every example and all).

    But when I try to test with another guy (not in lan), I can see his room but I always failed to connect because of that Limited NAT Punch through Symmetric error.

    After a looong search, I understood that it was because I'm running it with the last Unity (v.3). So I wanted to know what can I do to make that work with a friend in order to test it and do my own project.


    Thanks a lot for you help :)
     
  49. dr-mad

    dr-mad

    Joined:
    Feb 15, 2010
    Posts:
    19
    please Leepo fix it for unity 3 because i made a project shooter out of your example and i just got unity 3 and now it dont work

    (dutch) heey kan je me helpen hoe ik het werkend krijg want ik blijf hangen bij de nat

    i like to see the shooter element in the next update because that was amazing i like to see it working again

    is there some way that i can set the nat to work again with the old project maybe change a line of code
    so it got a connection again please help me


    help me aub dank voor het lezen greets dr-mad
     
  50. Mex

    Mex

    Joined:
    Nov 11, 2010
    Posts:
    5
    Yes it seems to be a big problem with NAT Punchthrough and Unity 3, we always got that Limited NAT Punch through Symmetric error in the logs.

    I tried a lot of things, but nothing worked :/