How To Make A Multiplayer Game In Unity - Client-Server - Mirror Networking

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I wrote already on the comments of the video on you tube. It would be nice to have more content about proper Dedicated servers networking on you tube. Thanks for your tutorial :-) Cheers

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/satolas πŸ“…οΈŽ︎ Feb 28 2020 πŸ—«︎ replies

You can also use this prototype: http://move.rupy.se/file/fuse.zip

That I made before we made https://store.steampowered.com/app/486310/Meadow/

It's hardcoded to only accept fdsa/fdsa and rewq/rewq as users but it still works after 4 years!

The backend is open-source: https://github.com/tinspin/fuse

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/tinspin πŸ“…οΈŽ︎ Feb 28 2020 πŸ—«︎ replies

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.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/AutoModerator πŸ“…οΈŽ︎ Feb 27 2020 πŸ—«︎ replies

In the context of this video if we're looking at one of the players also being the server how do you deal with any potential NAT issues from players on outside networks ? Assuming that you don't want your future players to have to worry about port forwarding etc. I understand dedicated servers are an option but as one of the other comments mentioned tutorials for this kind of thing are uncommon.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/metsudo πŸ“…οΈŽ︎ Mar 05 2020 πŸ—«︎ replies
Captions
okay so hello and welcome back to another unity tutorial so this video will be the first of many where I'm going to be covering at different multi player topics using mirror I'll be explaining in the first part of video why I'm using mirror / photon and what mirror is and how it works that's what this video is mainly gonna be it's gonna be how to set up the mirror and explaining from a sample project they've got way you can host a server and connect to play a game of pong I'll be explaining the code behind and how it all works keep in mind this won't be a series even though there's going to be lots of different videos using this apart from episode 1 you don't have to watch any of the others in order this video is about the setup and explanation overview but then all the other videos will be standalone because I know on youtube people prefer standalone videos as opposed to you know me doing a series so let me know how you're liking the videos in the comments of each of the videos but yeah let's jump into the video so I split this video up into three steps step one we'll be installing mirror through unity through the asset store step two we'll be actually looking at a sample project and you know if you want you can go and look or leave a sample projects in your own time and then after we've looked at the sample project and had a little play around with it for step-free we'll actually be looking into the code and I'll be explaining how it all works let's get into it okay so step one what is mirror and why are we using it well I want to keep this part quite short because you guys can go and do reading about it but I did a poll on my channel and a lot more people a huge majority wanted mirror so that's one reason another reason is because there's no user limits so you'd have to worry about you know paying photon because you want more stuff mirror is completely free you just download it you can download it off github or you can download it on the unity asset store which is the way we're going to do it you can go look through the code you can debug into their code you can make modifications to it if you need to if you have any bug fixes you can obviously submit it to them on here you can talk about it in the discord server there's pretty good documentation for it there's good starter projects and guides everything I found about it is really good and the reason how I've picked it up so fast in the last few days is because it's pretty much identical to the old unity multiplayer system because it's built on top of it they actually took the old multiplayer system they fix loads of bugs they've added new features and they've just made it a lot better and they're constantly making it a lot better too so I hope you guys agree with my choice sir and just let me know down below how you're finding the videos one of the other benefits is that it's client-server so we're actually going to have code that is running on the server and code that is running on clients and server can be hosted by an actual person when they run the game so when you build the game and you can make maybe make a host button when you press the host button you actually run a server and a client and then when your friends try and connect to you they connect through your IP just like because if you are playing a minecraft server you're putting their IP address then you connect to that and then eventually if you want to have dedicated servers you can also do that too but for now we're not gonna worry about dedicate service and all that lot because that's a bit more complicated but doing your own hosting is super easy and we're going to be looking into this video with a sample project then in the next few videos we'll be setting up our own custom network managers you know making some other little simple things like spawning in players and whatever but yeah let's get into this video okay so over in unity I've made a blank project so just go ahead and make a blank project when github is backup because as I'm making this video it's down right now they're having some problems when it's up I'll be creating a repository for this so you guys can go download it and but when I do that I'll be ignoring the mirror folders that we will get in a second because there's no need for me to have them on github when you can just get them from the asset store yourself so you make a blank project okay go to the asset store and just search mirror and once you search mirror you want to go ahead and download and then import it into your project alright I'm sure you guys can Press that and do that once it's in your project you'll have a mirror and a script templates folder well ignore script templates that's just like you know when you right click and create a new C sharp script and then it's a new behavior script and it's got a template it's got like the start in the update that's just some more templates for doing other things but we'll ignore that for now we want to go over to mirror and we have the run time which is some of the stuff to do of the actual framework but if we look in examples we've got these different examples and we've got one about chat one about pong room tanks we're gonna go for pond and we'll go to the pong scene okay so here's the punk scene if we go to game you see here's the pong and before we look into the code we're gonna have a little go with it and I'll show you how to make a server and connect to it and then we'll actually look into the code so to do this to actually play and test with yourself you need to go file build settings and adds this scene as your initial build scene you can remove the default one for now so this is our game okay and then we're gonna say build and I'm gonna make a builds folder so let's call it builds okay and then in there I'll just click select and that's gonna go build the game so I'll see you guys when it's done okay so it's finished building I am now running the game over in unity so I'm going to maximize it and you'll notice up here we have free buttons we have one host one client and the lanΓΊs server okay so notice how this says local host so local host if you've ever played minecraft for example you can type this in and it actually just a way of saying your own IP so because I'm playing on the same network as myself I can actually do that now this won't do anything if I just click it by default because it's trying to connect but there is no I'm not hosting anything on my computer so it kicks me off okay it failed to connect if I click land server only then this will run as a server and I can connect to the server so if I go over to my built version which is fullscreen I should have made it windowed but anyway I can now LAN client and I'm a client on the server okay I can play I can move up and down for pong and if you actually look over on the server you see me there but I can't play because this is just the server okay let's stop the server you know it's back over have been kicked now if I click run host this means I have to also allow access for people to join to me because this is a new application so it's asking for permission I am now hosting on this machine okay so that means I am a server and a client right I am the server and I'm playing as a client let's move my pedal up here and I go back over to unity I can now join as a client and now I've got two clients in here okay the game like the example game doesn't actually have a scoring or anything you can't want to lose the point is I can now move on this instance and on the other instance you'll actually see it too so if I hit F EE click over we see how the game continues over here I can hit the ball back in an echo dot C like hit it tap out hit this would be very hard to actually play I'm I'm just that skilled pow sure okay sorry about that um so what we've got is we've got a pong game where this built version is the server and the client and then we've got the one in the editor which is currently just a client that's connected if I leave on this client the actual server just gets rid of the ball get rid of my player and it just leaves me as myself and I can stop - okay it's that easy to actually just make a server and actually join to it it's super easy okay now we're actually gonna go look into the code okay so if we look in the scene we're going to go through each object and explain how it works so ignoring the camera we have the network manager so this is the main thing this is the entry point for your application so the network manager pong okay this is a custom version they've made for the pong game so it just overrides the built in version now it's got lots of things so we'll quickly go through obviously you not gonna use most of these things also no you'll use most for you not menus all the things so don't destroy unload simply means when we go to a new scene this thing still exists that's what we want running background so if you tab off this or something you know you still want the server to keep running start on headless means that this will start running so a headless server is why you don't have any graphical user interface so if you're just hosting it on like a remote thing and there's no UI you know just ignore that they won't really matter for what you're doing most likely the server tickrate yeah so if you're playing as it says here like a top level a high-end FPS where it's super important to get everything perfect then go for 60 whatever wise 30 is good for any like you know games where it's not about complete precise you know you don't have to be perfect if you're playing a game of 60fps but you update data over the network half of that time so every two frames instead of every one frame effectively is what it's doing then you'll be just fine debug messages up to you when you come online so when you connect to the network manager or when you disconnect so you might you know loose internet connection or something you can give it a scene to go to so yeah you might have like a menu scene you would drag in there that's that's the scene I want to go to if I'm in the middle of a game and I lose connection just to go back to the menu network info so this is just telling it which transport layer to use you can actually get different ones the default one is called telepathy I'm not going to be getting into all that the network address is when you call there's a connect function when you say connect it uses this address so you could type in your IP address this is just the default value it uses when you press play but you actually need to change the network manager network address when you're trying to connect to someone's server Mac's connections is for so obviously it just automatically stops more than four people joining authentication currently non but you can add a username and password authentication and stuff there's different off as you can use the player prefab is simply the thing that's spawned for you when you join and this boolean just says whether you should create their plays object when they spawn or not round-robin so you can have different spawn points and this is basically telling it you know should I pick a random spawn point or should I go in order like looping through them and then going back to the start if you have different spawn points and in here it says here's the place for the left racket spawn here's the place for the right racket spawn so you actually see in the game if we go to 2d mode do this zoom out press the view okay so that's the left rackets born that's the right rackets born they're just empty transforms telling the network manager where to spawn in the player and then here's a spawn a ball prefab ball so anything you need to instantiate at runtime needs to be added here you can either add it manually in the build like this if you've got a few items if you have loads you can write scripts to actually load it in automatically so you might have a folder in your project the resources folder you chuck everything in there and you can just tell it to loop over the resource folder and load them all up into here okay but to spawn something in on the network to appear on other people's clients you need to have in this spawner balls a list okay for this game it's just a ball so just one just drag it in it's it's fine right and then this is when I press play you notice how there's a hood at the top left just to have this host and stuff you can turn that on and off you can just remove it if you want in in an actual game you'll have a menu to handle these buttons but for now they just do a little GUI dropping to make life easier and then this is the port hosted on and the Mac server sites are some things you can set this is all just random data or not random do sorry it's all just data for you to sir now this is all on the network manager but they've got the network manager pond which adds these extra free right the ball the rackets okay and this version that they've done overrides different methods so it tells you here if you mouse over they've got really good documentation it tells you whether a function is called on the server or on the client because keep in mind this is one code base for server and client so this is only called on the server and it's when a client is added okay so when a client connects that means they've connected to your network well once they've been connected they then need to be what's called like added right as it says here it's added with climbed or add player and that's what actually like will then spawn in the paddle for the player so you can actually go look at the code for this if you want right if we go to the override you see here what normally happens when a player is added well we say go grab a start position so it basically figures out what it is right there's they've written a method to they've run a method to find your start location okay get a start location and then basically if the start position exists then use this position of Hawaii to spawn it in in the center but that's just kind of like an error that's just in case there is a problem then just spawn it somewhere else and then add player for connection is a way of saying take this this player and tie it to this connection to this connection so by doing this it basically says the player is now owned by this connection okay so I now own this paddle when I join okay and then that's the base implementation but they actually don't call that head that instead of doing that they actually use this logic instead so they say well if the number of players is zero use the left spawn point or if the number of players is not zero then use the right spawn point okay so they get the start location they say spawn it in at that place and then add it in themselves and then as well as doing that they also say if the players is now two so if there's like you know if you've joined and the game's now full then spawn in the ball prefab okay now that spawns it in on the server and then network spawn as it says here spawns it in on all the clients okay so you spawn on the server then you tell the clients to spawn that game object which will keep it synced between them okay you can add components to sync like the position the animation and then you can write your own right to sync other stuff and they've also overridden one of the other functions called on the server only when someone disconnects okay when someone disconnects then that means there is no longer two people so the ball let's destroy it and by calling Network server dot destroy it gets destroyed on every one okay and then do the base logic and the base logic is to get rid of their connection so get rid of any connected object right so this actually gets rid of their paddle okay so over here it says destroy the ball and then go find that person's connection and destroy their paddle and then log it okay if they should if they're logging debug messages then walk the debug message okay and that's all they've done then everything else is in the network manager already you can override a lot of things you can add custom logic for when you know there's so many here when a client changes scene well see this is called on the client so if it says on client it's basically telling you it's called on the client Ava says on server it's called on the server so it says on the server when a client is ready on the server when a network error occurs before a client on the server connect called on the server when a new client connects you can do so many things with this but for the pong example they only do these two things ok that's the network manager then they've got the ball so the ball is a network behavior you need to inherit from network behavior which by the way inherits from monobehaviour if this object is synced over the network ok and the ball starts at speed and the rigidbody and then it sim simply says I mean I don't know why they don't just set these things enough so you can't set it any Specter why mind about yeah so when the this is called ok when they become active on the server so what that means is this code is only ran on the server ok only on the server as it says here only simulate it on the server but what that means is simulated is normally false but because this gets called on the server we're going to set it to true so the ball is only simulating physics on the server and we're going to give it an initial velocity to the right just to just to start the game playing ok then they've got some math here to calculate the velocity based on the player to say ok and all this time the way it sinks position with the player is if we go back to pong prefabs ball they've got on here as well as the rigidbody and stuff they've also got a network transform which as you see here client authority says set to true if the movement is coming from the client otherwise false if it's coming from the server now the physics is being simulated on the server so it's false ok we're saying no the client can't move this object it is being moved by the server and the client is receiving the position basically ok and just by having that synced on here because the ball because there's a network identity if I try to move this I think what'll happen is yeah I can't remove it because they have a fing relies on it you need to have a network identity to have a network transform so this ball is now synced across clients from the server and it's moved only on the server okay and then we have the rackets themselves so the racket has a network transform to sink its position but it's got client authority which means that the owner on the client owner has permission to move this object okay now maybe you don't even want that maybe you want even more authority I'm sorry off party authentication maybe you only want the client to tell the server that it wishes to move then the server should move the paddle that would be fully operative this game right now is only semi operative the ball is done by the server but the clients each handled their own racket depending on what kind of game you're making this is fine but people could quite easily cheat with moving their own paddle they could you know easily write something to just you know teleport it wherever they want to whereas if the server was moving it couldn't they couldn't do that okay and the player script simply is a network behavior okay so this thing is on all clients this player you know you see the other person's paddle it has a speed and a rigid body and then every fixedupdate frame only if we are the local player do we affect the what do we read input and set the velocity so is a local player returns true if this represents the player on the machine you're on so effectively it's just saying does it belong to me that's all it's doing right if this thing belongs to me it's the same as photon view dot is main if you remember we have we had a thing called photon photon view dot is mine or something like that right if you do that it's the same as is local player and it's local player the way that the place that comes from is the network behavior network behaviors have lots of things on them one of them is is local player okay and that's actually the entire code they used to make this pong game okay the game starts it spawns in the ball the ball moves it's synced across the client and the server there isn't actually there's no functions that are calling from though actually if you see here the on collision for the ball is a server callback which as you see here prevents the clients from running this method so when the ball collides on the client it just doesn't call this function right this only is on the server only the server tries to do this code if you tried to do on the client it just wouldn't work it might throw some errors so this is just to protect and say no only call this method if this is on the server okay and that's it now I've got pong okay that's it for this video to be honest we're gonna move into some other topics in the future videos let me know what you thought about this video let me know down below if you've got any questions obviously you can go through and look at the other examples there's there's some tanks one over here you know I could spend this video going through all the examples but it'd be a really long video so I chose pong cuz it'd be quite simple apparently this is pink because it's missing some texture I think it's cuz I it's cuz I changed to using the universal render pipeline so you want to do something about that maybe fix the materials but I'm not gonna bother doing that so yeah hope you enjoyed this video let me know down below leave a like and subscribe thanks everyone for watching I hope you look forward to the new videos on this feel free as I said to ask any questions down below I'll see you next time thanks for watching and good bye but of course before I go I've got to thank my patreon to special thanks to Jason Swearingen Liz Kimber Josh balsam beard or Die Dustin Miller Francisco D s wreck your Isleta Hades Orko Renee Emily Baldwin if a known else is able to help support the channel monetarily link to my patreon is down below be greatly appreciated if you could go and check out any links to social media such as twitter twitter and discord as well as our website they'll be greatly appreciated I'll see you guys next time thanks for watching and good bye
Info
Channel: Dapper Dino
Views: 325,633
Rating: undefined out of 5
Keywords: Unity multiplayer, Unity multiplayer tutorial, Unity multiplayer game tutorial, Unity multiplayer 2020, How to make a multiplayer game, Unity, Tutorial, How to, Multiplayer, Networking, Server, Client, Mirror multiplayer, Multiplayer networking, Unity networking
Id: 5LhA4Tk_uvI
Channel Id: undefined
Length: 19min 4sec (1144 seconds)
Published: Thu Feb 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.