Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Unity 5 UNET Multiplayer Tutorials - Making a basic survival co-op

Discussion in 'Community Learning & Teaching' started by GTGD, May 13, 2015.

  1. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I originally started this set of tutorials so that I could get a feel of Unity's new awesome multiplayer system UNET. I've decided though that the end result of this series will be a simple co-op survival sort of game because I can! I've already got primitive zombie AI working across the network!

    I started with the Unity 5.1 beta but now that 5.1 is released anyone can follow what I've been doing no problem! Please note that you should be an intermediate Unity user because I don't explain basic concepts in this set of tutorials.

    There probably won't be any particle effects, sounds, weapon animations or any of that additional stuff in this series because it is primarily about UNET. That sort of stuff will be covered in my in development Gamer To Game Developer Series 3 tutorials.



    UNET Part 2 - Rotation Syncing
    UNET Part 3 - Being a bit more efficient
    UNET Part 4 - Latency Simulation!
    UNET Part 5 - Almost Time Travel!
    UNET Part 6 - Efficient Rotation Syncing
    UNET Part 7 - Unique Identity
    UNET Part 8 - Shooting
    UNET Part 9 - Taking Damage!
    UNET Part 10 - Player Destruction!
    UNET Part 11 - Player Resurrection
    UNET Part 12 - Zombies!
    UNET Part 13 - Zombie Identity
    UNET Part 14 - Shoot Those Zombies!
    UNET Part 15 - Zombies Bite Back!
    UNET Part 16 - Zombie Motion Sync
    UNET Part 17 - Better Spawning
    UNET Part 18 - Fixing my bugs 1
    UNET Part 19 - Matchmaking First Attempt
    UNET Project Folder
    UNET Part 20 - UI & Custom Net Manager
    UNET Part 21 - Network Animator
    UNET Part 22 - Host & Join Game Buttons

    If you're new to game development then see my Series 3 tutorials! You'll build the powerful FPS system that I use in my upcoming game Weekend Drive! Over 180 videos :)
    Unity forum thread for Series 3


    I've made a set of short multiplayer tutorials that are based on Unity's text tutorial. They cover the basics of multiplayer.


    This is the new GTGD S1 on Steam, a Unity 2017 multiplayer tutorial.
     
    Last edited: Nov 18, 2017
  2. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    248
    As usual.... you sir are AWESOME! Thanks, will check things out with the 5.1 beta in a day or so
     
  3. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    32
    omg, thanks very much
     
  4. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Thanks for the encouragement! Part 4 will be ready very soon.
     
  5. Teo3-N

    Teo3-N

    Joined:
    Jul 15, 2013
    Posts:
    2
    Could you please explain how to internet -part of UNET works? I can launch internet server only on my own computer (no other device works (don't know why)). And again, no other device can connect to it. And there isn't very much of documentation...
     
    Roma6 and erebel55 like this.
  6. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I'll get to that. I'll first implement bots after I implement respawning (video 11). Then I'll make my own UI and show how to setup server parameters manually (such as the IP address). Btw, if you are using Hamachi then take your hamachi IP address and copy it into the Network Address of the Network Manager. This is under Network Info on the Network Manager. Then build the game and distribute it. Now others will be able to connect to your game across the internet so long as you don't change your hamachi ip address.
     
  7. Teo3-N

    Teo3-N

    Joined:
    Jul 15, 2013
    Posts:
    2
    Thank you Sir! (I just watched your newest video, great job as usual. :) )
     
  8. erebel55

    erebel55

    Joined:
    Jun 14, 2014
    Posts:
    42
    Yes, an explanation of how uNet works (vs Photon) would be amazing. Also matchmaking :) Thank you for these videos sir.
     
    Last edited: Jun 8, 2015
  9. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I definitely want to implement matchmaking. My understanding isn't sufficient at this moment to implement it, but I'm sure when 5.1 is officially released it will become clear and I'll be able to see the pricing structure for it as well, and get a cloud project ID. If I can integrate matchmaking nicely then to me I can say to myself that UNET is superior to Photon. Already in my videos I have basic AI working effortlessly across the network, and it was easy for me to implement it, and this wasn't as easy to achieve in the old Unity networking system nor is it easy in Photon cloud which is peer to peer and so uses a master client to try and mimic central control somewhere.

    Personally I can already see UNET becoming really incredible for all manner of games, and especially RTS and turn based strategy games. If the networking system proves to be reliable then I personally think this is going to be the most significant technology that the Unity engine can offer at this current time, besides all the other amazing stuff like uGUI, mecanim, etc. Hopefully we'll see all manner of awesome multiplayer projects come to fruition.
     
    ramand and erebel55 like this.
  10. JesseAlexander

    JesseAlexander

    Joined:
    Nov 22, 2009
    Posts:
    286
    Wow, thanks for these! Now that Bolt was purchased by Photon, UNET is looking like a better candidate, it's just mainly been a black box thus far, going go binge watch this like some House of Cards :)
     
  11. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    @GTGD
    Awesome, as always very detailed and informative tuts, I pretty much learned mulriplayer with unity networking from GTGD,
    thank you much bro.

    And now the new networking i am ready to learn again from you,

    quick question:
    do you know if unity added the Area of Interest to UNET already in this phase 1?

    they seemed to release publicly 5.1 yesterday so i wonder if they have this feature in the new networking?

    they talked about it in unitite back in 2014, and it was planned for phase 1, altho in the presentation they were not already there yet.


    regards
     
  12. Adam-Buckner

    Adam-Buckner

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,669
    Woah! Bolt was purchased by Photon!? Interesting news (tho' not necessarily directly related to this thread, so apologies!)

    3rd party vendors still have a lot of advantages at this point, like hosting solutions, but UNet should be a robust solution, and it's built in.
     
  13. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Yup it's there though I haven't got to using it myself. The latest documentation describes it pretty well. In the manual it's under the section called Object Visibility. I've got heaps of reading, learning, and trying out to do with this new networking system.
     
  14. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    nice,
    ill take a look at that,
    if you get some chance and free time, it world be great if you include this Area of Interest feature in an upcoming tutorial.

    will be much appreciated.

    regards
     
  15. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    I dont know if any1 of you guys noticed, but there is something in unity 5 that really , really bothers me and maybe be a show stopper for unity instances servers.

    which is when you want to run a unity headless server ie: Server.exe -batchmode -nographics
    the servers or unity instances consume 10% of my cpus resources, even when the server is on idle, no mobs spawned, no players connected, complete dead server in idle ready to take simple connection. and still consumes 10% cpu resources, whats is the deal here, this wont work for unity servers, what if you need several servers instantiated in a single machine, it will just take all cpu resources without even having players connected, this will be a complete disaster.

    I dont know if this is a bug or because new unity enhanced whatever , but this is useless for unity servers, a complete no, no.

    unity4 does not do this, the same server in unity4 in idle in headless mode takes no cpu usage at all around 0 to 1%


    so if this is the case and small price to pay using unity 5.x then i think for me i better find a third party server ie smartfox or anything else.

    unless this only happens on my end??

    any have experience this? if not go ahead and give it a try ad post your results, maybe it is my setup, but am running both unity5 and unity4 headless servers same scene and unity5 eats 10% cpu resources vs unity4 takes 0-1% cpu resources. on idle both.

    this is not cool, not cool at all. i hope it is just a bug or my setup, because this wont work at all.
     
    Whippets likes this.
  16. Adam-Buckner

    Adam-Buckner

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,669
    Can you file a bug report with repro? And get me the report number? I'll see if I can get some traction...

    If you find it's in your setup, can you let us know?
     
    Whippets likes this.
  17. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    thanks, ill do that, lets see if GTGD or any1 here reading this will test to make sure it is not only me.

    I already opened a new thread about this issue in the unity support forum 3 months ago, but i guess noone read it or cared about it.

    http://forum.unity3d.com/threads/linux-headless-server-on-unity-5-idle-cpu-load.319081/

    there was others too complaining about this in other threads too,

    anyways hopefully people here will see this and test in their system to confirm it is not only me or not.
     
  18. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,653
    @GraphXCreations missed your previous thread - was only looking in the 5.1 beta thread. Because of the uNet memory leak bug #698749 which made testing uNet impossible, I did see a 2% load on my server, but because the leak was so big, I figured the load issue could be part of the same thing.

    My previous lidgren server had an almost zero load whilst idle, and I'd expect the same from uNet. If this isn't the case, it does need seriously looking at.
     
  19. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    thanks for the input Whippets, i guess i am not the only 1, so might be a real bug and not just my setup,

    hopefully more will corroborate.
    thanks again for testing and helping corroborate the issue.
     
  20. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,653
    Get a bug filed anyway :) I concur an idle server eats far too much cpu when it should be zero
     
  21. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
    I just submitted the bug report a minute ago,

    hopefully unity dev team will get that sorted soon.
     
  22. Wieljer

    Wieljer

    Joined:
    Nov 17, 2012
    Posts:
    28
    Looqmaan - Tremendous job with these tuts, I have been holding off but now that 5.1 is officially released I will run through your tutorials over this coming week! As always you have my greatest respect, keep up the great job and Unity community support. I know Unity is quite pleased that you have been the first to release this type of UNET tut course.
     
  23. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Good to hear from you Jeremy! Lol, most likely if Unity networking developers saw my code they'd most likely groan and say, look at the awful things this nubby nub is doing with our wonderful networking system that we spent years working on!
     
    Last edited: Jun 12, 2015
    moco2k likes this.
  24. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Would you be able to describe how you actually setup your headless server? Are you aiming to run it on a Linux EC2 cloud server or something like that?
     
  25. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Great tuts! At least half way in the first so far. :D

    Did they change something in the final release of 5.1? I didn't have to do any code changes to get my first try working. I was impressed it took only a few mins for a networking anti-talent like me to setup basic working lan multiplayer with it. xD

    There was no camera or controller confusion. I think my setup is exactly like yours in the first tutorial. Except I tested by making the vehicle example multiplayer. I added network identity to both the camera and car. Took some tweaking to get the rotations to interpolate on an acceptable level but its all fun. :D

    Now I'm experimenting making the scripts NetworkBehaviours not MonoBehaviours. I read its better for network scripts, but forgot where and if there's a "why" to it. :p

    EDIT: The only thing confused are the brake lights. They go on, on both cars whenever one hits the breaks.

    EDIT 2: Scratch that. The cameras switch. I know how to fix it, I'm just experimenting to find a no-code way to do it. :D

    EDIT 3: Can't do it. So I did as you did in the tutorial. What is the network ID for? I guess I thought the network ID would check automatically who the object belongs to, so we don't have to do it through code. Trying to make the wheel sync now. They're not for some reason, even tho I added a Network Transform on each wheel. Any idea why?
     
    Last edited: Jun 12, 2015
  26. JesseAlexander

    JesseAlexander

    Joined:
    Nov 22, 2009
    Posts:
    286
    Have you tackled (or are you going to tackle) some type of inventory? We're a bit confused since you can't sync arrays natively how to port our inventory system over from Bolt (it's grid based, Diablo-style)
     
  27. Wieljer

    Wieljer

    Joined:
    Nov 17, 2012
    Posts:
    28
    LoL!
     
  28. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I wasn't intending to go into an inventory system because that can get pretty complicated pretty quickly. Could you use a SyncListStruct for your case? Maybe one int for row and another for column and then a string for the item name?
    http://docs.unity3d.com/ScriptReference/Networking.SyncListStruct_1.html
     
  29. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    The network ID identifies each unique networked GameObject but you still have to control what GameObject is running what function.

    You have a car that has brakes and so do all the other players. Their cars are in your scene and they are all using the same scripts. You must make sure that when you press the brakes that only brakes on your car GameObject turn on and not all the remote client GameObjects. This is why you must do the isLocalPlayer check and send a command from your car that you control to the server. Then your car on the server will receive this command and will run whatever code you wrote in the command. You will then want to have your car on the server run a clientRPC for example to execute code on your car across all remote game instances. Or maybe you are using a SyncVar with hook function. Study the Unity manual and try stuff out and it will slowly make sense.

    http://docs.unity3d.com/Manual/UNetUsingHLAPI.html
     
    ChrisSch likes this.
  30. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Yeah, network was always hard for me to understand, but at least its clearer than yesterday. :p
    seanr helped me solve my problem almost entirely, and understand better, here. I just need to make the wheels interpolate. I'll try that with your tutorial when I have time. I noticed interpolation is in the first one, but I didn't get around it yet.
     
  31. JesseAlexander

    JesseAlexander

    Joined:
    Nov 22, 2009
    Posts:
    286

    Wow... I swear I didn't see SyncListStructs in the docs when 5.1 dropped, maybe I missed it or maybe they're constantly updating them. Thanks.

    BTW - This guy is hinting at UNET support for his inventory mod. I'm lobbying him to take a shot with it :) Cross referencing the threads :)
    http://forum.unity3d.com/threads/inventory-pro-ugui-performance-mobile-support-more.308505/page-18
     
  32. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I also bought that inventory system as well. I haven't had the opportunity to really make use of it but it looks like the best one out there by a fair margin and the developer is a real programmer. In my case I'm looking forward to studying his code so that I can learn how to make my own code more modular.
     
    bioert likes this.
  33. Ibzy

    Ibzy

    Joined:
    Sep 15, 2013
    Posts:
    112
    GTGD - Great set of videos, a priceless resource.

    I've been following along and have hit a snag with the Take Damage section. I'm not sure if something has changed with the release of 5.1, but it's almost like the Client only instance isn't being effected by the Commands.

    When I get to the part of shooting, as the client (Player2) I shoot Player1 and see both his and my health go down. If i shoot Player2 as the Host (Player1) debug confirms I hit him, but health doesn't appear to be effected on either.

    I'm using the exact same code you have used in your tutorial up to this point - any thoughts?
     
  34. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Get the project folder from my website and that should help you debug your code. There's most likely some mistake in what you've written because for me there's no problem. Btw, commands are only sent from the local player GameObject on the client to the player GameObject on the server. They aren't run on the clients.
     
  35. Rick Love

    Rick Love

    Joined:
    Oct 23, 2014
    Posts:
    76
    I've been meaning to research multi-player technologies. Thanks for the lessons!
     
  36. Ibzy

    Ibzy

    Joined:
    Sep 15, 2013
    Posts:
    112
    Thanks. Turns our there was a slight issue with my Health text object - the hea;th was affecting the right player, but not showing the health for the local player.

    Back on track :)
     
  37. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    55
    I have a question about Unet. I am working on a game match maker using c++/cgi and mysql/c++ connector. The reason for this is to reduce the amount of cheaters in my game as my players are currently complaining about too many cheaters. The main of this program is to be able to create new usesr, login users, create gamse, destroy games, and send message between players.

    I don't want to spend too much time on this, so my solution is to allow players be able to flag/report other players and I will use use a collected game data to determine if the player cheated then ban them. This is why I don't want to use Unity built in match making system. So far, so good, players can register, login, message each other and that's where I stopped to ask IMPORTANT question here as Unet is new and I am not sure about this.

    My question is:
    If I know a public IP Address of a player that I want to connect to, can I connect to that player with Unet library just by providing the IP Address or does Unet have firewall or hole punching problem like the old Unity network system? Did Unity solve the NAT problem with Unet or do I have to implement my own TCP hole punching solution using c#? Also, when I say public IP, I mean connecting to another person with different public IP in another place NOT on the same local network. Thanks,.
     
  38. vekkna

    vekkna

    Joined:
    Apr 6, 2014
    Posts:
    6
    Brilliant tutorial, thanks.
     
  39. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I think you'll want to see this Unity forum thread though it may leave you with more questions. I don't know anything beyond what is in that thread and I haven't yet looked into how to use the Low Level API.
     
    AzatKhafizov likes this.
  40. fantom642

    fantom642

    Joined:
    Oct 14, 2013
    Posts:
    19
    Great tutorials! Thanks a lot!

    Just a quick question: Do you know about any way how to get a list of nearby servers? Any alternative to MasterServer.RequestHostList in previous unity networking system?

    As far as I know, the remote client needs the IP address of the server in order to connect to it right? but can the server advertise this IP address in a local network?
     
  41. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    UNET has a matchmaking system for making public games possible. You'll want to have a look at the documentation on it. I haven't gone into any code on it yet but I intend to once I can sort out getting matchmaking to work properly on my end. In my project the client times out and I think it's because I have lots of zombies all syncing their motion across the network and it's all just a bit too much.
     
  42. fantom642

    fantom642

    Joined:
    Oct 14, 2013
    Posts:
    19
    Thanks for a tip. It seems like unet Matchmaker requires a server in a cloud (but please correct me if this assumption is wrong). What I'm looking for is a solution for a local network. Any ideas for the local network?

    PS: good luck sorting out the matchmaking problems.
     
    Last edited: Jul 14, 2015
  43. Frusciante

    Frusciante

    Joined:
    May 6, 2013
    Posts:
    1
    Hay man, thanks a lot the videos, they've been enlightening
    .
    Do you think this is a good system for turn-based games? How would you lay down a turn-based system?
     
  44. PCSzymik

    PCSzymik

    Joined:
    Jul 10, 2015
    Posts:
    2
    Make guide for teams/factionsc and classes
     
    Last edited: Jul 14, 2015
  45. Dracoth

    Dracoth

    Joined:
    Jul 14, 2015
    Posts:
    1
    Hi! This tutorial is awesome!

    The only snag I'm hitting is that the movement between clients is still really jerky (based on the first tutorial only). I'm not quite sure where I went wrong, as our files look identical, as far as I can tell. The second player ghosts between two spots and blinks constantly whether he's moving or not. I'm hoping it's just a small issue, but I can't figure it out. The executable has been tried on three different computers, all with the same effect.

    The code being used is uploaded.
     

    Attached Files:

    PCSzymik likes this.
  46. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    30
  47. PCSzymik

    PCSzymik

    Joined:
    Jul 10, 2015
    Posts:
    2
    Dracoth I have the same problem
     
  48. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    Keep watching the videos.
     
    Last edited: Jul 15, 2015
  49. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    I don't how to do that but this looks like it will take you in the right direction
     
  50. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    425
    This system is ideal for turn based games. I recommend you first go and study/figure out how to make a turn based game in single player and then think about multiplayer. I think there are many ways you could go about achieving a turn based system. Of the top of my head here's what I just imagined now. Each player could have a flag that is set to true by the server when it is their turn. They are allowed to only send one command to the server and then the flag will be disabled. The server will wait for a command from that player but if they take too long then their flag will be disabled and the turn will be given to the next player.