How To Make A Game With Unity Multiplayer Netcode | Network Animations & Player Controller #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody this is dilma and welcome back to my channel so in today's video i'm going to continue working on net code for game objects and specifically i'm going to introduce you to the network animator component that is going to allow us to synchronize animations over the network i'm also going to be doing a new version of that player controller by changing some of the implementation that we did before and making a good change that is going to utilize a character controller i'm also going to go over network variable permissions again because i got some clarifications from unity and i want to make sure that you know the proper information and the correct information when you're implementing networking with these components so let's jump into my computer and start working on it all right guys so what i'm going to show you today is how we synchronize animation so right here i have basically the server running and also one client and if i walk around you're gonna see that it's doing the work animation if i sit still it's a little hard to see but basically it is playing the idle animation this is another client that also is synchronizing so as i move the character there you're going to see that it's moved also on the right window and everything gets synchronized correctly so i'm going to go ahead and close out of these and then start working on a couple of changes that we need to do to the player are mature so let's go ahead and go into it and if you remember what we did previously we apply a network object the network transform also the player hot and also the player control which we're going to be changing in this video let's go ahead and hit add and if you do that you're going to be able to add a network animator and what i'm going to do is i'm going to go ahead and move that maybe right above the network transform just to have them organized and i'm also going to be creating a new animator so we're going to go ahead and create a new folder and this one is going to be called it's called animations very simple there's really not going to be a lot of different animations i'll do an animation controller we can just say simple animation and then once you do that you're going to be saving it we can double click on it and what i'll do is i'll put perhaps this one right here let me put it right here okay and then what i'll do is there are a couple animations in the starter assets so if i go into that repressor controller character animations you're gonna see that we have a lot of different ones in here that we can we can use in fact i'm actually going to move this because i think this is going to be easier to see because i'm sitting right on the bottom right and let me just go ahead and do that we can just pan and put the other okay so we're going to have basically nothing in here just going to be any state and also entry i can add a new parameter and this one we're just going to call it walk and i'm going to go ahead and just set it to we're going to set it to zero just for now and then what i'll do there's a couple animations in here that i'm going to want to use i'm going to have a walk animation and i'm also going to be adding the add animation which is going to be actually the default so we'll just go ahead and right click on that and make that one the default i'm also going to go ahead and do a duplication on the walk because i'm going to actually do reverse walk just to be just just so that we have something different to do i'll just do reverse walk in here and then let's make sure that that one got renamed let's see reverse walk and then this one is just going to be walk we can just rename it so we don't have that weird naming convention and i'll just go ahead and move these ones over here so what's going to happen is we're going to start in the idle right and as soon as we start incrementing the walk speed we're going to go to a walk and then from a walk we're going to go i mean we could do a reverse walk if we go and hit the s key or the down arrow so what i'll do is i'm going to go ahead and basically connect all of these ones i'll do connect this one as well connect this one and then we can go from idle to reverse walk so all of these states are going to be valid once we have all those what i'm going to do is i'm going to start adding conditions so we're going to go back to idle and then make sure that you basically remove the has access stay on every exit time on every single one of these because we don't want to we don't want to set a time for the animation i want him to end right away so i'll do that and then also do that on this one okay so now if we're going to walk the way that it's going to work is we're going to set this to we can just set it it's going to be greater than 0 so that means that we're working and then what i'll do here if i want to go back what i'm going to do is i'm going to say okay if it's less than and this is going to be less than 1 anything less than 1 we're going to go to idle so the part that is going to allow us to reverse walk is going to be basically going to a negative number so i'm just going to do if the walk is less than actually less than zero then we know that we're going to go back to you know reverse and if we go forward then that means it's going to be basically greater than zero so that means we're going to go to a one the same thing in here is going to be greater than zero and then this one is going to be less than one which means that it's going to be zero and then if we want to go basically reverse walk right away we can say this is going to be less than zero and then we can also get back to idle if we want to set it less than basically less than one so very simple animation system so that we can change and we can test this right now so if i were to here we can go back in here and what i'll do is i'll grab or player we can just say i think i can go ahead and test it without having these so what i'll do let me go ahead and hit play and that way we can instantiate the the network player armature right away we can do host and then what i'll do is i'll go ahead and go back in here i'm gonna go ahead and go back and then right now it's not playing yeah and it's not playing anything because we haven't really added the animation controller so it's actually good that we made a mistake and by we i mean myself so okay and then it's gonna tell you okay what what animations as this has and everything is going it's going to it's going to work so by default it's going to add this one in and this has multiple ones i want to change this to be basically the simple animation again i want to keep this as simple as we can and then i'm also going to make a change to the network transform because i don't want to change i don't want to send scaling information back if we don't need to if we don't want to have the server basically syncing that information we don't need it in this case so just going to keep it keep it simple so we can go back here and then hit play make sure that we don't get an error and if we get an error it's okay because we'll go ahead and we'll go ahead and fix it click on star host and you can kind of see that it's doing the animation we can get here into the scene view and see that the character is currently breathing i really like that animation so we can make these a little bit bigger here so let's say that we wanted to go to a one so if we go to a one we're going to start walking right and that's basically what we want if we wanted to stop basically either animation that is playing and if you select the clone you're going to see which animation is actually playing what if i do negative one we're doing a reverse walking which kind of looks really strange it's because we don't have the ranges correct so let's go ahead and hit play to stop it what we need to do here is i need to add another condition because i want to make sure that i am you know between one and negative one which means that is zero so that we can go back to idle let's say play and make sure that that works let me go ahead and focus on this guy so you can now see that it is doing a new animation but the animation here that i want is a negative one so that we can go back in reverse you can see that how we can reverse that so let's go ahead and test it out so if i go to zero we're going to be idle if i go to one we're gonna start walking if i go to negative one we're going to basically walk backward and if i go back to zero again we're just going to get back into idle so we know our animations are working let me make sure that everything here looks good and i think it does so i think we're good with the animator so i'm going to go ahead and put it right here and then get back to where we were so if we take a look at the prefab right now just to kind of give you an overview we have our character controller we have a network object which we're going to need we also have the network animator which is a new component with player armature which basically links back to this animator and this animator has the animator controller that we just created i also remove the scaling because we don't need to synchronize that information and then what we're going to do now is we're going to go ahead and make a couple changes to the player control that we currently have so when i cover some of these on the previous video so if you didn't watch that video make sure you watch it because it's going to it's going to make more sense so the first thing that i want to do is i'm going to go ahead and rename this and i'm going to call it speed i'm also going to go ahead and do a new variable this one is going to be rotation and speed and by default i think we can do something like 1.5 is a good number and i'm also going to introduce a new inum it's going to do public you know and this is going to be for the player estate so basically going to have walk stay and then we're also going to have an idle stay and then obviously the one that look weird at the beginning but this is the one that we're gonna we're going to start you know implementing is the reverse walk so we can do something different and then i'm also going to rename this i was about to cuff so sorry about that default initial plane position this is so that it makes more sense and what i'm going to do here we have couple of variables that we network variables that we were using on the previous i'm just going to go ahead and leave those there for now but we're going to be removing them so what we'll do here is i'm just going to do a network variable but in this case i'm going to do a vector3 and this vector 3 is going to be for the basically the direction of the character controller so i'm going to do network position direction equal new of the same thing a couple of things that i want to mention in this video just as a correction of the previous one remember that i said that you could have a network variable that is basically writable from a client or readable well that's actually not true you can either have a variable that is readable by the clients or you can have one that is only readable and writeable by the server so the server can write and read but you can basically restrict who can actually read from that variable if you didn't want the other clients to be able to read from this one all you really have to do is do something like this i can say permission owner only which means that only the server can basically read the variable and if you want every one you can do just every one in my case it's gonna just set it by default which is everyone everyone can read it every client can read it but only the server can write to it okay and then the next one that i'm going to do is i'm going to say network rotation it's going to be to control the rotation and then we can just go ahead and remove some of these and then the next thing that i'll do is i want to also store the player state so that we know which animation to play so i'm gonna do we can do just copy that create a new serializable field and then in this case i'm just gonna make it the player stay which is the enum that i just declared and then we can say network player player stay equal new network player state okay awesome so what else do we need to do here so this workflow so i'm going to change into b vector 3 and then this is going to be vector3 and i'm also going to rename it this is going to be all input position and then i'll do the same thing here all input rotation because we're going to be getting that information from or input controls so i think i got everything that i wanted to i wanted to change okay perfect so the next thing that i also want to change here is i'm going to introduce a new method which we're going to be basically uh intersecting the awake method okay so i'm just going to do character controller and we haven't created a variable for that yes i'll just do that here private character controller and then character controller and then we'll just do character controller equal get component because remember the the component that we're currently using has a character controller in it and it's going to make it a lot easier to basically to manipulate the rotation and also move the character and i also need to do one more in here it's going this one's going to be for the animator because we need to change the state of the animator so i just do animator there and then animator equal get component the animator which this component also has this is fine i don't think i need to change anything in here other than i only want the correct line to be able to randomize and then i also need to own this object so basically if i am the one you know i'm the client and i am the owner i'm going to be randomizing the position of the actual rubble otherwise i just want to read the information that comes from the network transform that the server is designating for the client okay so i think everything in here looks fine i am going to change this it's going to be actually i'm going to do client input and then i'm gonna change actually let's go ahead and remove all this so i'm just gonna i'll do it from scratch so what we'll do here so i'm just gonna say okay as long as i'm the client and as long as i'm the owner i'm going to basically be capturing the the client input so i just do a new method here which we haven't looks like we created but i'll just i'll just change it because it's going to change quite a bit now and then i'll have two methods in here which we're going to implement one is going to be client move and rotate i'll just basically leave it commented out and then the next one is going to be client visuals visuals and this is going to be so that every client gets the proper move and rotation and also the client visuals which means you know what is the what what is the player state at that point so we can actually play the animation and those are going to be set by using the information that we're storing as network variables okay so i think everything here looks fine it's going to go ahead and remove all of these we don't need any of that and then we can just remove this it's going to change as well so just barely bear with me this is going to be cool and then we'll just do it's going to be client visuals i'll just create another one in here which is going to be client move and rotate okay so far so good on the client input we're going to be changing the way that we move the the actual client so that's going to be cool because it's it's actually going to feel more natural when you're moving it so i'm just going to do a vector 3 and then it's going to be the input rotation so that we know how we can rotate our character it's going to do it's going to be input and then get access and then horizontal axis and let me make sure that i do that right and then i'll just do comma zero so this is gonna be just the y rotation that we're going to be getting from the horizontal axis and then for the forward direction we're going to do a couple of things we're going to do get the direction this is going to be we're going to get this direction of the player by just doing transform direction and then we just do the vector three four so that's gonna let us know okay what is the vector for the direction of the the player is actually looking at and then what i'll do here i'll just do a float this is gonna be the forward input i'll just get that from the get axis and it's going to be the vertical vertical axis so that should give us you know if we're basically hitting up or down or if we're doing w or s are we going forward are we going backward and then what i'll do i'll do another vector3 here that's going to be the input position and then i'll grab the direction and i'll multiply that by the forward the forward input okay so now that we have the input position basically are we going forward or backwards and then we also have the rotation we need to make sure that we can track that and send that to the server so we're going to do also a couple things in here just going to make sure i only do this if there's any changes so if the o does not equal to the input position or the all we also want to do that on the rotation and the all input rotation does not equal to that new input rotation then we know that there was a change right so we want to change that to the to the server but i also want to make sure that we don't do that every time so it's going to do all input rotation equal all input rotation all input position equal to all input position okay so and then the last thing is i need to send that to basically the server so let's go ahead and rename this it's going to be update client position and rotation and then make make sure that you have the suffix which is server rpc otherwise if you don't do that it's going to it's going to obviously complain and then this is going to be the new position that we're going to set for the for the player we can just say basically it's going to be you know how far we're going forward how how much are we going backward and then i also need to know that new rotation we can call this new position direction and then new rotation direction i think that makes more sense to me in my head and then what i'll do is if you remember we have this position these network variables in here what i'll do is i'll just say okay for the actual network position i'm going to set it to the near position direction and then i'll do the same thing for the other one i'll just go ahead and rename it and then this one i believe is new rotation direction and everything there looks fine we need to also make sure that we call it and make sure you do the new one here not the old 101 because that's really not gonna help it's gonna keep executing that every single time and then i'll just go ahead and copy and paste this method here we have to send the new position directions make sure that i do that which is going to be the new position and then i'll also do that with the rotation in here i think if i name these ones rename them a little bit different they will make more sense but i think that's fine for now and then there's going to be a couple more things in here that i want to i don't want to do so we know the forward input so if i going greater than greater than zero then i know that the i need to change the player state so what i'll do here is we'll just do another server rpc this one is going to be basically for the animation state the player state so we can call this one update player state player and player it's really difficult to explain and type it at the same time but i'll make my base guys so we can do and then it's going to be the basically we can say this is the new state and remember on the very top we had also the new network player state so make sure that we set that player stack correctly and a new state okay so that now we need to do is if the value is greater than zero remember we did it was going to be a walk so we need to update the player state but this needs to be server rpc so i just make sure that i name it correctly and then here i'll just say it's going to be walking and then we'll just do another one very similar it's going to be an else else if and in this case i want to know if we're going to be basically doing a reverse walk then i'll just do that and then lastly i'll just do an else anything different than that which means that it's going to be zero then we're just going to be doing an idle and that's going to cover everything that we need to do on the client input so this is basically going to be for or player state changes and then anything everything in here is going to be for player position and rotation input okay so and i think based on input okay so that's going to wrap that up so now the next thing that we need to do is we need to implement that move and rotate so what i'm going to do is i'm going to go ahead and go back down here to the move and rotate and this one what i'm going to do is i'm going to say network position direction the value make sure that is not equal to zero and if it's not equal to zero then i'm going to use the character controller so we have our character controller we're going to be using our simple move and i think i did something here now that i'm looking at it this is actually okay but we need to make sure that we multiply this by the speed otherwise it's not going to be very fun and then this one is going to be multiplied by the rotation speed so we're capturing that information but we also want to make sure we keep the speeds that we set up here on the very top as our options i just noticed that so let me make sure that that is set and then if we go back into the character controller simple move we're going to now use basically our network position direction and then get our value and then i'll do the same thing with the rotations it's going to be network and then rotation direction that that's not equal to zero if it doesn't equal to zero then we're basically just going to grab that variable and then just set it here actually you paste it and that's going to basically do the climb over rotasa we're capturing the input and then we're sending that information reading that information from what the server set and then once we have that then we can use the character controller on this object to basically move or character and then if we go back over here we can just go ahead and comment uncomment that out and then i'll just uncomment the visuals as well because we're gonna be implementing that now okay so this song is gonna be it's gonna be fairly easy we can just do network play or stay and then what i'll do also get the value of that and then if we're walking then obviously remember the animator and then we set basically a flow and then that flow was a walk property and then in this case it's gonna be one right we're gonna be walking and if we're walking we're gonna set it to one and then i'll do basically uh i'm gonna stop saying the word basically if i say basically again make sure that you tell me in the comments that way i'll remember not to say not to say that and then reverse is gonna be negative one anything else is going to be idle which means that it's going to oops let me undo that that is going to be set to to zero so if the player stays is walk then we're gonna set the animator value of walk to one if it's reverse walk negative one and then zero and i think that's everything that we need to do in here let's go ahead and get back into unity and make sure that i didn't make any mistakes and wait until everything loads and i'll hit play and make sure that i don't get any errors in here if i do get ours then obviously we'll fix it let's go ahead and do that and i think i'm getting let me see i can go back i can't rotate for some reason and okay we'll fix that but that's fine one thing here i'm going to set this to zero by default okay so we have the speed correctly we have the rotation speed here everything looks fine if we go down i think okay yeah looks like i just copied that and i didn't change the method here so we can't really just do a simple move that's no that's not really how you rotate the the current the actual player so what we'll do here is we'll just do the rotate meta and then what i'll grab is i'm going to grab that value and that's because you know obviously it was go it wasn't going to rotate when we're calling the basically the wrong method and then what we can do is we can go back into unity make sure that everything works now so we'll wait until everything compiles and then if this works we'll also test it to make sure that we have multiple instances running and and you know we have the multiplayer implementation working and you can see that everything everything works now obviously i can't rotate with the simple move because that's obviously just for moving and not for rotation so if i were to increment here the actual speed we can do something like 10. and then we can rotate the character like crazy anyways i just wanted to make that fun but we can go back here to maybe 1.5 and you can see like if i select these the player amateur network you can see how everything is changing in real time if i go back we can go back i can go forward so so we know that that part is working let's test the multiplayer functionality make sure that that works i'm gonna build it and then build it to my build folder and then i'll show you as soon as it's done building all right so i got this built and i have multiple sessions in here open i'm gonna do the hose here i'll do a client we'll do another client and then lastly one more client so if i move the character you're gonna see that the character is moving and then let's see i can move the character and the animations are playing correctly and we can do the same thing with this guy i can move that we can move this guy so we can go back in here and then everything it's working so i'm going to call it good i think that was a good you know 35 37 minutes of the video it could be probably shorter by the time that i finish editing but if you guys have any questions about what i just showed you today please let me know in the comments and if you want to see more videos about using netcode for game objects please let me know in the comments thank you very much guys
Info
Channel: Dilmer Valecillos
Views: 2,689
Rating: undefined out of 5
Keywords: unity multiplayer, unity multiplayer tutorial, unity multiplayer 2020, unity multiplayer 2021, how to make a multiplayer game, unity multiplayer game tutorial, multiplayer networking, unity multiplayer game development, how to make a multiplayer game in unity, new unity multiplayer, new unity multiplayer system, netcode for game objects, unity netcode tutorial, setup netcode, network manager, photon 2, photon2, multiplayer game dev, beginner, unity networking 2021, unity unet
Id: GOtE96OKyVA
Channel Id: undefined
Length: 26min 22sec (1582 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.