Making a Multiplayer FPS in Unity (E04. Syncing Movement) - uNet Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus but everyone and welcome to video number four on making a multiplayer FPS in unity in this video our primary focus is going to be syncing our movement and rotation over the network so things will start to look like an actual multi player fps also we're going to update the looks of our scene a bit and just clean up the project so let's get started but before we do I quickly want to mention that if you have any questions you can go to farm practice comm if your code isn't working you can download it off github links in the description and if you want to support my videos you can go to break is calm slash donate so now that that's out of the way enjoy so let's start by adding a bit more contrast to our scene so if we go into a scene view here and select the ground plane I think we should add a new material to this so let's right-click go create material and let's call this ground then we can drag it onto our ground plane we can go under the albedo slot and change the texture here and you can see that there are a bunch of textures imported with this standard assets pack one of those is the swatch orange and I'm just going to be using that and also we might might want to bump down the smoothness a bit actually I'm going to change this from standard to standard specular set up and then bump that down to 0.3 and maybe I want this to be a bit less orange so in order to do that we can simply find a color pretty much opposite of orangey s or light blue and tint it a bit in that direction just so it won't be too much and then finally I want to add the same grid that we see on the cubes here so if you select those those are also from the standard asset so you have this an AV grid texture and then they use an emission map that has this grid look so we can do the same so if we simply select our ground plane go underground and then we can find our emission slot and find the grid emission texture again under prototyping and the textures and then we can maybe bump this up to two to make it a bit more clear to see at least from a distance also when we had the grid on the ground plane here I want to go on the gizmos and make sure to disable sure grid you can see I've done that already I think that looks the best awesome so I think that really added some a nice feel to our scene and I think you will find it - the pretty cool with the skybox also so now that we have that in place we can maybe just quickly create a couple of folders here so let's create one for scripts where we can place our controller on motor and I'll play your setup and one for materials where we can place our ground and our gun cool so let's hit save on that and now we are ready to do a movement synchronization over the network for our player objects so if we select the player here unity has provided us with an awesome component called the network transform component and this takes care of pretty much all pretty much everything for us so singing position and rotation over the network isn't necessarily a very difficult task on top of the HL api and we could definitely go ahead and create a script that would work just fine right now but the default unity component has some nice features that I think will you will like so let's let me just talk a bit about how this singing actually work well you can see the first slider we have here is the network send rate and this is the amount of times and second that we will send a package containing information about our position around and rotation to other clients so this is kind of the update rate and well in an ideal world this would be 60 at least so every time our computer rent the frame we would also send the information and then it would be ready next time the other clients would want to render frame so this way we would get very accurate movement and we would get a very smooth movement also but in reality that's not really possible because if we were to bump the send rate up to a very farm a bit behind what this allows us to but then we would very quickly clutter up the network and that would maybe cause it to completely stop or to a black candy connection or whatever so that would give us a lot of issues instead what we do is we send out a limited rate you can see the default here is 9 and we're just going to leave it at 9 and then what we do is we take this very jerky motion where the player just drum jumps from one position to the next and then we smooth it out by looping between the positions or doing what is called movement interpolation and this is super awesome because we get suddenly a very smooth movement it's not going to be as accurate but we can get close so that's kind of what we do and the extra awesome part of the network transform component here is that it can utilize that we are using a rigidbody 3d and this means that it will take into account of velocity and movement direction and all that to predict where we are going to be in a second and therefore can make more precise interpolation for our movement so that's super awesome so if you have you if you're using a rigidbody like I am here definitely use it here under the transform sync mode if you're not there well then you can just sync the transform and that's going to be fine but this is definitely very cool and then we have the movement threshold here and that is the amount of units or that's the how much we can move in units before we send updates out that we have moved so this is how much this is basically the threshold for registering that we have moved on the network and that's going to fine here and then we have the snap threshold and this is basically how many units we need to move before we don't do any interpolation and just snap to that point so let's say our we had we would have 8l appoint teleport the point over here and the end of the portal would be over here well then we would want to be able to go through that and then jump over there with any without any interpolation along the way well that is what the snap threshold can be used for and also if we should experience a huge lag spike and a loss of connection for a time well then if we move all the way over here we will wanted to simply jump once we get the connection again and not suddenly just fly through the map that would look super weird so that's kind of what that variable is for and you can just leave it at five then we have the interpolate movement factor and the smaller you make this well then the more we are going to interpolate the slower we are going to or the more smooth I should say the movement is going to be and a pretty good value for this is actually one that's a good default value and we're going to leave it at that but I do want to show you what it looks like if we disable interpolation and you can do that by setting this to zero and let's also just disable rotation here set that to none and we can look at that in a second so if we were to now save this and then go on to edit project settings player and then we can just disable this annoying resolution dialog each time that we make a build we can also disable the oil set the default not to be full screen and then maybe input 800 by 600 years so we're just going to get a small VIN window that starts automatically so now let's press control B a command BR if you're on the Mac here and it's going to build a player for us and you can see that it starts up there and inside of unity I'm simply going to drag my scene over here split it down the middle so that we have our player here and the scene here and we can view both the build and the scene over here so now let's hit play and go to host and yep and then we can navigate over to our build here and select client and if we turn around here then looking over here we can start moving around and you can see that it is indeed updating but it's very jerky so we have this motion jumping which we definitely do not want but the cool thing is that we can go in and adjust this interpolation factor while the game is running so this might not work it has worked for me I don't know if the component is built to do this so not all of the settings might not work in real time but I know at least if we find our player here on the player that we built so that would be this one not the one connected to the editor so that one here associated with the build and then bump the interpolate factor up to one and then look at them here and go over here you can see just how smooth that motion now is so that is actually a pretty good amount you can see that it takes him a little while to slow down when we stop while over here it's instant but that's kind of the price that you have to pay so I'm actually very satisfied with that movement just for fun let me show you what this would look like if we changed it to 0.1 that means that the until it interpolation would be very slow and you can see indeed it is and if we were to bump this up to something like 3 well then you can see we start introducing some of the jerkiness back into the motion so I think a value of 1 is great there and we can go in here and save that to 1 then the rotation here we can select what axis we want to sync over the network and in our case we only want to sync the y-axis because we are going to do the sinking of rotation on the camera as a separate component so we'll just select Y there and the interpolate fashion factor we're going to have to bump that up to ten actually I found when using one it was way too slow I mean way too slow so I found that 10 is a good value and why these two differ so much is not something that I can answer your on I'm sorry I simply don't know but I do know that 10 is a pretty good value to set for this you can of course again build the players and play around with this so we're just going to assume that this is working and then add the rotation syncing for the camera also so you might be thinking well let's go ahead and find the camera here and let's add a component and add the network transform where you can see now it introduces a network identity and another network transform and that's not something that we want to have instead what we want is we want the player to have this central network identity component and have everything be controlled from the road object so in there for unity has made this network transform child component that we attached to the route the player object and then we drag in the camera and it will control this so if we select Network transform child here you can see that we have this target variable and in here we can drag the camera and we have a bunch of settings for this too and this behaves a little weird because when I use the movement threshold well I said okay so I don't want to sync any movement I just want to sync rotation on the x-axis so I went in and said set the movement threshold really high so that it would never send out any movement that didn't work hard because movement threshold I think it's a bug also has an effect on rotation it worked on some of the clients but on the host system and the the rotation suddenly didn't update so if you want to be sure that this is working simply leave the movement threshold as is then we have the interpolate movement factory this we can go ahead and set to zero that's that's fine because we don't want to spend any processing power on the templating movement on something that doesn't move then we have the interpolate rotation factor and this somehow behaves differently than the rotation factory up here and it might be because this swings using a transform and not a rigid body which is also something we want because our camera doesn't have a rigid body attached so that might be why but for now let's just leave that this at 0.5 and see what happens and then we can change it at run time the rotation axis we of course want to set to X only and we don't need to compress the rotation here so let's save this and let's build this and see if it works so there we have the build we hit plate select host there select client here and you can see now that our rotation looks just fine however if we kind of get closer here you can see that the up-and-down rotation is a bit jerk and jerky still it's kind of clunky so in order to change the smoothness on this we can of course find the object as always not that one this one and we can try and make this smaller so let's set it to 0.2 and just focus on him there and you can see now the rotation is much smoother I think however now it's a bit it takes a bit long for it to slow down so I think we'll find an intermediate value at something like 0.3 and you can see there what it looks like and now we can move around and look around and it will update on the client which also hosts the server and on the remote client here so that's basically all for syncing movement over in the net so that's it stop on that let's stop playing there let's go into the player and change this to point 3 and we can collapse that and collapse that and we are basically done so that was basically it for performing movement synchronization over the network for our player I hope you enjoyed this video and the next one I think that we're going to look into expanding on our character controls so maybe adding some flying mechanics the spring physics that I teased you in the first video and also have setting a lock on how much we can rotate our camera so that we don't suddenly view everything upside down so that was basically it for this video I hope you enjoyed it and I'll see you in the next one
Info
Channel: Brackeys
Views: 208,802
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, material, materials, beginner, learn, course, series, game, programming, coding, C#, Unity (Software), Software (album), sync, movement, syncing, synchronisation, synchronize, update, network, networking, multiplayer, host, server, rotation, interpolate, interpolation, lerp, smooth, rate, transform, networktransform, networktransformchild, fps, brackeys, how to make a game
Id: MR8SULMRKKk
Channel Id: undefined
Length: 16min 29sec (989 seconds)
Published: Wed Oct 14 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.