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

Simple Multiplayer Game Tutorial

Discussion in 'Community Learning & Teaching' started by willgoldstone, Mar 7, 2016.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi all, we just shipped the first of many tutorials for networking, making a simple 2 player shooting game to help newcomers get to grips with the system. It's written too as I know many of you are keen for us to do more of that than video.

    http://unity3d.com/learn/tutorials/topics/multiplayer-networking

    Hope you like!

    Will
     
    yaezah, theANMATOR2b, MHUnity and 6 others like this.
  2. OldRod

    OldRod

    Joined:
    Mar 2, 2009
    Posts:
    136
    This is great, thank you!
     
    NirmalNishanth likes this.
  3. BombEthan

    BombEthan

    Joined:
    Mar 7, 2015
    Posts:
    2
    I have been following your tutorial so far and it is great; However, when I was on part 10 when I ran into my first error. I get the following error:
    InvalidCastException: Cannot cast from source type to destination type.
    PlayerController.Fire () (at Assets/PlayerController.cs:32)
    PlayerController.Update () (at Assets/PlayerController.cs:24).

    I also get this error when I don't comment out this part of the code--> NetworkServer.Spawn(bullet);
    Assets/PlayerController.cs(41,23): error CS1502: The best overloaded method match for `UnityEngine.Networking.NetworkServer.Spawn(UnityEngine.GameObject)' has some invalid arguments

    Assets/PlayerController.cs(41,23): error CS1503: Argument `#1' cannot convert `UnityEngine.Rigidbody' expression to type `UnityEngine.GameObject'

    I am guessing that I did something wrong but not sure where since I copy and pasted the whole finished chunk of code on part 10. Thanks for any help.
     
  4. Que

    Que

    Joined:
    Mar 14, 2014
    Posts:
    135
    Thank
    Nice. It's time to jump in to multiplaying.
     
  5. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    Setup a dropbox with your code in it so someone can take a look. For cast exceptions, it's going to be really hard to figure it out without seeing your code specifically.

    If you're not interested in dropbox - at least use the code tag and put your class contents into a message.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  7. BombEthan

    BombEthan

    Joined:
    Mar 7, 2015
    Posts:
    2
  8. Holyschytt

    Holyschytt

    Joined:
    Nov 3, 2012
    Posts:
    4
    OMG.... Please do something Other than NetworkManager Tutorial for crying outloud there aready 500 tutorial on Youtube of the same thing.. How about Client / Server Tutorial 2 seperate Scripts NetworkServer.cs NetworkClient.cs 2 different scenes Server Scene Client Scene..
     
    Last edited: Mar 21, 2016
  9. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Health bars don't update for new players, they are all full when a client joins (even though a player took some damage before). This is because the OnChangeHealth hook is not called initially for the client at start - only after that. You will have to override the OnStartClient method and call all hooks manually.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There was a typo in the lesson code and it has been corrected. Network spawn needs a GameObject not a Rigidbody.
     
  11. badawe

    badawe

    Joined:
    Jan 17, 2011
    Posts:
    297
    I need to agree with @Stillborn, every single tutorial out there is exactly the same, add NetworkManger, add NetworkManagerHUD, add Identity to the player and sync transform and is done. And this is far from what we need to use to actually create something nice with this. Will be really nice if we actually show, how to communicate properly with the players, server and ids.
     
    mcbauer likes this.
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    We will be rolling out more complex multiplayer lessons and assignments as we go. As much as I appreciate the fact that there are a number of 3rd party tutorials on the internet and YouTube, we cannot rely on these as official teaching solutions. Be aware, however, that most of our tutorials are going to revolve around the High Level API provided by Unity Networking.

    So, expect a few more very simple tutorials before we get more complex. We want to make sure that everyone has a chance to get started from the ground up using official tutorials.
     
    mcbauer likes this.
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ah - good point. I'll look into updating this and keeping the lesson as simple as possible.
     
  14. badawe

    badawe

    Joined:
    Jan 17, 2011
    Posts:
    297
    That is awesome Adam, really appreciate that!
    I add a few questions here: http://forum.unity3d.com/threads/how-inform-a-bullet-who-is-the-owner.394484/ a simple thing I can't get my head around it yet :S
     
  15. asae

    asae

    Joined:
    Feb 23, 2014
    Posts:
    2
    I can get the tutorial to work when running a build on the same computer as the Unity editor.
    However the problem I'm having is that when I run it on an Android device (Samsung S7 Edge) and Windows 10 computer. I'm running the server in the Unity editor and enter its IP address in the LAN Client box on the Android device. The Android device never connects to the Windows computer.
     
  16. fineman

    fineman

    Joined:
    Mar 23, 2012
    Posts:
    19
    Just worked through this - nice work, very good and introduces each of the fundamental concepts. One comment as a former creator of training material myself - if you are going to say (as you do with the health bar UI) "the following solution is less than optimal" it would be nice to explain why, or direct readers somewhere they can understand why it is not optimal :)
     
  17. fineman

    fineman

    Joined:
    Mar 23, 2012
    Posts:
    19
    I have just noticed that when I launch an instance of the completed tutorial application, even before I start the host (so only the network HUD is running) the Unity client is using 25% of CPU continually on a core i7 gaming laptop. This can't be right surely?
     
  18. pingboo

    pingboo

    Joined:
    Apr 3, 2016
    Posts:
    7
    I already did take a look on the given tutorial and have done it myself. I have run it successfully on the pc but I'm still wondering or really confused on how to do this on android devices? I've searched for possible ways but I can't seem to find a solution.

    Hope someone could further explain or at least say some steps on how could I achieve this.
     
  19. Culzean

    Culzean

    Joined:
    Jan 25, 2014
    Posts:
    48
    This is nice, I am trying to follow the tutorial in my own project but have run into a problem.

    When I test the local server out with two versions I have two players. But they are glued to the same spot. Is the network transform not working right? To reiterate, the player that joins the server, if I move him he is rubber banding to the original player, while if I move the original player the second one is getting dragged around. Any ideas what I have done wrong?

    blue for local, red for remote.
     

    Attached Files:

    • mp.jpg
      mp.jpg
      File size:
      252 KB
      Views:
      1,275
  20. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    Excuse me, but this tutorial is almost useless, since it have very crucial mistake at the first and most important step: How to Start and Connect to server.

    You are using default NetworkManagerHUD, but it useless for complete project.
    You better explain how to start a server via C# code and connect to it using direct IP connection.

    This simple task is very over-complicated in the new network system in compassion to the old one.
    I found several different ways to start a server and still not sure which one is correct.

    Also you should show how to spawn a player using a code, instead of automatic spawn of Network Manager.


    Please don't get me wrong, I am VERY admire the Unity, but this new network system and/or its manual is terrible. So hard to understand something about how it works. However previous network system I learned easily for a couple of hours.
     
  21. Sarrivin

    Sarrivin

    Joined:
    Dec 18, 2015
    Posts:
    24
    I have a problem with this tutorial:
    When I Host a game, my player and enemies (4 clones) are spawned properly via OnStartServer(). Enemies and my player are destroyed when server is stopped, with Unity still in Play mode.
    But OnStartServer() isn't called whan I host game again. Not at all. So my player is spawned, but not enemies.
    How can I fix it?

    Ps. Also Health isn't synchronized when new player joins. You must fire and hit another player to see his current in healt bar.
     
    Last edited: Sep 21, 2016
  22. DennyBoy

    DennyBoy

    Joined:
    Jan 19, 2016
    Posts:
    34
    Can you make a video seires on it too?
     
  23. timberlinehackerspace

    timberlinehackerspace

    Joined:
    Nov 9, 2015
    Posts:
    6
    Thanks for this. Is there documentation or a simple (like this) tutorial on how to allow this game to connect over the internet, rather than just LAN?
     
  24. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Thanks, good for mobile too?
     
  25. HK_PHANTOM

    HK_PHANTOM

    Joined:
    Jul 12, 2018
    Posts:
    11
    I'm on part 09 and I'm getting an error saying GetComponent needs a '1' type arguments.
    I've looked though the next part and it also has a GetComponent () which is also causing the same error.
     
  26. Spark_Valedi

    Spark_Valedi

    Joined:
    Aug 4, 2019
    Posts:
    1
    Hi, this link says it's not available anymore. And i cant find any networking/multiplayer tutorials. Can you guys help please? TIA