Making a Multiplayer FPS in Unity (E03. Networking Intro) - uNet Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This guy fucks.

👍︎︎ 1 👤︎︎ u/changingminds 📅︎︎ Oct 12 2015 đź—«︎ replies
Captions
thanks for tuning in at brakus hello and welcome to the third video on making a multiplayer FPS in unity in this video we're going to be putting aside making the character for a bit to focus on networking so we'll finally add some networking functionality to our game that means that we'll set up a network manager and instantiate a player prefab into a network scene I will also talk a bit about local player authority and those kind of concepts and on making it be making a short presentation on how you net actually works in terms of programming and how clients connect to a server and all that kind of stuff so before we get started I quickly want to mention that I've now added the multiplayer FPS tutorial on project to github so if you want to download any of the files over the entire project you can go here things as links are in the description and if you want to have the exact version that I'm currently on or a previous version you can go under commits here and I'll try and name these these according to the video so there should be a version saying video 3 video 4 and and so on and there is also a test branch that you can use if you want to see what I'm working on for future videos also I've gone ahead and completely redone the donate system I thought it was really bad before and a lot of you guys have said that you wanted a better way to kind of support the videos so you can go to break Easter comes less donate and fill out the forms here if you want to make a donation it truly helps me out a lot and yeah so thanks a lot and let's dive into today's video so let me start by explaining a bit about how unit actually works so I've made this model here that displays kind of how unit is split up into two api's application programming interfaces the first one is the high level API and this is what we are going to be using this provides a bunch of classes and methods that we are going to be using to apply networking to game the high level API HL API is built on top of what is called the transport layer this is this is much more bare-bones and low level and this takes care of a bunch of stuff behind the scenes so the transport layer can be directly accessed but that's not something we are going to be doing and basically this takes care the high level API connects to the transport layer and takes care of stuff like distributed object management and stake state synchronization along with a lot of other stuff like message queues and all that so if that sounds very technical to you well then you're in luck because that's not something we need to care about in our instance so the transport layer gives great control but it's also very technical and therefore will be fine off by using the HL API so on top of the HL API unity has gone ahead and made some components for us for example we have the network manager the network identity the network transform component and a bunch of others and these are basically suggestions by unity of how you could implement the HL API in your game so these do a lot of stuff for us and they are going to be need to be customized in some kind of way so basically what we are going to be doing later not in this video is we are going to override or extend these components to create our own custom scripts that has exactly the functionality that we are going to be needing we might add on to these components or decide to do stuff in a different way that makes more sense but basically these don't need to be used at all you can create a game a multiplayer game by only scripting or making your own components that communicate with the HL API so that's kind of how the unit programming functions and basically if we take a look at how the networking in unity actually works well unit is based off of the principle that one of the clients meaning one of the players is the host so use you have seen this in a lot of games for example Call of Duty uses this principle of naming one of the clients the host and if that clients the client then leaves another client becomes the host so this it's a pretty neat way of doing things because basically we have this client here called the local client because that's part of the host and we have also the server sitting on the same machine and this machine then has an IP address and you have just used a sample IP address this is what an IP address could look like and then all of the clients or all of the other players that want to connect to this host well they basically connect to that IP address to the server which then connects locally to that client if needed so let's say that this client wants to shoot this client well he connects to the IP address to the server and the server connects back to that client so if this client needed to connect to this client well he maybe shoot that client by going to the IP address going through the server and then the server will talk to the client here so everything goes through the server which sits on the hosts machine cool so now that you have this understanding I might present to you a problems because well a lot of modern games don't use this kind of IP address you don't want to be typing in an IP address every time you want to connect to a player I mean if you want to play over the Internet you want some kind of matchmaking system and when using this kind of setup we have some issues with firewall preventing connections and all that so to solve this unity has created a matchmaking service which is called the unity relay server and here all of the clients connect to a server that is hosted by unity which then community Cades to the host and that means that unity takes care of IP matching and it solves issues with firewall and net configurations and you don't need to know exactly what this means and how it's sold but just know that we won't have any issues with connecting that will have to be solved by the clients they can just connect and this is not something we are going to be integrating today it is something I hope to integrate in a later video but basically unity hasn't released or announced what the prices on using this will be a very common thing to do is have a limit on the number of concurrent users that you can have on this system before you need to pay but unity hasn't released any information on this so I won't go ahead and say anything yet about what this will cost and how many users you can have for free if any so we'll see about that but for now let's focus on setting up some networking locally on this machine so the first thing that we are going to be doing here is adding this central networking component by unity so let's create an empty game object over here and let's call this the network manager and that's without a T let's reset the transform on this and just drag it to the top of the hierarchy there let's go ahead and hit an add component here and let's add the network manager that's the one and this basically is a component made by unity that can do a bunch of different stuff well it sets up the scene here to do networking and you have a bunch of a different info here on how you want this set up and for now we can ignore this and you also have the ability to use this component for spawning in players so you can add a player prefab here which is what we are going to be doing so we have this a network manager and we also have a network manager HUD and what this does is right now when we hit play we see that well nothing has really happened and we just have this player and he's not on a network in any way so let's just go ahead and drag this player into a prefab and then delete him from the scene and now let's look at the network manager what basically we need some way of interfacing with the network manager script and unity has created this component that we can use while testing called the network manager HUD so this creates a hits head-up display and you can enable it and disable it here and you can also play with the offset if you wanted someplace else on the screen and this will just give us some some a UI that will allow us to connect using the network manager but we need to add a camera here which will show the scene when we are not playing so when we don't have a player yet and we are in kind of the lobby where we want to choose what we want to connect to well then we want to have some kind of camera that just views our level so let's right click here and hit create camera let's reset the transform on this and let's rename this to maybe scene camera let's also make this the default camera so let's let's tag this as main camera and let's drag it up and out maybe up some more and let's rotate it down to view our scene and I'll just switch to global up here and that looks ok oops I screwed up the rotation here so that's 0 0 0 and then rotate it down on the X something like that and you can see in the game view here how that looks and I think that's going to be fine for now I might drag it up a bit more and then rotate it along the Y and I think that looks just fine and you can see here that our skybox looks very boring here so let's change this from skybox to solid color and let's just make this some kind of gray or deep blue here just temporarily at least we'll make it look something like that it's going to be fine and this looks a bit washed out but we'll have a little get creating some more awesome graphics later and we want an audio listener and all of that on this component so now that we have that here and we save the scene here in hit play you can see that this the network manager HUD is now displayed here and we can choose to either create an a host here or join as a client and then we specify an e IP address a local local host here is just in the name of a certain IP address meaning on the local system and you can also choose server only which we are not going to be messing with right now so and you can see here we can enable the matchmaker we'll get into all of that stuff so now that we have that we can create a select land host here but it says that we have to add a player prefab so let's go ahead and do that so inside of our network manager we have this spawn info tab and we have this player prefab slot so let's go ahead and drag in our player and it will give us an error here it will say that the player prefab must have a network identity so this is another component created by unity and the network identity is a component that we add to all networked objects in our scene so stuff that we want to communicate on the network in anyway are going to need a network identity component so we add that and it has two toggles here the first one is server only we don't want that because we're going to control this player so we wanted to sit on the local client and then we have local player Authority and this makes sense for us because we are controlling the player locally on a system and then we are sending the move information and other stuff out through the server to the other clients so they can update it on their systems and therefore we want to check this local player authority because we have the authority of the movement locally and that we send it out through the network call so we can clear the console there and that's basically all that we need to add in order to add it to the network manager slot so we'll just drag it in there and the player prefab and we'll have Auto create player prefab and we have the ability to specify where we want the player to be created because right now it's just going to be created on this zero zero zero mark so in the middle of Icy so let's instead add a few objects or at least two for where our player will be spawned so let's create an empty object here and let's name this spawn point and let's just add a component to this and if we search Network here we find the network start position and that's just the only completely only component that we need to add and let's also create a gizmo for this so let's just select an icon con here and maybe make that blue and you can see now that it says spawn point here so we'll just have it sit on zero zero zero then set the Y to one because we want him to spawn a bit up in the air and let's the blue arrow here is probably the way that the player is going to be facing so let's give this an X amount higher or I'm sorryi Z amount of negative five duplicate this rename this to spawn point two and let's rename this to spawn point one I maybe just drag this down so they're right by each other we can maybe actually make them a child of the network manager so spawn point two here we are going to set the Z to five and we are going to rotate him a hundred and eighty degrees on the y oops and if we change this to local you can see how that works so now these two objects are facing each other and they are 10 units they have a distance of 10 units so this way we can go into our network manager and we can change the player spawn method to round what robin and this will basically first choose one of the spawn points and then the next player that creates will just choose the next spawn point and the next one and then loop so when we only have two this will make sure that we will switch between the two which is pretty cool because then we will also always spawn opposite of each other cool so now that we have this in place we can go ahead and hit play on this and we can select land host and we are spawned into the scene but we get a small warning here saying that there are two audio listeners in the scene and that's because our scene camera is still active and if we were to go ahead and try and build this and let's actually do that now and there will be a couple of issues so if we go to a file build settings and just add in our main level into the builds list here and one second my phone is going off so I'll just turn that off so we add this to the list and now basically we have to select the target platform I'm going to build for Windows 86 and development build is fine and then we can hit build and run and we can choose some place to build it you can see I've made a test build here already but basically I've gone into my project folder created a folder called builds and just name this one build one now hit save on that hit yes to it replace it and it's just going to build here and it should go pretty quickly and once it's done it's going to open up on the window here I'm just going to select windowed and maybe a bit smaller of a client here and then hit play we get this window and now we can hit play in the Z in unity here and these basically represent each or two different players or two different computers or clients that want to play with each other so I'm the first one here in unity we can select hosts and we get dropped into the scene and here we can select client and you can see now that we are facing each other and this actually was rotated wrongly but we are standing opposite of each other and starring down each other's barrel and that's awesome however you can see in unity right now it's complaining that there are three audio listeners in the scene and that's because we have on this a local client here we have both a a camera on the one player a camera on the other player and the scene camera and that's another issue with this and that is that the player motor and controller is enabled for both the players on both the systems so when I go ahead and move here you can see that the other player is moving too so both players are responding to input and that's of course something we need to take care of but that's one more issue because if we go in here neither of the players have moved so what is going wrong here well basically we need to kind of disable the components that sit on the other objects meaning the ones that are not controlled by the system so that we don't affect other clients when without input and also we need to kind of network how the movement should go so we need to add some kind of component that will transmit our position and rotation over the network so there's a bunch of stuff we need to do here and in order to kind of set this up we create a script and let's call this the player setup script so under the player here we'll add the player set up new script and let's just make that of type C sharp and in this script if we double-click it to open it up in Visual Studio basically we are going to be creating some lists for objects and components that we want to disable when there for all other players in the scene than the one that we control so I hope that you can follow me on this so let's just remove the using system collections namespace we're not going to be needing that and then let's just remove these two methods for now so let's start by creating a list of components that we want to disable so let's make this an array so all components in unity are derived from behavior so if we want to kind of make this very general we could make the reference to the player motor the player controller in each their variable and then disable them but I'm just going to create this array so we can just add in any kind of component and don't need to add anything to the script each time so let's make a behavior here and that's going to be an array and let's call this our components to disable and let's close that off and then in our start method basically what we want to do here is we want to check if we are in the network or if we on the local player so in order to do this we need to access unity networking so first off we need to include the namespace up here we need to be using unity engine networking and all of the high level API sits within this namespace but we also need to derive from network behavior so this will allow us this script to act as an object that is networked and will have the API up here so now basically all we need to do is check if we are not is a local player so if if is local player is equal to fall so if we are not the player or if this object isn't controlled by the system well then we want to go ahead and disable all of these components so we'll just loop through that an array so we'll say for int I equals 0 is less than components to disable dot length I plus plus and then we want to say components to disable that enabled put done enabled whoops we want to take the height and set that equal to false so basically what we're doing here is we are saying in the start method when the player is spawned we want to check if we are controlling the player and if not well then we want to disable all of these components so we basically loop through the list and set the component that we're currently looking at enabled state to false cool so that's basically all we need to do in scripting here we could also start disabling the camera but let's do that in a sec so let's minimize that and you can say that there's a warning here saying that it will never be assigned to you and that's because we need to mark this as a serialized field so that will allow us to access it in the inspector here and assign stuff to it so now the player setup when a multi object editing is not supported there we go so that just needed to update you can see that we have this network channel and to extend interval information and that comes when deriving from network behavior that can just be ignored but we also have this array and let's just increase the size on this we want to disable a bunch of things so if we are not controlling on this object well then we don't want to have a motor and we don't want to have a controller and we do not want to have some kind of camera or so we want to disable the camera component one audio listener so those are the components that we need to disable but these components sit on another object so we need some kind of way of dragging them in here and for the camera that's pretty simple you can simply drag the object in here or add him to back into the scene and then browse here and simply drag the camera in and that will simply drag the first component that sits on the object into the slot but how do we access the audio listener well that's actually pretty simple we can go ahead and lock this inspector so now then we said select another object it's just going to stay on that object and then we can create a new inspector so we hit add tab inspector and let's just drag it over here and that is going to change unless you log it so now we can go on the camera here and we can take the audio listener and drag it in there so you can just keep the inspector here or you can just close it here I don't think we're going to be needing it again in this tutorial and then we can unlock this so now that we have these components to disable added we can hit apply and we can delete the player and we can try this out but I assure you this should be working so let's just go ahead and add some functionality for disabling this scene camera well basically when do we want to disable the scene camera well we only want to disable the camera if we add the local player so we don't want to disable or enable the camera each time and by the player joints we want to disable this local camera when we join so therefore we can simply add an else statement here where we disable the camera because we marked it as a main camera we remember we added the tag there we can simply say camera dot main dot game object dot set active and set this to false so that that's all we need to do and in the future we're not going to be doing this under the player setup because the camera and the scene camera doesn't really have anything to do with the player but for now we can just add this functionality here so that's basically all we have to do but we also have to rename alit once we disconnect so in order to do that let's make a reference to this camera first of all so let's make a camera variable here and that's just going to be private and let's call this scene camera and then here in the start will set whoops this okay I'm just going to write this again we'll set scene camera equal to camera dot main and then down here will say that if scene camera so if it's actually found a camera it's not equal it's not going to be equal to null and then we can simply say scene camera dot game object dot said active and then we can disable it so this will make sure that even though it doesn't find the camera we don't get any errors and then if we want to re-enable this we can use the handy function provided by unity called on disable and this is also called when the object is destroyed and in here we can simply say that if scene camera is not equal to null well then we want to say scene camera dot game object dot set active and then true so then we enable our the object again and this of course uses set active we could also just enable the component which might be something that we want to do but on enabling and disabling the whole object we don't need to do anything with the audio listeners so this is going to work just fine so that was all for the player setup script now let's save this build this and building the play with you and by the way the shortcut that I'm using is a controlled B let's hit play on this and play in the game and let's select host and select client and we need to turn here you can see now that we are not getting any errors in the console and if we inspect in the hierarchy here the scene camera is disabled and the player that we own has both its player motor and the player controller and it's camera but the other player here doesn't have any of these components enabled so this means that when we now move in the game we are the only one moving however it still doesn't update so we need some kind of way of sending this information over the network and that's what we're going to be looking at in the next video so that was basically all for this video I hope you enjoyed it a lot and we're able to follow along with the concepts if not you're going to get the hang of this don't worry networking is very different from other kinds of programming and can be pretty hard to get a grasp on because you have to kind of think of so many different players connecting and interacting with each other so I hope that this made some kind of sense and if you couldn't get the programming side to work of course check out the github page it's called multiplayer FPS tutorial and there should be a link in the description and if you enjoyed this series a lot and want to support me of course you can go to practice comm slash donate and yeah make it a nation it helps out a lot so I hope to go full time in the near future and this is the kind of stuff that helps me do that so thanks a lot for watching this video and I'm looking forward to seeing you in the next one
Info
Channel: Brackeys
Views: 374,230
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, material, materials, beginner, how, to, howto, learn, learning, course, series, tutorials, workflow, technology, game, development, develop, games, programming, coding, basic, basics, C#, network, networking, multiplayer, intro to, introduction, initial, spawn, Multiplayer Video Game (Website Category), First-person Shooter (Media Genre), Unity (Software), instantiate, player, setup, create, hlapi, transport layer, client, host, server, matchmaking
Id: -m28axeuRNs
Channel Id: undefined
Length: 30min 58sec (1858 seconds)
Published: Sun Oct 11 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.