How To Make A Multiplayer Game In Unity - Ownership & Authority

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so alone we'll come back to another unity tutorial in today's video we're gonna be doing some multiplayer using mirror in unity in the previous video I showed you guys what mirror was how it works and we looked at an example project and also I showed you how to get it from the asset store install it set it up in unity and for the next few videos on mirror I'm gonna be explaining different concepts how they work and doing some little examples so in today's video we're going to be looking at ownership how to know whether a certain object on the network belongs to you belongs to other players is on the server only and all those kind of things I hope you guys are looking forward to it let's get into it so this video will be split up into three parts part one will be setting up the scene putting in the network manager making sure we've got everything we need step two we'll be creating the player and adding all the components we need to the player and then for step three will be actually writing a script for the player our custom script that'll handle some logic for movement I hope you guys are excited let's get into it okay so in the previous video I said I'd be setting up the github repository for this project and github was actually down at the time but now it's back up and it's on this if you guys need it you can go to my link in the description to go to github and find the repository mirror multi-plate tutorials and an inside asset we've got tutorials inside there we've got two days which is ownership it's all about ownership and then when I make more they'll come in here too so you guys can go get the source code there if you need it so in this video there's not much coding I'm actually just gonna be doing it all on camera I'm not going to be skipping anything step one as we said we want to make sure the scene setup so you guys you don't need these necessarily but I like using them and I know a lot of other people do I've used some empty game objects to separate the different things in the scene so the system's I've put the network manager when you're gonna need to stick on the network manager component which I covered last video a bit but this is it's got so much to it I can't you know it would need its own video to explain what it is and what it does get into so much stuff but effectively it's how you connect as a client or a server you change some settings you are meant to override this to actually add custom logic to it but for the sake of our demo we don't actually need to override anything and we want to add one more component which is the network manager hood which is gonna show as when we press play if we wait a second in the corner we get that little UI stuff if you've used unity multiplayer before it's the same thing it's a button for host client or server only okay and we're gonna be a host and we're going to build the game and run a client so that we can play against sells okay for this for the demo but effectively oh that's it it's a network manager we don't really care about players or IPs or anything right now it all works fine for testing locally the only thing we care about is setting the player prefab which is going to be the next step but the other thing you would need to make sure for this example is that your camera is aiming in the Z direction or the Z direction so the blue arrow this is on global rather than local in this direction is where it should be aiming because I'm gonna spawn players in and make them move off in that direction so you can see what's going on and yeah we can move on step two now make the play so step two let's make the player let's go and create an MT let's make a sphere actually for the player okay here they are let's just reset them so the player is going to be called player and set the player tag just because what we want to do is we want to take off their collider because there's no collisions in this demo and we want to actually add a custom script for the player which I have already made but it's got nothing in it so if I go over to here and drag on this player script there is as I said no code in here yet we're gonna be writing some stuff and then we also want to add on something called a network identity if you don't well let's show what happens if you don't if you go to prefabs save the player in there and then unlock the editor okay if we go across the network manager it won't say player prefab which is what it spawns in when a client joins so if we actually do lock that again let's drag in the player it's not gonna like it it's gonna say player prefab must have a network identity and network identity is you know how mirror knows what is what on a network and what belongs to who and all that lot so if we go to the player we want to add a network woops why is all this stuff on the player oh it's not it's just cuz I've got this lot okay so on the player want to add a network identity okay save and just by adding that we can now go to the network manager and add the player prefab so this is something the network care well the network manager can now spawn okay the server can spawn this object and whoever it spawns it for can actually have ownership okay but you can check on the network identity who owns it and whether you own it whether you have authority to do stuff and that's what we need to be doing so let's actually move on now to write the player script we're gonna write a simple script to move the player so we're gonna say su Lacefield private vector3 movement okay just so I can set it any inspector and we want in the update function there to basically say if we don't press spacebar so if input if not input get Q down key codes space this is just a really simple example as I said return if we don't press space but if we do press space then transform got translate the movement vector so we're saying move that much in that direction every time we press space now problem of this is quite few problems okay that we're gonna solve so one this would run on every client because every client would have many players and they'd all press space when they press space all of the things would move but we want only you only want your input to affect your player and we also need to sync movement because this won't actually sync across clients so the way we're going to do that we've got two ways I'm going to show you today and the first one is going to be by using a component that comes with mirror that syncs transforms or syncs has two positions basically and rotations and scales so we go back to the player and we want to add a network transform okay let's open that up so that's the network transform and this component will sync across a network where this object is so I'm gonna go put that there so client Authority is whether the client has the authority to move this object if it's false it means only the server is allowed to move it so for now we're going to say yes the client has authority which you don't want most of the time but for the sake of this you know it's fine and if your game isn't if you aren't worrying about cheating then you can allow the player to move themselves but at the end of the day if you're making a competitive game then you definitely want to validate movement yourselves or as the server rather than letting the client do it we're not going to change anything else there by default now this object will be synced across clients but the same problem is still here which is that you control all of the things you only want to control your own so before we even check if you pressed a key we want to only do this method if you have authority over this object now currently we're inheriting from monobehaviour we need to actually inherit from network behavior okay and that allows us to access whether we have authority so if we have authority well if we don't have a priority sorry we return which means that we end up only actually checking input on the ones we have authority for which is our player equally we can also add this client tag I'm not sure if this is actually necessary but it means that this method only gets called on a client we don't need the server to run this code this is only on clients okay and then now this should actually work if we actually go and build the game we'll be able to move but there's still a slight problem which will be sorting out in a minute let's go give it a test actually first we're going to delete the player from the scene make sure the prefab is saved and up-to-date also make sure the prefab has a certain amount of movement so I'm going to set it to one on the said axis then I'm gonna delete player then go build the game I'll see you guys in a minute okay so I've built the game here is the editor and here is the built version we're gonna use the editor as the host so that's the client and the server we press this and it's Borden's it spawns in our player object which we've registered down here and they play a prefab and if I press spacebar I'm gonna move forwards then if I come over here and I press land host sorry line client I'm going to join and now we are now if I press spacebar here I move forward but you notice there's one problem here I'm teleporting which let's pretend that's my intended you know intended thing as I teleport well the network transformed for you is actually going to try smoothly syncing the position which is what you want most the time for example you know just running around your game as a player most moving isn't teleporting so you want to using transform view like we're doing right now and it'll be fine because it'll look better but let's say for example you do want to teleport then I'm gonna show you the second alternative because you know if we want to teleport and the player sees us move smoothly then we're not really seeing the same thing and it might break you know well not the immersion but it it's not what you want so also if I leave I go away and I can join back in him back in a start I can move here so now I'm really far ahead if I go back to tier you see me off in the distance so yeah it works but I'm gonna show you the alternative for if you actually want sudden movement rather than smooth movement so for that go over to the player prefab we're going to remove the network transform because we don't want that to sink our position okay then we go back into the script and rather than translating on frame we need to actually get the server to effectively teleport all our players but the problem with that is we need that to be synced to so we want to send a method to the server and then get that server to then broadcast out to all the clients and this allows you to validate on the server we don't need any validation for this example but I'm going to show you how you do that so if we go over here we want to make a method so a private void and it has to start with CMD there's a command and I don't have moved for example okay so and then to actually mark this as a command we put the command tag before it call this from a client to run it on the server okay so down here instead of this we're actually gonna comment that out and instead I'm just gonna call CMD move and what that does is I'm a client calling this command which means that the server then runs this logic so this logic is on the server the server is then well this would be where you do you know invalidate logic here you know but then let's pretend we've got no validation we just want to then say RPC move down here we make another function called mock RPC move and this is a client RPC so call this from the server and it runs it on all clients okay you can't as a client call a method on other clients you have to tell the server something and in the server decides yeah I want to tell all the clients to do something so as the server obviously if there's some validation then you do it here but then once you're happy that you know this command is what we should be doing then we say okay clients move so then the clients say okay let's go grab this whoops transform duck translate and movement and I'm just going to put that like this so the the flow goes you know every frame on my on my client if it's me I'm gonna check for input and then I'm gonna tell the server I wish to move okay then the server's gonna validate logic and then say okay everyone move ok then everyone receives this all the clients and they move their that object now the only problem of this which comes in with the validation is that I could technically you know get a hacked client and move other players because you know this this constraint is only because I've coded it someone else could still connect and some different code that you know at the end of the day all validation should be done on the server so this is where you should actually check that the person requesting to move is the right person now that will take a bit longer and we're not going to get into those kind of scenarios in this video this is meant to be simple but effectively now yep I want to try moving I tell the server this I was like cool everyone move okay and because we're using transform that translate and not a synced transform component they will teleport rather than smoothly moving on the other clients so let's go give this a go so I built I'm gonna join as a host gonna join as a client and I'm gonna put this here and move okay look as I move it's pretty pretty much it instant okay I move forward I go over here I move i teleport forwards and then obviously yeah its teleporting I can't stop Joe him back but I can start teleporting around again okay so as I said the thing that's happening we'll go for it one last time in and I'll end the video is that we say okay does this object belong to me if it doesn't belong to me then I don't care okay return did I press the space key if I didn't return okay so at this point this ring belongs to me and I press the space key tell the server I wish to move the server receives us only okay the server runs the logic for validation if you had any okay and then the server's like okay everyone move I then call this method which is RPC move because it's a client RPC it actually calls it on all clients equally there is a way to do target RPC which is to call a method on a certain client rather than all we'll get into that in a different video and then the clients all receive this method call which then they all move their object okay if we had free players it would also work you know you could add another one I can actually show that before we in the video if I stop okay over here let's let's host then join us a LAN clients on before it's a bit on before us a little bit so I got another version the game I'm gonna join as a client now we've got free okay this is going to be very hard to show but maybe I can do it like this so as I move forwards you actually see me on all three clients moving forwards it all sinks perfectly okay so yeah that's it for this video I hope you guys enjoyed any questions feel free to ask down below any suggestions also give those down below if you liked the video then please leave a like and subscribe I hope you enjoyed 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 special thanks to Jason Swearingen les Kimber Joss Folsom bearded a Justin Miller Francisco DS rack your sled sir Hades Orko rene boudreau mary baldwin if anyone else is able to help support the channel monetarily link to my patreon is down below if not there also links down below to other social media such as to which Twitter and discord if you could help me out by following on any of those or going on to our website and having a look around there that'd be greatly appreciated I'll see you guys next time thanks for watching and good bye
Info
Channel: Dapper Dino
Views: 90,665
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, Authority, Ownership
Id: oBRt9OifJvE
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Mon Mar 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.