Let's build a 4-Player Networked Game LIVE - Online Shooter (with Mirror & Unity)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/AutoModerator πŸ“…οΈŽ︎ Nov 28 2020 πŸ—«︎ replies

This is great, have it queued up to watch later. Thanks!

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/Telefrag_Ent πŸ“…οΈŽ︎ Nov 29 2020 πŸ—«︎ replies

Thanks for sharing this! It’s actually been a long time since I watched Jason.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/anarcht πŸ“…οΈŽ︎ Nov 28 2020 πŸ—«︎ replies
Captions
all right hello out there everybody hopefully you're excited and ready to get to this uh today we're gonna go through the process of building a multiplayer network game if you've been interested in building network multiplayer games and just kind of never really had an idea how to get started this should be kind of perfect for you i'm going to go through the process of taking a single or a simple multiplayer game that's local multiplayer that you can read all the source for and there's really not that much and we'll go through making it multiplayer using mirror so if you've never used mirror before don't worry i'm very new to it as well where it's going to be kind of a learning process for both of us but we should be pretty streamlined in getting this all set up building a multiplayer game is not too complicated if you've done it a bunch of times and the things are simple it just gets much more complicated as they get bigger or when you're converting a project later on so here we're just gonna start with a project that's very early on i'm gonna show you the process of just going through that conversion but if you haven't seen the previous streams by the way and you don't know what the code looks like don't worry i'm going to run through all of the past code first we'll talk about it real quick to take like five minutes go through how the project works how you run around with two players using playstation controllers and then how we convert that into network multiplayer um before we get started though if you don't mind just sharing the stream or maybe just hitting the like button or subscribing and sharing whatever you can do really helps i appreciate it if you just go out and like post it on whatever your favorite sharing thing is that'd be awesome um but i guess yeah that's pretty much all i had to say so i guess um was there anything else i wanted to ask oh yeah i was really curious before we get going how many of you have already seen the previous streams can you just put in like yes or no on whether or not you've seen it i just kind of want to get an idea of the number of people so i know how deep to go into the uh the passcode it's not going to take very long like i said but it'll be i just getting some context there and how many people have actually seen this stuff so let me switch over to desktop mode while everybody answers that and start showing what we're going to be building what we're building upon and what we're going to turn into a multiplayer game you might have seen in the thumbnail got a laptop sitting right next to me to do multiplayer development on so we kind of connect to it over the network once we've got it working locally so people shooter game i got my controller i can move around a character i got the red guy as player one i believe i can move and i can aim with the other thumb stick so here you see the thumbstick spins him and i move around using the left thumb the right thumb is aim left thumb moves him the second controller normally moves that character but i've reconnected the second controller over to my laptop so i can do network testing instead and we're going to get rid of kind of having a second player locally to go for network multiplayer anyway so i didn't want to go back to it but ideally you get the idea that this would move around the other player and this one moves around the red player and we just go around and blow up enemies actually i have all the enemies turned off so if i go over here and turn on one of my spawners i see some bad guys start to show up let's see and then you see that i've got robots here and i blow up the robots i get points they explode and again i'm going to show you what that code looks like in just a second we also have a score system here keeping track of this player's score and then we've got a high score in the middle that you can tell i kind of left my guy sitting there shooting straight at bots forever but if i hit that button it'll reset the high score and then whenever we get a new high score it should just become the high score there we go that's our new high score that saves off into player prefs let's um jump really quickly into how the code works right now and then we'll dive into implementing or importing mirror and well turning it into a network game instead so the current version of the code has only a couple of scripts you can see here there are one two three four five six seven eight scripts it looks like yeah not very many and they're all very very small they're not complicated at all if you look at like the bom script here um actually which one should i go through first um let's look at the i'm gonna look at the enemy script first so the enemies just walk towards the player right they're very simple i'm gonna open up the script take a look at it then we'll look at the player because i feel the enemy is simple super simple player's a little bit more complicated so we'll start with a very easy one so the actually in here in visual studio if you see if you expand out the solution explorer here one of the very few times that i like to do this when i'm trying to go through and look at all of the code it's very rare that i need to do this and use the solution explorer with unity project but here it's nice to see all of my code so let's look at the enemy here so the enemy is just a simple mono behavior just a standard script like we would normally have that's going to change it's got a prefab for when it gets hit that's just the particle system an explosion prefab for when it dies some health a score value that we can adjust for giving them you know more and more points based on how difficult the enemies are then in on enable we're caching an audio source and resetting our current health we just cached the audio source here because we didn't have an awake and there's no point in adding it so we just reset the health right here in on enable and then in update we look for the first player that we can find it's very very simple it's not doing anything complex it looks for the first player and then it sets its destination of its nav mesh agent to that player so it'll find any player that's active in the scene if there are two it's gonna always find that same player so the one player is going to be in trouble but whatever we don't have to worry about randomizing it right now and then we just get our nav mesh agent set our destination to it so this makes it just essentially follow a player now we probably should cache this or something but that's not the point of this we're going to multiplayer network stuff instead a couple other things it does it takes damage at a point the reason it takes a point in there is so that we can spawn a particle you see here we instantiate a particle and then here we play an audio source when we get hit check to see if we are below zero health or at zero blow up we'll play an explosion particle turn herself off and then tell the score system to add some points the only other thing that we do is check to see if we've run into a player here in on collision enter if we've run into a player we reload scene zero so that's the kind of thing that we're going to need to maybe change when we do the when we do this conversion because we don't want it to just load scenes randomly we got to do something a little bit different there or something a little bit more intelligent or maybe just disable that for now i don't know let's look at the player now so the player moves around by just reading the two axes the horizontal and vertical axes i'm going to just scroll through the code real quick but let's talk about the first couple fields up here we have a speed field for just controlling how fast we're going we have a transform named direction i'm going to show you that in just a second another transform name shoot point and a player number now the transform name direction if we just look in here if i search for the word direction you see that it's actually an object that i just have placed in the scene i can hit f to go to it over here clear the filter so it actually shows up and turn on the mesh render you can see it's just a box that's lined up and notice the camera here it's actually lined up to match the camera's rotation just with only the y value set so i'm using this as a transform that we'll use to um well let me show you let's show you where it's actually referenced because we use that direction when we do our movement code so oh just keep that in mind for a second i know i kind of jumped ahead for one second there but i wanted to make sure that you know what that is that we also have a shoot point that's this little transform that's just on the end of the player's gun if i go down here and look for search for shoot point i can click on it and expand it out right there and hit f to just zoom in you see that it's just this little point kind of out at the end of the gun if i hit play on it you'll see that it kind of matches and lines up with the gun when the gun's animating here let's just do it play and not just make you trust my word for it right so we hit scene view oh actually it's off oh i lied it's actually down i remember now i lowered it because the reason that it's lowered is so that these bullets will fly out low enough to hit the enemies when they were firing from that actual shoot point part um they were too high and they were going above the enemy's head so i lowered them down totally forgot that glad i covered it because i would have not mentioned it if i hadn't all right let's uh dive back down into how it moves so we've got the awake that caches the animator and then an update we move with a controller and we aim with a controller so move with controller reads the horizontal and vertical axes it reads horizontal plus our player number so we read horizontal one for player one horizontal two for player two and then we do the same for vertical player number one reads vertical one player two reads vertical two we create a vector three named movement using just our x and z because we don't wanna go up and down on the y axis and then we just translate it using our movement vector times time dot delta time so it's frame rate independent multiply it by speed so we can control it and then here's that transform i told you about the direction so the second parameter here this isn't a multiplication this is a parameter there with a comma is this the transform that it's relative to so we're going to translate it in that direction relative to this direction objects forward so we're going to move it you know if we're going right on the x and it's going to be right in relation to this direction object so i just keep it easily lined up with the way the camera is facing and if i want i can spin it around flip the controls or whatever else and i don't have to explain the math for translating that stuff the other part we have is an aim with controller that just reads horizontal aim and vertical aim for our player numbers and then sets our forward to that vector so that we basically just snap towards whatever direction we're pushing here on the x and the z let's go look at those um input settings real quick and then let's dive into well making it multiplayer because there's a lot of stuff here um and well actually oh look at one more thing let's go see um what did i just say i was going to look at like i like oh the inputs go to the input so go to edit and then project settings and input where's that input manager right there so expand out axes and you'll see that the controls that i've set up the reason i want to show this is because if you see vertical and horizontal those are default if you see vertical one and horizontal one you might think those are default they do not exist by default they're things that we've created so we changed the horizontal down here to be horizontal one that reads from joystick axis one and then made a horizontal two that reads from joystick axis two very simple stuff it just did the same for the aims except for the aims we had to change the axes here it's axis four for aim2 and i think um i don't i don't remember what it is for vertical actually i think vertical was axes three so a vertical as axes three for vertical one and vertical two instead of the y axis which was again just for the playstation controllers it's gonna depend on what they look like all right i'm i'm done talking about how it exist how it works right now i wanna get into the network side let's get into moving these players over the network setting it up so we can actually run around and then we'll talk about the different code like launching bullets and spawning them as we go through converting them so the first step here is going to be to import the mirror library um let's do that now to get mirror you first go to the asset store and you hit search online you go into the asset store window let's see if i can pop that up here and so you get your asset store window it looks something like this and then you search for mirror go up here find mirror and it should be just available oh let's see how do i get the mirror there it is mirror by vis 2k so you grab this and import it or add it to your assets really then you can hit open in unity and it'll pop it up in the unity editor and go right to the package manager the other way to do it if you already have the asset is to just open up the package manager window make sure that you're on packages my assets and then just search for the word mirror and you'd find it here i didn't need to search for it let's search for it anyway search for mirror see it filters down i've got mirror and multiplayer mirrors the one we're going to use though and we'll just import it into the project this shouldn't take too long but it's not the tiniest project it wasn't very big though let's see i'll give it a couple seconds real quick and i'll take a drink um by the way anybody here used mirror is just show of hands how many people here have used mirror in the past in chat while i take a drink and we import this all right almost in it's importing anytime now it's busy for 30. this is taking longer and i thought okay there we go it looks like it just finished up so we should have a mirror folder here and then this mirror folder should be full of examples let me just hit play make sure that i can still compile everything works after importing it looks good and then we'll go jump over to one of those example scenes so i'm going to go into the examples folder they've got a very basic example and it's got let's see in scenes example i'll save my scene because i don't know if i've changed anything in level one and we'll go take a look at it so this scene doesn't really have anything in it if you look around like i don't think there are actually any objects in it at all are there see anything on the canvas there might be something here that shows up on the canvas but when i hit play you're going to see what the default mirror setup looks like so by default we get this little gui here this little old school ui that has a host plus server button or a client button or a server only button i'm going to hit host server plus client and this has essentially started up a network server and connected me to it obviously it's extremely boring because there's nothing going on here i'm the only player and it's just showing that a data number which totally doesn't oh i don't think it impresses anybody it doesn't impress me so first step is going to be to set up our second player i want to make sure that before i go into modifying my my current code in my current game that i can connect their example so that way if there's an issue with my setup or something that i'm doing i know that it's with my setup and it's not something with mirror number one first first step first rule get it working with the example make sure it works and then we'll go on to our own stuff so to get it working with the example we need two instances of this thing running i could either run two unity instances which is something i would do a lot of the time but it was something i heavily considered there are a lot of really cool assets and tricks for doing this but this time i wanted to show how to do it with builds because i think most people are going to do it with builds and it if you're not doing too much debugging it's about the same you don't get as much benefit of doing it from two editors two editors is nice don't get me wrong i'd always recommend it but that's not how i want to do it in this video so we're gonna go into edit or file and we're going to go to build settings and we're going to create a build of our example level and then we'll connect it to our local editor one and see if it works so do that i need to add my open scene or add the example scene and then just hit build and run and i'm going to give it a folder i made a folder called build i'm just going to build right out to that just right off my route i just made an empty folder that i could build this to it's going to put the executable there and all of my tiny little bit of data so we should get an example window that pops up and if i haven't changed it it might pop up full screen and make it all kinds of weird i guess we'll see in just a second there we go yep it popped up full screen i hit alt enter and now i've got this giant window that goes all the way across my screen so before we well yeah you know what let's connect it let's see if it works and then let's fix it so here hopefully you can still see this window i've got the little host spot right up here i'm going to click the button to host the server i've got my player data right there in this window that's way off the screen and then we'll hit play in unity close out this build settings window and then we'll see if we can connect so i hit client to connect to my local host here let's can i scale that up ah it's not gonna scalp call there we go and look i can connect i've got player zero and player 1. seems like it's working so far all good so i'm going to close out my big giant window that scrolls off the screen and let's switch it to windowed mode and make it nice and tiny this is something that you're always going to want to do when you're building out these multiplayer games so we're going to go to edit and then project settings or i could have done it from the build and hit project settings and then we'll go to let's see where is this one i always forget this section it's player that's right and then full screen window will change to windowed and then i'll just leave the default resolution of 1024x768 something nice and small save and now let's try um well i don't know if i want to try another scene i feel like trying a bunch of sample scenes kind of cheated just like hey look something else moved let's make our own thing move so let's set it up now so that our our scene can have a network manager and we can connect and see where we got to go from there so we'll go into the existing working version of the game so go to scenes go to level one go out of 2d mode and let's go find the player real quick just zoom down a little bit so this version works we can run around with the controllers let's add a network manager and see what that does so we'll start by just going to well actually i wonder if there's a prefabs folder in here they have a prefabs folder a network i think they just have scripts in there i don't know i'm just going to create my own so the way that i generally do is go create empty network manager and then add a network manager component and save so now if i hit play you're gonna see that well we're not gonna notice any difference right everything's gonna be pretty much exactly the same i can still run around with my controller and shoot and i don't have that little ui i don't have that hud there so i'm gonna stop playing and first we'll add the hud the hud is actually a separate component you have the network manager it auto adds the transport which is like the network communications layer you can swap it out for different types of games different scenarios that you might need but this is the default one though but we need to add in the uh look i have network manager already filtered network manager hud that's the one we want i'll save that we'll hit play one more time and see where we're at there we go we've got a ui now i still have a bunch of guys standing here shooting and i can hit host and well i mean that's about it if i yeah it's not doing anything else it's letting me start and letting me run but it's not actually going to work as a networked game let me show you why watch if as we build out at our scene so i'll go to build settings let's add our open scene delete out that example one i'll save hit build and run we'll let it do a quick build and then let's just watch what happens on the client and on the server you might have noticed that like right away it's starting up with two players already in there shooting we haven't connected two players it's because we haven't actually done the code but let's go through it anyway so here we go it loads up that's ah where'd my unity window go there we go we've got this instance of it here that moves around with this controller and then if i hit play here i should be able to start up a server here let's see i host a server let's go over to the other window here see if i can connect to the server yep and now i'll move around this character on this controller or this system and you can see like oh it moves but it's not synchronized it's not actually doing anything they are connected they're just not sending any useful data back and forth in fact if i go in here and just turn on my spawner see that enemies will spawn on what here i'll turn on all these spawners enemies are going to spawn or i'll just get that one i guess there and oh look at that the guy died and the whole level reloaded and stuff so there's obviously a bit more to it than just um than just putting in a network manager and having it magically work right so let's go through the process of making it actually work if we expand out the network manager you see one of the first things here is this player prefab is i said first thing it's like first thing that you should really pay attention to and know about the player prefab is going to be this object that gets spawned for every player that comes into the game so when a player joins your game it's going to spawn a prefab for that player and associate it with that player's network connection and you use that to well communicate with the player and make them play the game so we need to assign a player prefab if i hit the little search box and go through my assets well let's see if i can find player go to assets and search for player player one there we go bam so i can select player one but look at that it just cleared right out so it didn't actually select it even though it looked like it was going to if you look down here right at the bottom hopefully that's on the screen it looks like it is it says that the network manager player prefab must have a network identity the problem is that i'm trying to assign a prefab that's not set up yet so let's go into the setup process for making a networkable prefab we need to go into our player so here's our player prefab and right now i don't think i have oh i do have some override so i'm just going to apply my overrides which are just positional stuff to it and then we're going to go open up the prefab mode so i'll just double click on it or here hit the little arrow and open up prefab mode now on the prefab it said i needed a network identity so let's collapse all of these fields here or all of these components and add a network identity they start searching and it should show right up now i can save this off go back out of prefab edit mode and let's try assigning it now so i've got my network manager i'm gonna go into my prefab folder and get the player from there i don't want the player that's placed in my scene i'm gonna get this prefab right there and assign it and now it actually shows up and works well kind of works so it should at least spawn that player now whenever i connect so let's test this out by just taking our two players that are in the scene and just disabling just want to get rid of them but i don't really want to delete them so i'm just going to disable them kind of move them save it off and hit play and see what happens by the way while we're playing if you guys don't mind hitting like subscribe and sharing all that stuff really appreciate it alright so we host and look at that we got a player that shows up let's see if i can move the player around that i can even move the player around so a player spawns and the player is movable looking good so far right all right so we'll stop playing save again and i'm going to do a build and run and let's try it network all right there we go so we've got our scene up here i'm gonna let's see i'm gonna make the unity editor of the host so i'll go in here and hit play and start hosting and then we'll go in and we'll join with a client now i'm in here with two characters let's see what happens when i move around so this one is moving around both of my characters on the the client scene here and on the server one i can move them around too but they're never moving together if i go over here look i move this one they're moving here i move these ones they're moving what's actually happened is the spawn two player ones and they're still using that same player's movement setup so we need to actually make the movement synchronized now and make it more controlled so that only one player or one oh yeah well you can only control your own player essentially so that you're not controlling both players so let's stop playing close this out and drag this back over here and then let's um let's see what do we want to do i think we want to go into our player script now so we need to actually change the player script to make it so that it's a networked behavior instead of a um instead of just a mono behavior so that we can control who can move it around and who has ownership and control over it so stop playing we'll go into our player script so i'm just going to select the player expand out the player script here and just double click on it to open it up all right so here's our player script and the change that we're going to make to it is pretty simple we're going to replace the mono behavior here with a network behavior so just go up here and i'll put a network behavior with the same behavior spelling that monobehavior uses hit alt enter and it's going to ask me to add a using mirror statement up there so that's enough to make this into just a networked object or a network controllable object and if you're used to mono behaviors remember that mono behaviors have a whole bunch of built-in properties and methods that you can use and override network behavior is kind of the same if i hit f12 i can just go into the definition and see a whole bunch of things like is server is client is local player is server only all kinds of different things down here i'm not even sure all of the different options here but we can override the way that it serializes data deserializes data and i think quite a few other things right now though all we care about is looking at whether or not this player should be controlled by the local system so on player the first player the host it should be controlled by the server the second one should be controlled only by the client that connects to it and the third one by the client that connects the third one and so on so we're going to add in a little check around our movement and aiming code to check to make sure that it's the correct player so all we got to do is go to our update and here we'll say if input dot no not if input if is local player then we'll do that code or run this in fact let's change it so if if is local player is equal to false return we could also just add a not local player but i'm just going to leave it like this so if it's not the local player we won't allow control over it anymore if we go in here and we hit play i should still be able to move my con my player around when i spawned him because he'll be the first one on here let's try it out hit play and i can move this one player around looks good i'm not sure why there's a second player there it was a little bit odd um what was going on there let's hit play again post and i got a player oh my other player turned on that's why it was uh something just flipped that script on i think there's something in the code that was turning it on so actually i probably just need to remove that from the scene in fact let's do that so i'll stop going here i'm gonna delete out my other two players that shouldn't be in there and then save it off so you did see though that i was able to move it around i think so let's try i'm going to build two do a build connect two and then make sure that each one can only control its local player and see if we get the network players moving or not so go to file build and run because i already have this scene set up as my only scene okay oh and let's do it okay so we're in we'll hit play there seemed to be a bunch of arguing in chat so just killed it no need all right so we hit play and we can move around this player and let's go into the other one and we'll join with the second player and now i can only move around this player so second one only moves around its player first one only moves around its player but nothing's synchronized the players still aren't moving together they're not moving on each other's scenes so stop playing and add the next step next step is still very simple notice that we haven't really had to write much code or do much at all yet so we'll go into here and see how easy it is so go to player and we've got a network identity let's hit add component i i'm going to actually go through the list here and just give a quick kind of a preview of what all is in there so there's a lot of stuff in here if we look at the network options under add component you see there's a network animator network discovery some logging stuff a lobby manager all kinds of things um oh man and they're just off the screen the ones that i want to select so i'll collapse these down a little bit more the one that we want though is just this network transform if i use this it's actually going to just synchronize the position and rotation and scale of my object so i can click on that save i'll do a file save project because remember this is on the prefab and prefabs get saved when we save project and then i'll do another build and run and let's see what this does now it has a bunch of settings for optimizing the network throughput and the position or the the throughput and the the speed of how how often it's synchronized and stuff we're not going to play with it too much how much you need to adjust it and what you need to do there is going to depend dramatically on the type of game that you're building but our game should work fine without making much adjustment there at all so i'm going to go and hit play and we'll start a server and i can run around looking good all right then we'll go into the client and we'll join as a client and move around and well i'm not moving there let's see if i'm moving here i'll get my other client up there we go and i'm able to move this one okay so partially working right this player the host is able to replicate its movement data down and the client is not so i think i know why that is let's stop playing and go check actually i'm going to leave it running real quick and just go check so i believe there's a setting on the player yep there it is on the network transform the client authority option if this isn't set then it's controlled um its movement won't be forced by the client so if the client moves around it won't move on the server it won't move anywhere else buying authority means essentially let the client be in charge of saying where its own player is this is usually what you want to have a lot of time some games will not have client authoritative movement most games tend to have relatively client authoritative movement but then they do some extra checking to make sure that your your movements are actually legitimate and realistic and that you're not cheating them so they might not know for sure that you actually did the movement but they'll know whether or not your movement was possible in our case though we're going to just check client authority because we really want our clients to be able to move and we don't really need to add any detection now when it comes to client authority on actual actions like shooting things blowing stuff up you never want to allow that but for movement it's generally it's kind of a default and a common thing because you want to have nice snappy smooth movement and then you kind of smooth it out over the network for everybody else all right so let's stop this client and then we'll do another build and run real quick with our client authority on so i've got it on on my prefab i'm going to make sure it's on for the prefab do a file build and run one more time and we'll get it going oh getting thirsty here oh thanks for all the super chats by the way guys i got it kind of off the screen so i haven't been seeing it nearly as much but try to keep an eye on the chat a little bit as i flap through the stuff unfortunately like with the mirror stuff it's somewhat not fresh to me or not new to me or something new to me i mean so i get a little bit engulfed just staring right at it all right so start our server there and i can move around looks good join our client and i can move around look at that it's moving on both of them i think it's looking pretty good and this one's moving on both of them as well you might notice though that like watch when i move that dude's not animating right and if i move this guy that one's not animating and that's because the movement code that's doing the animation is actually being bypassed so i'm gonna stop we'll go fix that and then let's uh we'll figure out what the next step will be so stop playing i'm gonna go into our player and the player is actually setting up its movement using the animator so the animator here in move with controller sets a run boolean which is just a true or false parameter to true if our movement magnitude is greater than zero so if we're moving it's true if it's false it just sets it to zero so this code needs to be run kind of regardless of whether or not we're a local player right that's my thought on it but we don't have this movement stuff if we're not a local player so why don't we just get the rigid body and get the velocity of that and use that instead so i'm going to go into my network shooter just double check that my player has a rigid body on it let's see yep it's got a rigid body right there so i'll just use the rigid body component's velocity instead of my movement vector here i'm going to take this line of code right here on 36 i'm just going to cut it and move it right up here so we'll say animator dot set bool run and we'll just set it to our rigid body dot velocity which doesn't exist yet the rigid body isn't cached yet dot magnitude so let's create that i'm going to turn awake into a regular method instead of an expression body so just switch that back and we'll say underscore rigid body equals git component rigid body save that off and generate a field alt enter and generate fields should give us a field right up here and i'll just delete the extra private keyword that i don't need so that should make him start syncing up the animation or making the animation not really network sync but we already know that we're moving so let's see if that works let's try it out go in and i'm pretty sure this isn't going to work by the way the more i think about it our velocity yeah our velocity value isn't um we think about because our we're not using the rigid body to move we're using the transforms translate to move so we need to sync it some other way actually you know what i don't want to do this i'm trying to think i kind of want to use a sync bar even though i i feel like i shouldn't um we could also just cache and check to see if our position has changed and then set it to running or something um i'm gonna oh this is a tough one i'm trying to decide to add a sync var here let's try that i haven't actually used the sync bars before so let's try it out we add a sync bar for bool running and then we'll just set it to whether or not we're running and then here in the move we could set that sync vars value to this let's see running equals and we'll switch back to the movement. magnitude and i think that'll work i'm not sure though it's been a little while since i tried the uh the syncfires oh network animator is a great idea too i didn't even think of that let's try this out real quick and then we'll jump over to the network animator component that's not one i've actually used on here so i think it's probably a good solution for it so here we've got let's see where's our player our player's got it's variable um let's try a build build and run and see if it works and then again if this doesn't work i'll just switch over to the uh network animator let's see how that works and what that's like and then we get into spawning bullets spawning enemies and blowing them up over the network and all that stuff right now we're spawning them all locally and it's not going to work network so i'll make sure that i get into that stuff too all right so we'll hit play host and client and let's see move around oh nope it does not seem to sync not sure what i missed there let's see what am i missing here on the sync far let me just look it up real quick so you need to use a mirror sync bar and we just pull it up sync far needs oh does it need to be public let's see players plus data sync far is applied on start client also up to date on cloud client sync firestone use any type it's when the value of the var changes so you don't need to track it um in this example so let's see i'm just doing a real quick check here so when we set it oh it's because we're not setting it on the client that okay got it so i i realized the issue right away so okay no i didn't realize the issue because this should still yeah there we go so look at this here it is i get it now let me explain this too because this is an important thing and it's gonna be um an issue for everybody and i'll look into the network animator in a second but i'll talk about the syncfire thing for a second because you might have thought that this was gonna work and without me thinking it through i thought it was gonna work right so right now i can move around this player and the second screen here animates you can see the player here animates even though i'm controlling it from this one if i move this guy though the other one doesn't animate so the client or the server here isn't getting the animation what actually happens here is these sync bars only get sent from the server out to all the clients if you set a sync far on the client it is not going to set it on the server and to everything else for that you need to use a command and a command is set up let's see if we i'm just going to go through it let's just do it real quick so command is set up essentially like this you give it a command attribute make a public method and then you set some data there and it's going to run on the server let's do it so we'll go over to unity and on our player let's add a command that sets our move with um or sets our running state again this is probably not the ideal way to do it i don't really want to be sending a command every frame that's going to um set whether or not i'm running but i want to show how to do it because i think it's an important thing to show how to do real quick and it's really really simple to do so add a command and here i'll say public void set run pool run so just it's going to be a true or false on whether or not run is true let's actually get this up to the top of the screen there and then here we'll say underscore running equals run actually let's rename that to running make our variable names set look good and then here we'll just call set run and we'll pass in our value of true or false there so now what will happen is we'll call this method and if we're on the server it's just going to call directly into our setrun method so if this is our first player it's going to call set run set it and then the syncfire is going to pick it up if it's on the client it's going to send a network message down that says hey set this variable to this value of course setting it every frame is probably a terrible waste of network bandwidth and not a great idea but i want to do it anyway just because um why not what we really want to do is maybe just set this only when it changes or something so let's try it out let's give it a go we'll go into unity again and do a build and run go back over to unity stop playing file build and run and we'll push this out all right oh the question about do we need to put cmd for the name that's a good question does it require cmd for its command names i hope not that would be really annoying but it might be the case i assumed that the attribute was enough okay so we'll find out in one second let's hit play and see all right so we're in we'll host and move around look at that and then we'll join a client and run around look at that it's working so now our synchronization is good to go so we've got simple state variable synchronization and some simple movement and oh also rotation i guess i didn't really spin around much but there you go you can see me rotating and spinning on both of them and rotating and spinning on the other ones by the way when i want to go over to hooking up to the network it's extremely easy all i have to do is uh put it onto another device and change the ip address make sure that i open up the network so all right let's see what's next um what should we do we've got movement we've got animation kind of working i i did kind of want to play the um with the network animator but i think i want to dive into something else like either spawning enemies or maybe spawning bullets or something else um i'm inclined towards spawning enemies first so that we have something to hit the bullets with um everybody kind of agree or anybody got any thoughts on that in chat you guys wanted to see bullets enemies or is there something else you'd rather see first maybe we could even do the bombs first i guess but i feel like the enemies are probably the right way to go and it gives me a chance to take a drink while everybody says what they want oh okay okay so there were some questions too in chat real quick i just wanted to answer about sync bars and commands so the sync far is essentially one that you're going to set on the server so you set it up on the server and it's a variable that whenever it changes on the server it'll update to the client and then the command is the thing that you send from the clients over to the server you got to also remember though that when you do a server like a host and server client thing at the same time like i've been doing you're running as both of those you have a client and a server and it kind of works as both so when you send a command from the server to itself it's actually going to run that kind just like it would if it were a networked one but without having to go over the network all right so let's go through the um the enemy system now we're spawning enemies right now our enemies spawn with this spawner and if i do let's do another build and run and let's see what happens if i turn on the spawner actually i didn't turn on the spawner first i'll turn the spawner on we'll do a build and run and we'll see um what that looks like when we have enemies spawning i think i'll turn on let's see which spawners do i got let's turn on like these two one there's zero and two so i've got these spawners here by the way let's take a real quick look at what these look like so this spawner object is just a cube and it's or it's really just an object with a box collider and a mesh render and a spawner script on it it's got spawn points on it so it's just an array of points and that's what all of these cubes are i left them visible they're just the different points that the enemy could spawn at so i can have a little bit of variability and then it has a set of enemies down here as well in the spawner script i'll open up the script and you see that all it does is check to see if it should spawn and if it should spawn it spawns it determines if it should spawn by checking to see if the current time is greater than or equal to the next spawn time so whenever we should spawn again and then when it spawns it just chooses an enemy so we we reset our spawn time we pick a random spawn point from our list of spawn points there or our array of spawn points and then we pick a random enemy from our array of enemies and we instantiate the enemy at the point not a whole lot to it but it's not going to work over the network is it let's try it we do our build did i do a building run already nope let's do a build and run oh thanks for super chat anything else going on in chat while i build and run oh on building your own so it looks a little bit of chat about building your own libraries too if you're curious about building your own network libraries it's it's a lot of work it's it's definitely a complex thing to do and it's the thing that you probably want to do once your game is starting to get to the point where you need it where the existing solutions don't work or if you've already built enough network games that you know that any existing solutions aren't going to work but for most games the stuff that's out there will usually handle the kind of game that you want to build all right so let's host our server and then let's see we got some enemies coming in here oh man that's a lot of them and let's let's join in on a client here so you can see well obviously they look very desynchronized right the enemies here are nowhere near what the enemies here were and i died really really fast so i'm going to turn off death real quick and then try it again well i'll turn off death and then let's just start making them spawn over the network instead of spawning on their own so i'm gonna turn off death mostly so i don't keep loading into that scene as as it plays like i don't want to get touched by an enemy and it dies so here i'll just disable the death by going into our enemy script and just commenting out the part that loads the scene when we get touched by the enemy it reloads our game right now and then let's go to our spawner and figure out how we're going to instantiate a enemy over the network instead of instantiating an enemy on every client because right now it's spawning for every client client has its own set of enemies they're totally unrelated to the network so we need to go to our spawners first and our spawners need to do some work so that they only spawn on the server so we'll open up the spawner script and i guess if we're not on the server we probably don't need to run the update at all so we can say if is server oh we can't check if we're on the server though because it's not a network behavior so we'll change the model behavior to network behavior hit alt enter and add the using statement for using mirror now we can see if is server is false then we'll just return because we don't want to run this code on the client that should be enough to prevent the client from seeing any enemies spawning at all that's not going to make the game actually work though because we want the enemies to show up on the other side so the next thing that we need to do is actually spawn the enemies over the network that's i was surprised just how easy it is to do with mirror because there's really not much to it all we need to do is get an instance of our enemy here and then call a network server method on it so i'm going to rename this enemy instance right here where we choose an enemy to enemy prefab just because i want to say enemy enemy equals this oh i want to spell enemy right though there you go so now i'm instantiating an enemy into enemy using this prefab name and i'm going to take this enemy object and i'm going to spawn it over the network to do that we do network server let's see if i can spell it right i cannot spell today network server dot spawn and we pass in our enemy dot game object we have to give it the game object now there's obviously one other step that i haven't done yet which is to make our enemy into a networkable game object i think if i go in and hit play we'll probably see an error let's try it though because i'm kind of curious what the error message would look like we jump in we hit host and oh and the enemy is showing up nope but we have some errors here so there's no network identity on the spawner no network identity on the spawner and here there's an exception on line 49. so i need to add a network identity to my spawner so stop playing add component network identity there we go it's added let's collapse some of this other stuff down we got a spawner with our network identity and it should be on the server only right this is a server only object i don't want it spawning on the client so we'll save hit play try this out host and server and well i still have errors because i didn't add it to all of my other ones but you do see some enemies are starting to spawn so it's actually starting to spawn the enemies let's see well let's fix it on the other ones and then see if it actually spawns them over the network so i'll select my other three spawners at a network identity we'll save oh let's check server only do a build and run so we can get a client up and then see if um if the enemies spawn over the network or not or if there's anything else we need to do to them probably is another step my guess is that our enemies are going to need to be set up as network objects with their own network identities but let's see what happens if we don't do that so we jump in here we'll hit play i'll start hosting on this one right here and join as a client and let's see oh look at that i've got error messages right here saying that the cute things have no network identity and i've got nobody showing up here you can see the characters getting pushed around by the bad guys so look at that this guy's getting kind of moved around a little bit strange but let's go fix it up now let's stop playing and let's go add our network identities to those moving dudes so go to our prefabs go to the enemies folder select all four of them i'm going to collapse all of these scripts and we'll add a network identity now i also want this thing to move around just like it does with the clients or just like the players do so let's add a network transform as well as far as animations go these dudes literally just animate non-stop so i don't think i need to do anything else there i think i can just save this off and do a build and run and we probably are going to have enemies walking and moving in sync let's see if it works maybe i missed something but i don't know i know oh you're welcome for the kind of somebody to mention in the dapper snapper dino in here cool oh hey what's up man i see you know just saw the chat i don't know i think specifically nice to have you in here that was a fun chat yesterday too if anybody missed it go check it out we did a live stream uh we'll go check it out after the live stream just talking about game development and stuff it was a lot of fun all right so here we go we'll host and we will join and i'll move around look i got enemies spawning oh no what let's see my enemies are moving here on the client they are not spawning or they're moving here on the server they're not spawning on the client what is going on here i must have missed something let's take a quick look i'm going to pause and find an enemy let's go select one it's got a network transform should be spawning it's let's see oh it has two network transforms uh oh i've got an issue here and it's got two network identities that seems like a problem i think i might have messed up here let's go back into my project view and go double check these yep how did i do that how did i double add a network id let's remove and remove and double check these guys this one has two so somehow i added extra components whoops i i literally i don't remember doing that maybe i uh i don't know i don't know what i did that's all good though we'll fix it up so we've got our components fixed up now i think we got a network transform this guy this guy this guy oh i know there's one other issue watch this i know what's happening sorry there were two problems here one is i had extra components on there and two is that there's actually to spawn game objects over the network there's another requirement and it's one that i completely just spaced on this registered spawnable prefabs option right here on our network manager so on our network manager we need to define which objects we can spawn over the network i'm going to hit plus and i'm going to hit it four times and add in all of my enemies that i want to spawn over the network because that's something that i need to do here we go we'll go assign them all and save one more time do a build and run and i think we should be good to go now actually spawned some guys there again it was double checking that didn't have two of the components on there and making sure that they're actually set up as spawnable prefabs let's try it out all right we're in almost so we'll host move around a little join look at that i've got enemies moving and they're chasing it's a little bit stuttery here but they are chasing and moving around and it seems like it's kind of working so there's something else going on though um if i stop playing and go back into the code and let's take a look at our enemy the way that it works there's something that people aren't going to notice if they haven't gone through this code before or they didn't write it and it's that our enemy right now let's zoom out a bit it's synchronizing its position over the network it's being moved by the server every frame the server is saying hey go set your destination the nav mesh agent on the server saying hey move to your position and then the transform sync is saying hey here's my new position but look in the update it's also doing the same thing on the client and then the sync is happening on the server and sending the position down so it's actually moving on both places and then fighting back and forth between the client and the server so i'm going to do here on the update to say if is server is equal to false return uh or read run i guess let's let's change that to a return and then we need to make the is server work because that's not there right now because this is not a network behavior but if i go to the top with control home and change mono to network hit alt enter and add the using statement it should now show up so if i scroll down now my is server's pulse is good and it should only move on the server and not do the movement on the client so now we have server authoritative npc or enemy movement let's try it out go back into unity and we'll do another build and run see if it works and then uh yeah i guess we'll just run around hopefully you have some enemies there by the way i also just wanted to say while we're waiting thanks everybody for showing up it's kind of crazy having this many people just hop into a stream and watch and join in and and commenting and talking and stuff it's a blast and so kind of amazing also if you guys don't mind hitting like and get it up to like a thousand that'd be awesome too like but yeah okay let's try it out now i figure i gotta say something while it's running all right so i hit play run around and it looks good and i join on my client and there we go now the stuttering has kind of disappeared it's now nice and smooth it looks pretty good i can run around blast enemies and it's uh somewhat working i mean to be honest it feels a lot like the game is kind of working there are some issues going on here though a lot of the things are still very client authoritative in fact we may start to see things get a little bit out of sync if we if we play enough and we kill things right so the reason for this is really just that our players right now are dealing with all of their bullets all the collisions and everything else on their clients so let's take a look at that and see how we can maybe fix that up so if we go back into unity and we go over to our let's let's take a look at the let me think what do i want to take a look at you guys want to do the destruction of enemies over the network or shooting with bullets over the network first because we can do either one we could either start spawning bullets over the network and make sure that that's all synchronized or we could do a um a thing where the the enemy destruction is actually synchronized that if we kill an enemy on one it's always killed on the other end it's not just like there are two guys moving around but you have to kill it on both and the bullets just have to link up or sync up looks like there's some some interest in the bullet stuff so i think i'll start with that let's do bullets first and then we'll do um do enemy destruction next so to set up our bullets remember we needed to add them to the network manager let's do that first we'll add a registered spawnable prefab plus there go into prefabs and find the bullet prefab and then we'll take a look at what the bullet prefab actually looks like oh i can't assign the bullet prefab because it doesn't have a network behavior so let's go take a look at that network or that behavior now or that get my word right let's go take a look at that prefab the bullet prefab all right so the bullet prefab is pretty simple it's a sphere let's just drag one out here into the scene it's a white little dot it's got a bullet script on it a rigid body the rigid body doesn't have anything locked it's set to what continuous mode and anything else on here nope a collider and a renderer very very simple object i'm going to delete the one from the scene go into again delete the one from the scene go to the prefab and let's add a network identity let's type in the word identity because apparently i cannot spell network anymore all right so we've got a network identity here it should not be server only we want bullets to replicate on the client and server go back to the network manager and assign the bullet there we go now i want to go over to my shooting script or my gun script let's go take a look at that look at our player see that it has a gun script on it and the gun references the bullet prefab has a delay and the speed that it launches the bullet at go take a look at it so our gun script here it's very similar to our other ones and then it just has a simple update with a if can shoot then shoot this is pretty much it so if we can shoot we'll do some shooting um we have a direction vector here i'm not sure why i'm going to delete that because it's not used and then we have a queue of bullets here that we use to pull the bullet so if you've never used a pooling system before let me really briefly explain what it is when we spawn objects in unity or in a lot of things we create an object and allocate some memory for that spawn it up create the thing it has a small performance hit and reclaiming that memory afterwards also has a small performance hit over time it can build up into a big performance hit so one of the tricks that we'll use is to build out a pool or just a big collection of things that we can reuse so i'll build out like a thousand bullets and just grab a bullet that's already spawned and used or spawned and available and ready use that bullet and then when it's ready to be destroyed instead of destroying it we're just going to put it back into that list here we're not doing a thousand we just do however many we need when we run out we create a new one and when are done with it we re-add it back into our pool so let's take a look at how the rest of the bullet shooting works and then we'll set it up to be a network side or network setup for it so in our update we check to see if we can shoot let's go look at it so can shoot just checks to see if we this essentially should be should shoot let's rename it in fact should shoot because it's not really can it's a should we should shoot whenever our time meets the next time then we call shoot we've set our next shooting time so that we can have that delay that which kind of refreshes automatically to our current time plus the delay we get a bullet from our bullet pool so this is the part that's using the pooling it checks to see if there's anything in the pool if there is it grabs one using the dq which just gets the first one available sets it active if not we create a new one and we set its gun so we say hey bullet when you're done hitting something you go blow up reset yourself to this gun's pool this gun has its own pool of bullets that are reused come back to it other than that well we've got the bullet here on line 27 we set our position to go to that shoot point which was at that point right at the end of the gun or a little bit below so we don't go over their head we use the rotation even though i don't really care about the rotation actually i lied we use the rotation because right here on the next line we set our velocity to the objects forward times the bullet speed so that's how we launch out our bullets so all we need to do here is spawn these objects over the network and then set their velocity right let's try it out so we get a bullet we spawn a bullet well let's see where we spawn it we spawn a bullet right here on line 42. so when we spawn it let's add it to the network server say network server dot spawn and let's zoom in a bit and get this nice and big and we'll pass in our bullet dot game object now i need to add a using statement because network server's not showing up so i hit alt enter and add the using mirror statement which should be right up there there we go save that off with ctrl s let's go back down where was my code there it is so i think that should be enough to just spawn my bullet over the network right so my bullet should get created and appear over the network set guns probably not going to get called over the network if i need to call it from somewhere else or something but that's not an issue i want to deal with right now and i don't know if they're going to launch out let's see save it off let's go back into unity let's make sure that our bullet has that as a network identity right and then let's do a build and run see how this goes all right almost done and we'll hit play in unity i'm gonna start a host i probably should turn off all of these all these enemies because they're just going to be distracting and then i'll start my client and well things are kind of happening you might notice that some weirdness is going on right so let's take a look my first first thing to notice is that over here this player or both players seem to be shooting out just fine but on the client the server or the client isn't getting the server's bullet movement so the bullets are just kind of sitting still you can see they're just kind of stacking up there if i move that player away might be able to see it it's really just a stack of a ton of bullets here let's stop the client here i'm going to stop this real quick i'm going to turn off my spawners and try again so here i'll stop the spawners hit play and i'm going to make oh and i wanted to make the other one the host but let's do it this way again so make this the host we'll join in as the client i just want to show that one more time i didn't get all my spawners off oh i turned them all on oops i did the exact opposite of what i wanted to do i think there all right um what do i want to do here i'm trying to think i want to make it so those bullets fly out in the right direction so let's let's go through the process of fixing that the reason that they're not flying actually i want to show it first i want to show that they're not flying out and why they're not flying out so let me do a build real quick i'll make the uh the little one the server and then we'll make our unity client the client instead so that way we can see what's actually happening there before we fix it because i think that understanding what we're fixing makes a lot more sense than just fixing it so we'll hit play go in we'll make this the host plus client and i'm i'm in here i've got my guy spawning and i got this other player here and let's take a look at what's going on here so here you see that my bullets oh geez they're not even spawning they're really they're just kind of like they're acting very weird let's go take a look at what's going on on these bullets i wish these spawners were off i thought i'd turn them off all right so on our bullets let's see can i see him let's get down here what i'm looking for is this stack of bullets that was just turned on and spawned somewhere there should be a whole bunch of them that are just like stacked up on top of each other i think they're they're right around here yeah right around this shoot point i guess nope these ones are actually coming out where are those bullets somewhere there's a bunch of bullets that are stacked up i give up on trying to show them because my spawners are messing with me but the problem that i need to fix is really to make it so that the rigid body gets synchronized here if we go look at our bullets let's go to the prefab go to the bullet object right now we have that network identity and remember on the enemy we did a network transform to synchronize the transform position and rotation for the bullet since what we're using is the velocity and we're just setting a velocity on it what i want to use instead is the network rigid body they use this and it's going to synchronize the velocity you can see right here there's a sync velocity option which will synchronize the velocity as it changes so if i use that and i do another build and run i expect to see that my bullets start working much closer to how they were before so they just kind of go flying out in fact let's take a look at the bullet script real quick while i do a build is there anything in the bullet script that would move our object anything different i don't see anything trying to double check there's nothing in here that's going to fight with our movement um against our rigid body that like we had with the player moving around all right so we've got an instance of our game here and we'll hit play start it up we'll make this guy the uh oh let's make this one the host and we'll make this one the client run around and now i've got bullets um somewhat somewhat better it looks like they're starting to come out but they're acting a little bit strange still and if i scroll down here my guess is that they're never getting pulled right i'm just constantly are they ever getting reused oh no some of them are getting reused so some objects are getting reused but you see that i'm getting a lot of bullets just spawning too let's go over here so that all of my bullets are hitting the wall theoretically if all of my bullets are shooting at the wall i should never create any new ones let's see if that's happening oh it looks like oh did it happen yeah it looks like it so we're not actually spawning any new bullets that's good let's see if we move it down here again our bullets firing out pretty smooth now something strange going on there let's see just doing a quick double check because there may be less to do here than i'm thinking i need to do i can run around i can shoot things and i can kill everything get this okay let's stop playing and let's go through a couple more code changes that i know we need to make so let's see should the bullet be using a network behavior was one of the questions that's a good question i don't think it actually needs to so i was thinking perhaps maybe it would but i think if we're not using any of the actual script stuff from it then we don't actually need it to be a network behavior we're not using anything on it like our um is server or setting a command or a sync file or anything but now that you say that and i think about it i remember that oh yeah we are setting our gun here so if we do our set gun call this should probably be done um let me see how do what does our set gun do our set gun is used to determine our scoring that's right and for our polling so i want to make sure that our set gun message gets sent over the network so that when we spawn a gun it gets sent to all of the clients as well i guess it makes sense right we spawn it let's let's go double check actually you know what hold on i'm thinking more on this on the gun ah i lied okay here's the problem yep the server is not i like it looks like bill just called it out thank you bill um the server should be spawning these bullets right now if i leave it like this i could cheat i could totally totally cheat right here accidentally or on purpose because right now my should check shoot should shoot check happens on the client so what i could do here let's let me replicate this really quick and show um why server authentication or why server authoritative stuff is so important because i'm glad that i made this little mistake here because this is great let's do a file build and run and i'm going to make the the windowed one the cli the server right i'm going to make that our server and imagine that this is like you know this is the real server we're out on a real game this is being run by people there's a lot of people on it right so this is our server we're playing on it and we're playing with some people or whatever and we host and we shoot at this speed we're like a normal player we're not a cheater right and then our buddy comes in and our buddy is a hacker our buddy says hey where's my player character let me go select it real quick let me uh turn down my delay because i don't like shooting so slow like you i don't want to be a noob and then they run around and their fire rate is suddenly like this right and they're like oh wait what happened tell you a flag or something right this is why bullets need to be spawned and controlled by the server you need to have one source of truth so that hey maybe the server is going to cheat sometimes the host is going to cheat but if you have like a really dedicated server or a dedicated server like an actual server process then that's less of a problem so let's see how we can change it so that it's spawned by the server and not cheatable very very important thing i'm glad i made that mistake i'm glad bill called it out too good stuff all right so what we need to do is make it make our bullets get spawned only on the server so to do that we got to go to our gun and we should only shoot or we should really only run our gun script on the server so i'll add a little check here and say if is server is equal to false return that means that our gun needs to be a network behavior so replace mono with network alt enter oh we already have a using mirror statement up there perfect and i think we're good how did i already have a using mirror statement up there that's strange shouldn't have that there i must have added that accidentally all right so we've got our this server check here and if it's not on the server we'll return so now only the server will fire bullet problem here of course let's see if i do a build and run is that i expect that i'm not going to see any bullets spawning um out of the client let's try it out so we do a build and run and we play oh and by the way if somebody's mentioning that on the client on the server you didn't see that happening that's true because we're not actually replicating those bullet messages out so we're killing all of the things locally they're blowing up and stuff but it's totally desynchronized so another good point too all right so let's host join as a client and um now i've forgotten what i was like oh my bullets that's right so i'm spawning my bullets bullets are spawning out on the server but look at the client i'm getting like random every now and then i get a bullet sometimes i get a bullet there we go i got another one a couple more are shooting out i'm getting some weirdness on the client right the bullet is definitely not spawning the way that it's supposed to so let's stop oh that's weird why are all these enemies stop moving too that was a little bit weird i don't know what happened there i'm gonna stop playing though and we're gonna go back in and we're gonna make it so that our bullets actually will get so they spawn over the network already right let's see let's go double check that they do spawn over the network but i want to assign them to a gun over the network as well so we're going to go into our bullet script again we're going to change this to be a network behavior and then we're going to make the set gun method oh i need to add a using statement alt enter and add a using statement from mirror and i'm going to make this a command so that our set gun method gets called on the on the server as well so if we run it from oh actually are we ever running this from the server or from the client i don't think we are are we let me think for just a moment we run it i think thinking through this real quick i don't think that i actually need to do that okay let's see what else do we need to do here sorry guys i just been blabbing so long thinking out loud i'm gonna figure this out let's see we're spawning in the enemies we're shooting the bullets let's see setting up that i'm gonna go through here real quick and just think for a second so we're spawning them i'm getting the bullets spawning out shooting out why are they not hitting right let's let's try let me do one more build and run real quick and see what this is looking like by the way if anybody has any feedback thoughts as we're going along feel free to just type them in and chat i'm going to take a look at it in a second it's kind of warm in here all right did my build work oh my build did not finish what happened with my build he had an error um oh unsupported type gun oh okay so my gun here is calling gun on it oh because the gun can't okay that's right this this doesn't actually need to be called anyway but the problem here the reason that wasn't working is that the gun isn't set up the gun isn't a thing that i can pass in as an id i would actually need to pass in the network identity of that object in fact here let's let me show you what that would look like so i change gun to network identity and you'll call this network identity the reason for this is we can't send just a random object through a command over a message we can send a lot of things i think there's a whole list of the serializable stuff that you can use but to do just a random object there's extra work that's involved but we can just do a network identity and i can do it like this so i can say that the gun is equal to the i believe it's network let's see i cannot spell this okay network identity.gameobject.getcomponent of typegun so that should get the gun from whatever network identity object is there so that way i can pass over the network identity and still get the gun out of it now if i do a build here ctrl shift b i expect to see an error saying that i can't pass a gun in as a network identity but i could just say hey get component network identity oh well nope the bullet get component i lied there i want to use the bullets no no i want to use this thing's network identity all right thinking out loud okay so we want to use our guns network identity and pass that in and then we on the other end we use that network identity and get the gun back from it all right that should work that should allow me to play now make sure that i can hit the play button and it all works all right hit play and post and i can run around okay good spawning and shooting just wanna make sure that's working oh okay here let's fix this issue first before we go any deeper we've got an exception right here when i hit something and it says that there's a no reference on the bullets on collision entry so i'll double click on it and oh look right here okay it's actually that our gun isn't set okay so our gun check that gun set that i did there just did not work that's interesting so i must have screwed that up let's see what i did wrong okay so look right here the first call doesn't do anything okay let's go look at this again we call set gun on it and we set our object right here i'm going to add a break point and debug it real quick and see what's going on so i hit f5 at a break point jump into here allow in debugging for this session hit play i may have to look back through my test stuff to see what i did different here and mess something up but we'll figure it out so hit play and any second now it should start up grab up my extra set of code just in case too while that loads up all right set up our host and we go blow things up and why am i not getting the exception oh i'm not hitting my break oh my command isn't getting called a bit if we go to set gun i bet that this is never getting actually called let's see why isn't that getting called add a breakpoint here there we go and we get our network id so bullet.setgun gets called and it's not actually let me see what i did on my other side real quick because i don't remember what i did here oh i remember i'm an idiot ha okay but the problem here is that uh i got okay so i i'm doing this all wrong i don't want to use a command i want to use a client or pc sorry if you're new to multiplayer development and uh or you're like me and just completely forgetful um commands go to the server client rpcs go to the clients when we're running from a server that's also the host and the client it's going to go from that server side of it over to the client side so i want to be sending a client rpc not a command also i don't necessarily need to use the network identity i forgot i can just use the game object and use game object and then here i could just put in game object dot get component actually let's call this gun object instead of game object just because i don't want to accidentally reference my game object component that is better that's closer to how i should be doing it and then we'll change this to just be the um game object just our game object of our gun there we go let's try that one more time this is the downside of not practicing multiple times before you do a live stream right all right so let's try this out um nope won't be making one of those all right let's hit host server and we run around okay let's see if i can kill there we go bad guys die now all right let's try building a multiplayer version again so go back to build build and run get two clients in there and see if we should blow some stuff up all right so some people were asking about uh dots um the data oriented tech stack uh you could definitely use it to build a multiplayer game but it's not where i would go right away if you're not familiar enough with unity building uh normal multiplayer games i wouldn't dive into dots multiplayer right away it's a bit harder a bit more complicated and you're probably not going to get any of the benefits out of it unless you're really pushing the boundaries anyway if you haven't already seen the boundaries and normal networking stuff you're probably not going to know you're not going to be anywhere near pushing those edges so let's host and let's join with a client and see if i can oh move this player around start blasting the bad guys looks like my bullets are now flying out pretty good on the client and the server let's go check out this one too okay so over here oh what where am i not seeing my bullets right that's interesting okay so over here there's still something going on on our client it's not working on the server it's good our bullets fly out and i think it's all it looks like it's kind of perfect on the server right the client however is all kinds of messed up the easiest way to debug this is to stop stop and then go back over here and make this little window into the um the server itself and and then make this one our client so hit host so our one that's built or standalone where we can't see the code and can't see the objects is the server and then we'll join it and then see it looks good over here and it's buggy over here not yet you'll see that it's going to start to get buggy over here so we're shooting out bullets we're shooting out bullets this character seems to shoot out forever though notice that his bullets because they're not getting pulled never go away if i move him up though let's grab him and just move him up here eventually i expect to see his bullets yep there we go his bullets are no longer working on the client either let's go take a look i'm going to just scroll down here and go look at some of the objects here so you see that no new bullets are getting spawned and none are getting turned on so the issue here isn't super obvious if you haven't been through the code before but let's go look at the gun and see how it works and see if we can figure it out so in our get bullet call or in our shoot really we get a bullet by calling get bullet and we either spawn one which remember when we're shooting straight off into nowhere they kept spawning so we instantiate it we spawn it we set a gun that seemed to kind of work right but when we get it back from a pool we checked it here to see if there's something in a pool we dequeue it we set it to active and then we launch it this part is only getting called on the server right so the part that sets our object to active doesn't get called on the client at all so that's probably the thing that we need to fix right if i look over here at my notes i believe that we're just setting it how did i set that i don't actually remember where i set that up to make it launch out let's see now i'm curious where i saved it before when i tested it out oh that's right i just added a method to it that makes sense let's do it that way so when we shoot our bullet out right now we set the bullet's transform position the rotation and the velocity right here in the gun what we could do one alternative way to do this and i think it's a relatively clean way it's the way i went with the first time so it seems like a clean way is to just make a launch method that does all of this on the bullet then i can send a client rpc so that all of the clients say hey here's my data i'll go do this stuff and then i'll synchronize later so we'll take this little bit of code and i'm going to change this into a bullet.launch i'm going to say bullet.launch and i want to pass in let's see i need my shoot point here i'm going to need my transform forward and my bullet speed maybe i could combine this into a velocity value and then use my shoot point as the first parameter so i'll say um actually you know maybe i'll do let's do a position a rotation and a um and a velocity instead of a transform i think that makes more sense that way i'm just passing over vectors instead of transform references so say bullet.launch and we'll give it our um oh actually i could even be grabbing these from the client i'm not going to though because i want to pass them as parameters but i could grab these from the client when i launch them too so we'll say player let's copy this i'm going to take this part right here paste it in as the first parameter copy rotation paste it in as the second parameter and then copy this part and paste it in as the third parameter let's split those into separate lines here so that it's really really obvious what we're doing all right so our launch method it's going to be on the bullet it's going to take the position the rotation and the velocity and it's going to set this data with that so i'm going to cut this code just do shift delete put it on my clipboard get rid of these two lines here and then we'll generate the launch method so i'll hit alt enter and generate a method f12 to go to it and then paste in what i had i'm going to replace internal with public just because a lot of people don't know what internal means and then we'll get rid of the bullet keyword right there and then i don't need position and actually i don't need the player.shoot point part so i'm going to delete them both but i'm going to hold alt click here drag down to the end of the dots so i can get them both and then just hit delete make it simpler and let's replace this code right here with this vector3 which should probably be named velocity let's rename it to velocity lost city velocity there we go and then we'll paste that in i need to add that client rpc attribute and save all right let's try that out i think that that's going to make our bullets now launch properly on the client and the server and we'll have an almost completely working networkable game let's try it out let's close that window go back into unity stop playing do a build and run one more time and also if everybody just hit like subscribe or whatever real quick while we do a build and run really appreciate it or just share the video go drop it up on your facebook or instagram or um there's probably something newer now that i don't know about too there's all kinds but yeah you can drop it up there i appreciate it all right let's try it out so our 3d shooter starting let's make um i'm gonna i'll make this one the host this time i'll join with our client we'll run around start shooting stuff and let's see if uh if bullets start to dry up or if they work forever oh here my bullets kind of looked like they were drying up i think they were hitting somebody oh okay so it didn't work now the reason it didn't work is because i left out one little bit of code so right now we're spawning it we launch it at that position but we're missing one final thing in the launch so we shoot it we get the bullet we launch it go in here we get the velocity and all that never actually turn the object back on so our pooled objects or the bullets they get turned off let's go look at what that looks like real quick so when a bullet gets hit or hits something where is it on collision enter here you see that we set the game object to deactive when we go back into the pool that's what we need to do we just need to turn it back to active when we launch it so i'm going to copy this paste it right up here i'm gonna do it right here between when we set the velocity and set it to true it doesn't actually matter where put it but i'm gonna put it right there now if i do a build and run my pooling issue should be fixed my bullet should fire off and i should be able to kill all the enemies then we just need to synchronize up the enemies or the enemies states and their health and their death and stuff so let's do a file build and run and we'll start up yeah there is um on those bullets by the way they do have a death timer of i think 10 seconds after 10 seconds they just reset themselves too so eventually we'd still see the pooling issue it'd just take a little while because the bolts have to all kind of fly off on their own for a while all right so we host and we'll get in here as a client and we will connect all right so we're in as a host and a client and i'm in here shooting stuff just blast things with lots of bullets like the offset on my bullets definitely a little bit weird i probably should adjust that a little bit more but it looks like it's working like the bullets seem to be flying off indefinitely not having any problems um synchronization looks good let's try it out here i can move i can blast enemies everything seems to be relatively solid the only thing that's not completely synchronized and you can't really tell because my network latency is so bad is the enemy's death that technically we could possibly maybe get things out of sync enough to to disconnect our enemies so that they're not being hit on both players right so that like one client thinks that they're hitting him and has killed the enemy the server may think something different especially if there's just a lot of lag on that client so we need to fix that up and make sure that the enemy's state is synchronized and it's i think probably one of the last big steps here so stop playing and let's go back into our enemy code so the current enemy script let's see find the enemy it doesn't do a whole lot with the network behavior in the update we just check to see if we're not on the server and if we're not on the server we just don't do anything outside of that we can take damage so we take damage on the client and the server and then we deal with collisions but we actually don't do anything on collision so when we take damage here this is happening on the client and the server and really probably should only happen on the server right this isn't the kind of thing that we want our client to do we don't want our client actually killing enemies adding score and turning things off so i want this to be a server only thing so i'm going to make this into a command instead instead of it being well it should be a command probably not the bullets should only be doing the collision on the server so we shouldn't even be sending a message to it so what we're going to do is make sure that this never runs on the client so say if is server equals false return we should never take damage on there let's go into the bullet too because on the collision enter for the bullets do we want to do anything do we want to take damage and kill the enemy probably not we probably don't want to call this code at all right we don't want to actually be calling in to check it now do we want to deactivate the thing and add it back to the pool if it hit something probably i'm kind of inclined to say that we do even though the um the bullet may not have hit on the server and it may not be completely resynchronized actually no let's just let's just bail out so if in on collision enter if we're not on the server we'll return this will only run on the server the bullet the bullets collision handling should only run on the server i think that that makes sense for me now if we try it again we do a build and run what i expect to see is that the client is no longer able to kill things or at least not in the same way and that the server is let's see what happens though see how it actually works out all right jump in here start building and there we go we'll have our 3d shooter little window here be the host and hit play here be of client and then let's see if we can uh see our enemies die and blow up so i shoot at enemies bullets go through them all on the client let's look at the server though on the server the even the other player seems to be killing him so like our other player right there i'm pointing at my screen here but the other player there is able to kill on both but over here on the client nothing actually dies because all of our death is handled on the client so we need to change it so that death of npcs or enemies is handled on the server to do that we'll go into our enemy script and then when the enemy dies we really just want to replicate out that death right so when our guy dies go into the enemy script like i said and not just say that we're going into it but when this guy dies we want to say hey he's dead um kill him on the server and all of the client so let's go through that process real quick to kill an enemy we just need to go through our check right here where we spawn them and when we spawn the prefab missile i suspect we talk about to kill an enemy they take damage we hit them at an impact point we give a player number so we can award some points and a damage amount we decrement their health by that amount get rid of that extra space and then we spawn a hit prefab whenever the thing gets hit so this is that little impact particle first step let's spawn that particle over the network so say var hit instance equals our instantiate this is going to give us back the game object that we're spawning here i believe hit prefab is just the game objective in fact let's call that out game object and make it really explicit because we're going to say network server dot spawn right after and we're going to spawn that hit instance right there and because it's a game object we can just pass that in so now we'll spawn the prefab whenever we take a hit on the object whenever the the enemy takes a hit there the next thing we want to do well let's see we want to play the source on there so here's the thing if we spawn these prefabs over the network and then we play the audio source we're going to be sending one message for that one message for that and probably another message for exploding it um so let's just change this up to say take hit and take it we'll do an audio source and spawn the prefab locally and then we'll do another one that spawns this thing as a separate message so i'm going to change this a tiny bit i'm going to actually delete that and delete out my spawning i'm going to take this chunk of code right here hit alt enter and extract it to a method and let's call this take hit on clients or on client and we'll pass in that impact point we'll make this a client rpc there we go and that should work right that's now going to just send the message out so that we will spawn the thing and play the audio source in both cases i think that's better now we'll do the same for destroying the enemy so instead of uh calling it take it let's just call it dion client so i'll take this little chunk of code alt enter extract method die on client i like this much better than spawning the prefabs or spawning the particle systems over the network copy our client rpc paste that in there i'm going to get rid of these two private keywords because i don't need them and save it off everything here i think should work i mean the score system stuff's not going to work perfect because our score's not set up yet for networking but everything else i think is going to work so what's going to happen well we'll take damage we'll call take hit on client which sends that client rpc the remote procedure or remote what's the p stand for i've lost it something call um how did i lose that word and then we'll call dion client on there if the player if the enemy should die let's try it out all right we'll go back in we'll hit file build and run one more time and see how this works all right so lots of chat about building mmos by the way building mmos a lot more than just building network there's network stuff there um that's a big chunk of it but there's a lot of other stuff that comes into building an mmo the network part is huge it's like a full game worth of development on its own but then there are like five other games worth of development that you got to do building an mmo a lot of stuff involved um all right so we connect we'll connect again and let's shoot some enemies i see him sparking i see him blowing up i think everything is actually synchronized let's check it out blow up some more bad guys looks like it's kind of working yep i can blow them all up everything is synced and uh the deaths are synced to my player just kind of ran off the screen let's see what's going on with the scores too so i look at my scores i got 14 hundred fourteen thousand five hundred i think i just end up with um player one's score right now or it might actually be i'm getting the combined score that's right that makes sense because i'm getting the score for both of the players all right to blast this guy yeah all right so let's stop playing take a break for just a second and talk about uh what we're gonna do next oh yeah well building your own network layer is uh yeah it's not not a realistic thing for 99 of developers or 99 teams some people are going to build their own stuff and it makes sense there are definitely cases when you need to build your own stuff most games don't need it most games work fine with built-in networking systems with a little bit of tweaking and tuning but if you want to build like crazy stuff you want to push the boundaries you may have to actually build your own stuff when you're building out an mmo you're probably going to have to build out your own stuff you build out like a a giant game you even like something like um a battle royale game you're gonna have to work heavily on the networking stuff and and really use it somewhat custom system you're not gonna be able to slap in mirror and have it work magically right you're gonna need something more complicated but you don't want to start there don't start by trying to build out your network system build a game and then if the game is fun and it makes sense then get the networking good and perfect it a lot of people will spend a lot of time just building out systems and never build a game and then it's it's demotivating kind of demoralizing and and i i don't prefer it i'd recommend build the game first um get it working get it kind of good and if you need to adjust the networking you need to make it better you can always tweak it tune it and replace it later and you'll have more experience you'll get better at it don't don't dive in and just expect that your game is like don't try to build a game that's going to scale to thousands of players right away on day one as especially not as your first thing should be you know like your third or fourth game where you're getting up to these crazy scales of stuff okay um so let's see what should we do next we have a couple options there's a bomb that's set up i don't know if i showed it but if you hadn't seen the previous streams there's actually a bomb that we can drop that little red thing that's out there blinking let's hit post and watch if i hit the button right there i can drop out more of them and they actually well they explode and blow up enemies if they end up near them let's see if that one does it gonna hit anybody oh it's gonna be too far from everybody but ideally the bombs should end up near bad guys and then blow them up there we go you saw those two guys blow up right actually that probably requires a little bit of skill to line the enemies up and get them to push the bomb around or sit on it right so maybe i'll set that up next you guys have any preferences or recommendations over something other than the bomb or should we just make the bomb networked and then oh stack up and watch that death boom okay yep i think i want to do the bomb but what do you guys want to do after the bomb should we do score system or do you guys have some other recommendation or idea or maybe like player color changes or something else so i'll set up the bomb and then um while we're going through that process if you have ideas for the next thing you want to see put them in chat and we'll figure out what the next one we're going to pop in there is so oh look at that bomb is really cool it just blows up all the bad guys so we'll go into our project view let's go take a look at the bom script now because this is one of the few things that's not networked our bomb isn't networked and we also have a bomb dropper script that's not networked i'm going to actually look at the bomb dropper first and then we'll look at the bomb script itself because they're both relatively simple the bomb dropper has a bomb prefab that we reference a delay for how often i can fire it and of a release velocity for how fast it fires out when it just kind of rolls forward um it references the player for i believe adding score and then the next drop time for when we can use it again has a should drop bomb which just checks to see if we can drop it so if our time is greater than the next time and we press the button for our player so we pressed bomb one which is just set up right now leave that circle here set up on in the input settings and then we drop a bomb by setting our bom time spawning the bomb using just a standard instantiate call and setting our player setting getting our rigidbody and then setting the velocity to our forward times that release velocity we can launch out out of speed let's go look at it real quick go look at our player here in the prefabs it's got the bomb dropper script on it got this bomb dropper component right there you see i've got a delay of five and a release file city of 10 so it goes out at 10 meters a second rolling forward stops pretty quick but you get the idea so we want to make this bomb dropper drop bombs over the network first step of spawning an object over to the network is go to network manager and remember we need to assign it as a prefab but it also has to have a network identity so i guess this is the second step sorry everybody lied to you again so if i try to take that in there and work go to step one first step was actually adding network identity and then i'm going to add a i'm going to add a network transform too because i'm already here let's speed that up and just add the transform as well leave those both on there and then i'll go to my network manager and assign the bom from my prefabs folder make sure i don't get the one that's in my scene all right save that off go back into our code and what do we want to do i guess we just want to do a network spawn of our bomb right so when we create the bomb add another line here we'll say network server dot spawn and pass in bomb dot game object this is the thing again that makes it replicate over the network so that the object gets replicated to all of the clients so hit alt enter and add the using mirror statement up to the top and the error should go away a bomb should now spawn on the clients as well as the server i'm going to go to the set player method though because this is a method that's going to need to get called over the network and well we're going to need to add in a client rpc just like we did before so go here and add the client rpc attribute and hit alt enter and add the using statement but i also need to change my bom into a network behavior start using these client rpcs and um yeah i think that's about it right so we'll add network behavior or replace mono behavior with network behavior so we can use the client rpc actually i'm not 100 sure if you can use client rpc outside of it i assume you can't but i haven't actually tried it all right so this should allow me to pass in the player number and that should be cacheable or serializable an integer or a number or string text numbers all those kinds of things tend to be serializable and work just fine in here same with like vectors like in the previous section where we passed in the or is it the velocity the direction the velocity and the rotation um why did i do velocity and rotation i don't remember okay so oh actually i do remember but it doesn't matter all right so now we're spawning a bomb over the network and it i think it should work let's double check that our bomb has a network identity yep let's try doing a build and run and see if we can spawn the bombs and have them roll out over the network and if anything else weird happens oh you know what i just thought about too in our bomb dropper script our should drop bombs should only be true if we're on the server our bombs shouldn't be well wait no that's not true huh oh let's go through it let's try i was sitting here thinking out loud while i'm uh waiting for a build and i think i'm thinking through the wrong thing so here we'll start up our server we've got our server there let's start up our client and here i'll run around and drop a bomb and oh i got a bomb on both right so here i'm shooting it but i'm dropping out two bombs let's try it again so drop out two bombs it's like here do i see the two bombs i don't even think i see him so here i'm gonna drop out a bomb on the client or this is the server right the other one was the client that's the problem okay i see here's the server i drop out a bomb and i see it on both and then if i move the client up here i drop out a bomb and i see bombs from both players but the bombs aren't going over the network right so it's spawning out there on the client it's spawning for both players but it's not going over the network and on the server too notice that it spawns for both players i had two bombs every time i hit it so let's fix the spawning for both players and then let's make it work over the network right we'll stop playing it shouldn't spawn for both players by we'll just adding in a check in our bomb dropper to not check if we're on the server though but to check if we're the local client authority right so we want to say if is local what is it let me let me oh this my bomb dropper needs to be a network behavior change that and then we'll say is local player so if the word of the local player then we should be able to drop bombs if we're if this is a player that's like somebody else's player and again local player means like this is the player for the client that's connected so for the first one the host it's going to be that the first player that's in there for the second one it's going to be true for only that second player the one that you're controlling so is if it's not the local player so if is local player is false we'll just return out of update we don't want to do anything with our bomb dropping for the the other player we only want to do it for our own player that should fix a lot of the issues already so do a build and run i expect to see that at least we're not getting two bombs launching out any so let's hit play and we'll host right here and join as a client all right so run around drop a bomb i see only one bomb flew out let's see i'm gonna drop another bomb on this on the client here if it only only one bomb flies out do i get a bomb here though i didn't see anything let's try dropping a bomb here so on the server i drop a bomb and i see it roll out for both right let's try it one more time there we go i see it roll out for about this is the server and then this is the client let's move it where's my client player again let's drop a bomb that rolls down this way i don't see anything let's try it again i've got just off the screen there drop another bomb like that so it's not replicating from the client to the server the reason for that is that the server is the only thing that can really spawn these objects the client can't just go spawning objects over the network it can't do a network spawn to do that that's well that's where these commands come in so remember earlier if you were watching earlier i accidentally used a command when i wanted to use a client rpc but i wanted to send something up to the client from the server commands are used for sending things from the client to the server and that's what we want to do here so when we want to drop a bomb now on the server or on the yeah really on the server from a client we can't just call dropbomb and spawn it into a network instantiate instead what we want to do is to say when we should drop the bomb so when the timer is met and they've pressed the button then let's just call a command on the server to try to call to try to drop the bomb so to do that we'll go in here and we'll say hey command this is now a command and we'll set our next drop well this will almost work i want to explain why this doesn't work because i feel like i'm about to miss it here so this would almost work just doing this we would say hey should we drop a bomb on the client so imagine i'm the client and i've met my timer and i pressed the button right i send the message up to the server the server says hey the next drop time is this and does the bomb dropping and all that i'm the client again right next frame i hit the button again and i send the message again because hey look the next drop time never got set right so next drop time doesn't get set on the client right here and i'll be able to spam out this message as much as i want i'm not doing any server side checks to see if the things should fire off let's try that out let me see we'll show you what this looks like because this is a very common networking mistake or bug that happens where we're giving a little bit too much authority to the client so do a build and run and we're going to spawn out a bunch of bombs and uh see how fast i can spam out bombs on the client ignoring the rule but the server will be completely following the rule because it's setting its value itself so try it out i'm going to make my unity instance the client so we can go into debug mode and see what these variables are looking like all right so here we go we've got a host and i'm going to hit play we'll join as a client oops i might have double clicked there hopefully that worked okay good join in as a client and then i'm going to hit the bomb button and look at that i can just keep firing out bombs just spamming them off oh actually now i lied about it because the server actually allows it too because the server isn't doing any checks either to see if we shouldn't be able to do bumps if i try to spawn a bomb on the server though it's fine because it's actually setting its value now let me show you real quick again what the problem is because i i explained it but i think that showing the code here or the variable here makes more sense too so we'll go a little bit deeper we'll select the player here and go to debug mode here switch over to debug mode in the inspector and if we look at the where is it at the bom dropper script look at the next drop time it's always zero no matter what i do no matter how many bombs i fire off it never gets set so we need to just fix that up and make it so that it's server authoritative instead of client authoritative there are two different things that we want to do first is that we want to make sure that the client doesn't keep sending messages if it doesn't think that it should be able to drop a bomb so the client should keep track of its next drop time as well but we also want to make sure that we do some checks on the server so that if the client does decide to cheat it can't so let's do the checks on the server first and then we'll do the client side checks second client-side checks are really just a performance improvement where the server-side checks are a cheat improvement so when we do the drop bomb we'll just say hey if uh time.time is greater than or equal to next drop time or actually let's say if it's less than then we'll just return we just won't do a drop bomb so we'll just double check the time on the server that should be enough to prevent us from cheating on the client but it's still going to allow us to spam out messages let's try it out so we'll go back into unity go build and run and we'll start hosting all right so here we are we're in i should be able to drop bombs on the server there we go and then we'll join on the client real quick and just start spamming out bombs so i'm hitting it as fast as i can and they still don't spawn out any faster let's see they spawn out at the normal rate but i am still sending up that message because my client doesn't do any checks so every time i hit the button i try to send it or try to spam that out if i wanted to add client-side checks to it to prevent that i just go back into our code right here and then what we could do is say hey when we do the drop bomb message or when we send the drop bomb message add another line right here to also set our next drop time on the client so we just copy this line of code right here and paste it in so that it gets set on the client and on the server oh i accidentally killed my braces there that would be enough to prevent the messages from going overboard and just uh getting spammed out every time the player hits it so that the player will only send the message when the client actually thinks that they should drop the bomb and then the server will get the message and double check that hey they actually should be able to drop it let's see i don't know if there was much else i wanted to get into here i think we've got quite a few different systems and really my goal here is just to make sure that show people how to build out a a relatively simple multiplayer game where you can take a single or a simple like offline game where you got two two sets of controllers and you'd be able to just build it out and run it over the network and play it online when you want to play if you want to use this one online this specific one i think the only downside right now is that this if i remember right this kcp transport doesn't work on webgl build so if you wanted to do a i was going to do a webgl build with it but i would have had to change that and i kind of ran out of time but if you want to um just use this and do a build you can definitely just create your own game and you know launch and connect over the network there's a couple of little things that you need to do though so one of the important parts well here let you know let's let's commit this to collaborate and i'm going to sync it on my laptop and connect from another system real quick to show what that looks like and what that process is because it's something that yeah there's no building a multiplayer game is fun and all but if you don't connect over the network what the hell is the point right if you're just playing a window and a window you may as well just have a local multiplayer game so let's say that we um added multiplayer i'm gonna check them all and publish oh no um i have an issue because i pre-synchronized all this stuff so i'm not gonna be able to do this i can uh collaborate [Music] can i fix this up i think if i sync it it's gonna let's hit it we'll hit sync and i'll just do the merge and i will accept my stuff i hope either that or i'll overwrite it and it's fine because the code is pretty close to exactly where where we're at now i think so let's go through it though so what we'll do is we'll set it up i'll start playing here and then i'll start playing on the uh there we go yep so we'll take if you haven't used collaborate by the way source control system built into unity it's not the most powerful one i still prefer git but out of all the source control systems is probably my number two now because it's built in it's easy to use and for solo simple projects it's really really simple and just quick to go with so i would recommend it for anybody who hasn't used collaborate before at least try it out so i'm going to hit use my changes for all of these and the reason that i'm having that by the way is just that i'd already pre-committed some other stuff where i'd gone through and tested it out and i had to revert back and then redo this stuff again and then recommit it so i want to make sure that it's all synced up all right so now i've got that committed and collaborate on there i'm going to go over to my laptop which is just right down here and then we'll just sync it up so i'll go over to my history and it says that um i made a change nine seconds ago so i'll hit update i'll pull that in and then we'll do a quick hosting and i'm gonna run around with this controller now not this one it's actually hooked up to my laptop and see the game running over the actual network so to get it going over the network while that one's synchronizing i'm going to hit play and show you um something somewhat important here notice this part right here that says client localhost that's really really really tiny this part right here i need to change this so that it's running over the network instead if you just set it as localhost here if i stop playing and go select the network manager you'll see it if i set it as localhost it's not going to use the um the ip address bound to the network card so when you have a network card it's got one ip address usually it's got one ipv4 address and be like 192.168. something dot something it's usually one dot something but it depends on your network so what you need to do is make sure that that iep address is available to connect with if you just have localhost in there only local connections can work and you can't connect over iep so you have two options there you can either figure out what your ip address is by just typing um you can open up a command window run cmd pull this up and you can type in ipconfig and then you'll be able to see your ip address right here you can see this is mine right now just switched over router so mine's a dot two instead of one because i haven't taken down the other router yet i've got that there i could put that in but i could also just go right here and put in 0.0.0.0 that'll usually i'm pretty sure it works the same in mirror as it does with unet but that should make it bind to all ip addresses here now there's still another step because connecting over the network also requires the firewall ports to be open so you need to make sure that you open up the port that you're connecting on which right here is 7777 through your firewall or if you're just testing it out and you feel safe with it you could disable your firewall for a minute turn it back on but generally you want to figure out how to do the port opening and then you're going to need to do some port forwarding those are things that it depends a little bit on your router and your your network setup in windows you just open up the windows advanced firewall add in a port or port setting and or firewall setting and firewall rule to allow it for believe it you need a udp and tcp but i'm not completely sure on this one um i have them both though just enabled for for the port so we'll hit um what do i want to do i want to hit play and see the number show up there it should be just zeros again instead of the local host and then i'm going to go over to my server or my laptop go to game view mode i'm going to hit play and assuming that my server my game starts up i'm going to try to connect okay so here i'm going to hit host and just connect over here from my client i'll put in my ip address which was that 192.168.2.7 let me let me go back into open up that command window again yeah it was dot seven so it's ip config at the command line so dot seven i almost put a dot six there and we will hit client and connect and look at that it actually connected so i could i saw it all appear on there but i'll show you on the other the other direction too let's get these controllers back on and see if i can move around there we go i got one player moving around and let's see if the other player will move there we go both players are moving around over the network now so now they're both just literally moving yeah over my my local network so i'm gonna stop the host and i'm gonna switch it over so that the other one is the host real quick just so you can see that actually disconnected the other one instantly so i'll make the laptop the host and then i will try to connect to it so this was let's go to maximize on play play play i'll change this to 192.168.2.6 because this is the ip of the other device connect and it won't work the reason it won't work is because i haven't opened up any of the firewall stuff on here your server needs to be the one with the firewall ports opened up don't do that you're not going to be able to connect and again i would recommend testing with the example scenes before you test with your own stuff if you want to do a network test don't do it in your own code don't do it in your own project because if you made a mistake there you might be mixing it up and it's hard to know whether or not it's a network issue or something in your stuff you want to make sure that you're debugging the right thing so do it in the example stuff first get that working over the network and then switch over to doing your game stuff in the network i'm going to stop playing over the network and i think i'm just going to take a little break here and just take some questions see what people want to know about if there's anything else and then maybe we'll code a little bit more maybe we'll just do a little bit of q a and stuff after i don't know so um anybody got any just networking general questions and stuff oh there definitely is going to be so it's the way that it's set up right now there's not any prediction there's not any um smoothing of the networking or anything so it's not going to be very good the the current network setup needs a lot of work to get better um but it's just kind of up and running so we can move around right and also my laptop is overloaded right now so it's not not running very well it's doing a bunch of other stuff at the same time okay let's see any kind of questions mirror for a super smash bros type game uh i don't know i would assume so if you set it up right i mean a lot of the things that you need to do so when you get into really fast competitive games where you need to have things that are super well timed and stuff you're going to be sending a lot less network state and network synchronization you won't be using things like network transforms that send the position and rotation constantly instead you'll want to synchronize usually on input and you want to have like a a simulation that matches everywhere no matter so you could replay that same exact input and have the exact same game play constantly if you see a lot of those games like that that have replay systems and they're a lot of time that's how they work where they're not really storing off like all of the action and the movement they're throwing off all of the input and then replaying that type of stuff i don't know if smash bros does it that way i'm not sure how they do it but uh you would probably you need to come up with something to make it nice and quick and a system for it but you could i don't see why you couldn't build something like that using mirror unless there's some other technical issue with it that i don't know about but i don't i don't think there's anything um anything in there that would prevent it really let's see will you create a stream creating a moba style lobby based on mirror um i don't know if i'll create a lobby for moba stuff or not i just didn't seem like that kind of thing that a lot of people would be very interested in but maybe uh when you put the command on top of the drop bomb function it made that function run from the server yes that's a great that was a good question so if we jump over here the command in the bomb dropper script right here so when i added the word command here that literally made it so that this call right here now doesn't just call this method it sends a message calls the method on the server which is kind of magic yeah i'm very used to like the or at least i was very used to the old-school ways of having to build up a message package up that message send the message read the message and then deal with it now with the way that mirror's set up in the way that it does the weaving you just put the command attribute there and it's really just doing all of that for you it's wrapping up all of the tedious stuff that is easy to mess up over and over for you so you don't have to do it all right let's go some more questions i just got here but are using ecs no i'm not um ecs is cool but i'm not using it for this right now i don't think that it's what i would recommend for people getting started with networking i'd say that if you're building a network game that you can't do without ecs or dots then um i'd jump over to that but it's not where i would start with building network games for sure and it's definitely not what i would recommend people build their first network game with because you got to learn dots and ecs or i mean dots and networking and dots is constantly changing not this mirror system is super stable super simple you can really just think about the logic and not so much the networking with the other systems you really need to be thinking about the networking and that's great once you're at that point and you're you know at the point of needing to optimize your network but when you're starting out having to think through how that networking works and not just being able to deal with logic i think is um it'll slow you down a lot and probably not for a lot of benefit again unless you really have a use case for super high-end stuff did you do any client side prediction um no not yet that's not something that we've added um and it may just do another video on how to do um client side network prediction because a little bit more complicated but not that much and probably do like a good like step-by-step tutorial on that let's see anything else um is there an asset oh yeah there is an asset that's true the um so on the topic of dots uh the guys that made mirror they i saw they do have an asset for dots i haven't actually looked at it though so maybe it's just as easy in dots as it is outside of dots but it's still i wouldn't recommend if you haven't built a network game before and you're not already using dots i wouldn't use it as your your first starting point let's say use that when you're at the point where you need the performance um or if you're already using dots for stuff if you're already using that in your project then hey that makes sense but i wouldn't make the jump just for that let's see um okay talk about connections network connections photon versus mirror um i don't have any real strong preference i have a lot of friends who recommend mirror over photon again i haven't used it in a production environment yet i haven't had a need for it we've all always used custom stuff but um or photon long before mirror existed but i think that when people ask me i would say i would probably look at both but look at mirror especially around the cost side and i think that there may be some performance benefits but to be honest i don't know i don't know if photon has any performance hits in unity anymore or not it's been way too long the biggest issue that i had with it though was more just on the the cost and pricing model there with mirror you know as long as you're hosting your stuff it's free with photon the licensing was somewhat expensive so it's a little bit harder to get started with if your brand oh i guess they probably have a they think they have a free tier that's not too hard to get started with and probably really easy to get going with you so i don't know it just depends on which one you want to use but i don't think there's any strong reason to use one over the other outside of cost but i could be wrong what did you guys use for unity mmo stuff um a mix of things a variety of things uh initial super early prototyping just with photon but obviously not for real stuff um switched over to a couple different network libraries um used u-link for a while and now custom stuff um and it just depends um a lot of games i would say that almost every mmo that i've ever seen has some custom they're going to have some custom networking stuff because i mean mmos are just a weird problem where you have an unknown number of players doing an unknown number of things that as you control it you're adding constraints to the game that start to piss people off so the more and more you limit it the more you anger people so you have to figure out how to allow as much as possible without um no killing stuff without making the game unplayable so it's uh it's always very custom stuff in there it's interesting i'm thinking through um talking to a friend not too long ago about one of the old oh i don't know if i should talk about some of the old game code and some of the old network code that that we had in some of the old stuff and um [Music] the the code there gets so i would i wouldn't say it's confusing it gets a bit confusing and complicated and scary to touch so we had code in one of these projects that was released and live for a very long time that nobody even realized had a bunch of debugging stuff just inside the code that was slowing it down and i think that it had code in there it had said it had notes in there saying hey make sure you delete this the stuff is for debugging and it's performance hit um make sure we delete it before release and of course as always it's a comment in there by somebody who either forgot about the comment or left the team or left the company or did whatever and isn't there anymore and then other people go look at the code and they go i don't want to touch the networking and break the entire game i don't know what the side effect of changing this is or deleting it i don't know if this comment is even true so it just stays in there and it stayed in there for years until uh way way way later somebody's trying to debug something found a found this bit of code and just went for it fixed it i think it was todd that fixed it which doesn't surprise me as a badass programmer always fine it was really good right so he i think he was the one that ended up fixing nothing i heard about it i was like yeah it's not a surprise well one of the many many reasons that i hate um commenting stuff and code for like things to do later just do it right then don't don't leave it um it's scary stuff stuff like that happens all the time and then literally over the life of a project just killing performance probably cost who knows how much in in network bandwidth the loan and cpu time and stuff so tons of cost anyway uh let's go into more questions um to be doing more mirror tutorials um i think so i think i'll probably do a couple more um just network game tutorials where i go over maybe like a couple different like mini games like start scratch um and maybe do it down in an edited version where i can show you exactly all of the steps involved without any of the debugging stuff as well to go through like a couple different little multiplayer games and by the way at this game right now i've only connected two players there's literally no thing stopping me from connecting five players ten players i think i put four in there because i was planning on just connecting four but nothing that can stop me from connecting any number of players i don't think i have a max number set or if that's the setting on the network manager now let's go check take a quick peek max connections4 okay i lied i set it to four players by default but i could turn that up and just connect any number of players and run them around all right let's see what else we got for questions here um do i get a return my dedicated object slash tag i'm not sure what that means um let's see just reading through here so oh slash slash notes slow down the game not i mean obviously comments don't slow it was not so i said it wasn't the comment that slowed down the game it was the code right after the comment that said hey delete this stuff before we go live instead of actually deleting the stuff before going live um that was the problem it was the replacing the action with just replacing a comment with a hey let's go do this later that nobody ever does because when you put a comment in there like that people get scared if they're not the person that did it and they don't know and can't ask the person they're going to be very terrified to go in and change the networking code of a live network game right especially low level stuff that says it's been there for years and says hey it's a debug thing okay so um some people want to see the debugging stuff that's good stuff is this something like in your beginner course um the networking side is not no it's much more the beginner course is more focused around understanding what all of the code means how the code works things like abstractions interfaces and just a lot of 2d development as well a lot about 2d stuff and really focused on the coding side not nothing on networking at all i'm thinking about adding a networking component to the architecture course so i'm going to send out an email sometime really soon to find out exactly what people are most interested in so it may end up being a part of that um but i don't know networking stuff isn't in there though it's it's primarily just um it's all code it's programming programming programming learning how to do the code and how to interact with all of the different systems i use source control and that kind of stuff and then testing the knowledge against that okay so changing the networking code of live game is one of those yeah what could go wrong situation totally apparently nothing went wrong so i'm glad to hear that uh he fixed it and it it worked out but uh yeah it's terrifying right they're just like looking in there like oh yeah we could just delete out this bit of code in here and see what happens huh let's see um any other questions in here um did you pick up many assets in the asset store sale i didn't get much really um this last couple days i i got a bunch of stuff last week and the week before so i've been um kind of burned out and it's not really that there weren't things to get it's just that i have a bunch of things that i still haven't had a chance to go try out yet so i've been trying not to get too much down the rabbit hole of digging in and looking but i probably will end up looking more this weekend too and just see what other kinds of cool stuff is in there for the asset store by the way anybody's curious i love grabbing art hacks off of there i grab all kinds of art constantly to pull into to demos and games and to like inspire me for stuff but then i also like to grab code packages too and just look at them see how they work what they're doing in there and pick up some cool tricks and techniques and stuff i find that to be fun okay um i guess we need to remove somebody real quick from chat but he's got some problems and we'll continue on all right well anybody got any uh other questions let's see would you try a 2d multiplayer game in the future for oh that could be interesting maybe that smash bros idea would be um a good way to go like a 2d um fighter or something as a as a game there for multiplayer networking because then we could go through the the performance side of it um how to get that quick enough but there's also not a lot of mechanics that we have to deal with we just get two players bouncing around punching each other or shooting stuff at each other or something yeah that could be a fun one and something i think it could be um done relatively quick and there's not a lot of extra code involved to just be around the networking stuff i like that idea so i'm gonna um write that down and consider it heavily see i used to be working on online project bouncing bullets how do you handle dozens of physics objects over the network like fall guys does um well a lot of it ideally is having i'm not sure how fall guys is doing it exactly but having as little as possible be sent over the network where a lot of things are either predetermined or they're um deterministic so that we can launch the thing off and it's going to do the same thing all the time now for fall guys um i'm not sure how they're doing it or what they're doing but you got to use a variety of tricks and also there's interest management stuff so that you're only sending out thing updates at speeds that are like one of the things i know it's built into mirror but i haven't dug into it too much is the idea of interest management where you can essentially have like a player running around in the world so say i've got a player right here where my bomb is and he's getting updates of all the npcs that are maybe in in the crosswalk area every frame right maybe he's getting ever or every server frame maybe it's like 20 times a second or 10 times a second or whatever we're running at and then as it gets further out we can slow that down so maybe if they're way down here by the end of the road they're getting messages you know maybe at half that rate or a quarter of that rate and as they get closer the rate of those goes up so we can send less messages over the network um and still and deal with a lot more objects out there and there are obvious lots of other tricks and things but that's just one of the things that's already kind of built in the interest management stuff and i'm not sure exactly how mirrors work so that might be another good um another good video topic to go into too just diving into how interest management works and how you can do that so i don't know i'm trying to think through like exactly what i want to do there but lots lots of good ideas here also if you guys don't mind just hitting like and subscribe and share real quick one more time by asking questions really appreciate it i take a couple more and then i think i'll wrap it up for today and then we'll do another one pretty soon um based on i guess we'll see what other questions people have too whether ideas come in so if other cool stuff comes in me we'll do that see um i'm programming a network game through photon what are the pros and cons of photon versus networking price wise um well photon is using i mean photon is a network engine it's just a network engine and if you're using photon cloud it's a network engine plus hosting service where you're paying for the benefit or the ease of use there so that they spawn up the servers and they manage them you don't have to deal with it and um it costs right the cost adds up now i don't know the exact cost that you'd have to figure it out for your game like based on the bandwidth limitations the connections and stuff but in general i would expect to pay a bit more with a hosted service where it's all managed like that but hey maybe you find a crazy deal or they give you some hookup i'm not really sure um let's see hello how would you handle random values over the network send it to all clients or have some sort of pre-visible randomness generate great question um you could do two things you could either have a controlled random seed where you're controlling it and um determining what every random number is if you set up the random seed you're going to get the same number every time for the next one um you just have to make sure that you're calling it the same number of times on everything and get to get the same number um and which isn't too hard to do really you could set up multiple random number generators even if you wanted to or you could send the number over the network depending on how you want to do it so it depends on how often you need to do it if it's something that's not happening often you can probably just send it once if it's something that happened all the time then you probably want to be sending over the seed and then making sure that the usages or the things that are calling to get the number from that random generator are synchronized and if that's the case then you won't need to send over that number as well but it may be overkill there and you might just want to send the number instead let's see anything else here oh mirror uses a proximity checker component okay cool that that makes sense that's that's a good name for it yeah well i think we're kind of running out of questions so i think i want to wrap it up here go take a drink and um start prepping for the next thing i got another video um that will probably come out tomorrow that's going to be on um to interview game developers releasing a game i actually have two of these that are in editing right now so whichever one comes out first will come out hopefully tomorrow with uh two different game developers who both built unity games one of them is a networked game that's really freaking awesome and another one is a non-networked game that's going out to steam that's also really cool um and really fun discussions with them about the process of building the games um one of them's building the game full-time as a job like the main thing that they're doing another one is doing it just completely side side job like just in spare time building out a game and releasing it um over a really quick amount of time too so it's pretty impressive from some really interesting discussion so if you guys are interested in that stuff make sure you're subscribed or hit the alert or whatever just check back tomorrow whatever you want to do um that should be a lot of fun and if you have um just requests or i guess ideas other stuff that you want to see on the channel or for streams or whatever just drop a comment below um send me an email or whatever i try to read and reply to all of them as quick as they can and then um yeah i don't know i think that's about it just make sure you hit like subscribe share all that stuff i had a lot of fun building this out and i'd like to build some more stuff really soon quite a few different ideas um so having a blast thanks again everybody i just really appreciate everyone coming out here it's kind of crazy it still blows me away that hundreds of people come out and just watch and go through building games and have fun together so it's fun stuff all right um i guess i'll take a drink and wrap it up here say thanks one more time and again don't forget to like subscribe share and all that stuff oh one last thing i was gonna um i got an email i haven't replied to it yet i'm gonna reply after this about um the mug here it will be um available sometimes this is i thought it was hilarious little debug mug that cracks me up and um my new favorite cup so if you're interested in that i'll put a link down in the description too you can check it out um it's sorry i feel weird but i like the thing it cracks me up all right um thanks again everybody thanks for coming out appreciate it have a great day again
Info
Channel: Jason Weimann
Views: 62,801
Rating: undefined out of 5
Keywords:
Id: j2so32ZlMoU
Channel Id: undefined
Length: 133min 38sec (8018 seconds)
Published: Sat Nov 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.