How To Make A Game With Unity Multiplayer Netcode | ClientRpc ClientRpcParams & ServerRpc

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody this is delmar and welcome back to my channel so in today's video i'm going to continue with netcode for game objects and specifically we're going to be looking at server rpc and also client rpc we clean our pc we're going to be using the client rpc params that are going to allow us from the server to basically notify a specific client this is going to be helpful because we're going to be using a new animation which is going to be a punch animation we're going to be doing a raycast from our face and basically when we hit another player we're going to notify that player that is getting hit so let's jump into my computer and start working on it all right guys so what i'm going to be doing today is i'm going to walk you through a couple things that i'm going to be covering such as how to communicate to other players that i'm actually punching another player and then decreasing the health of that player so when we do that we need to think about okay who needs to be notified right so if we're creating a multiplayer game we need to you know decrease the health by going to the server because the server is the one that has access to the network variables and then based on who i'm punching i need to notify that client that it's getting that you know that it's getting punched so that we want to if we wanted to run a different animation maybe you're getting hit and basically you can do that so i'm going to show you how we can use the server rpc client rpc and basically when we use a client or pc how we can actually choose which client we want to notify that it's getting punch so i have two different animators right now this is the one that you guys are familiar with which has the idle run walk reverse walk the new one that i'm gonna be you know talking about today has a new stay and the state is punching and i'm using a blend tree to basically alternate between this is basically a mirror of this animation that i have right here so this is a punch and then the other one is just mirror based on the same thing and then i'm using a blend tree so that we can have more dynamic animations so let me go ahead and pause that so that's how it's going to work and then if i go into parameters we're now going to have a punch trigger and also a punch plane which the blend is going to you know play a big role when we want to make more dynamic animations so the first thing that we need to do is i'm going to be making a couple changes so just know that i have a new scene which is demo server rpc incline rpc and this one is going to be specifically for this video i'm also going to be creating a new controller it's going to be a specific for this video because i know you guys mentioned that you didn't like me changing a specific script and that makes sense because if we want to go back to that then you know things don't change in the middle of you watching a video so so what i'm going to do is first i'm going to go into my prefabs and i'm going to clone the armature player armature network and then this one i'm just going to call it something else so that we can we can use it for this video and then this one's going to be player with ray cass and then amateur network i think that's completely fine and then if i go back i think this is going to let me do okay i see let me go back here and then if i go back i think that should be named correctly okay looks like its name is named correctly and then if i go into a scripts and there is a player control and you guys are really familiar with this player control because i've been covering this in multiple videos this is going to be the first one that we implemented i'm going to be cloning this as well we're just going to create a brand new one it's going to go ahead and do control d to clone it and again i'm going to just give it a unique name so that you guys can see the changes and i'm going to call it player with raycast control and the reason why i'm going to call it with i'm going to call it raycast is because when we punch a player we're going to have basically a ray going from our hand if we're using our left hand or right hand there's going to be a ray that is going to go you know so far and if that ray hits a client then i know that you know i need to start the action of notifying the server and also the server notifying back to the client so if we go back in here and i need to make this player player with raycast control and then if we go back i think everything should be compiling now and then i'm going to assign that script also to our new prefab so that you know that's going to be it's going to be the one that we're going to be using so we go here you're going to see that we have all these variables we can probably just copy those values i think we're going to be able to do that then remove the component and then this one is going to be player let's see player control with raycast player with raycast control there we go and i think all these values should be okay and let me see if i can do okay you know i cannot paste the values because it was a different script the other thing that i want to do as well that i noticed that was an issue specifically with this is a component that we're going to need which is going to be that network transform and i think some of you asked me don't know why do you add it uh network let me do that again network transform because you guys asked me why did i edit it and then also i had network variables well the reason why i added it is because if you look at these right here the star method this is going to designate where the client is going to be you know going to and this is going to be a random number so by using the network transform is basically going to synchronize that position initially if i don't use it i notice that it puts the player it's going to be a random so it won't the other players won't know where this client is going to be located so it wouldn't really look realistic all right so now we can go back into unity and i think that should be everything should be compiling and we should also see the network transform if it doesn't get at it then we'll add it manually i think it didn't get at it because i added this later we can do remove component and then i can also go back and go ahead and add it a couple of things i don't need the x y and z here in fact we probably don't need the rotation i'm just going to leave it in there because that's how i i tested it before but i think that should be everything in there and then if we go back to network manager we're going to now associate that new prefab to the network manager so let's go ahead and double click it and i think everything in here should be should be good so now what we need to do is we need to focus quite a bit on the on this script and we also need to so let's go back into player with raycast we also need to associate the right animation right right now i have the simple animator but we need the advanced animator which is the one that is going to have or new to parameters which is punch and also punch blame and i think now that should be everything that we need so okay so we have the network manager with the new prefab we also have that prefab with the new scripts if we double click on it we have a network transform and also the player raycast control which we're going to be extending so a couple of things we're going to need to do is we're going to need to change the player states if we go back into player state i had an aim and i shoot and i was going to do basically like a gun type of game and then i decided just to do a punch just to keep it keep it very simple so that's what basically we're going to be calling you want to make sure that you match this player punch to these parameter because we're going to be using some variables that are going to have the same name and then we're going to actually use the name of the state to determine what trigger to execute in the in the animation so now that we have that ready we're going to have to we're going to need a couple more network variables so the first one is going to be our health so i'm going to do the network variable this one is going to be a flow and then it's going to say we can say network network player health just to keep the you know everything else consistent with that naming by prefixing network and then i'm going to set this to a large number we can just do something like a thousand and then i'm also going to be doing another variable and this one is so that we can change the the blend shape the blend tree value that way when we're basically doing a punch it doesn't look always the same way and we also synchronize it to to other clients so to do that i'm also going to be doing a float and this is going to be network layer punch blend and then i'll just set that to basically to an empty we don't need to do anything in there there's going to be other things that we need to also take into consideration because we're going to be doing a raycast from our actual hands so i'm just going to do a game object here this one is going to be my left hand and then i'll do the same thing with the we need to basically reassociate these in the prefab so i'm just gonna do right hand and then we're also going to need a minimum value to determine you know at what point we're going to be detecting the the raycast so i'm going to say this is going to be float minimum punch distance and then we can set this to something like 1.0 for now and then as we tweak it we can make changes to that so now that we have those we could go in and reassociate everything we'll do that in just a few minutes now we need to implement the check punch so that one i'm going to do we can just do it right here and this is going to be avoid i'm just going to go call it check punch you can say check punch day i mean however you want to call it i think it's fine it's going to be taking a transform this is going to be you know which hand we're going to be punching with and i'm also going to be taking a vector and this vector the reason why i'm doing this is because we're going to be punching and then i need to determine you know am i going to be and i'm going to be doing a raycast you know from going going forward going up going down so we need to determine where we're going to be going from from the origin of the raycast and where in basically where are going to be or destination so this one is going to be the aim direction hopefully that makes sense it'll make sense more when we start doing the raycast and then what i'll do here i'll just do a raycast here this is going to be basically a story the hit of the raycast and i also need to do a layer mask so we'll just do layer layer mask and then we'll just say layer mask get mask and this is going to say okay what you know what is going to be the mass of what we're going to be doing a raycast again so i'm going to be punching players so we're going to be basically just get the player and before i forget let's go ahead and go into the player itself and let's change and add a new layer so that we have that already set up and we can go here and re-associate the other game objects that i want for the left hand and also for the right hand so in the layer i'm going to create a new layer and this one is going to be just player and we can go back here and now that we have that set we also need to go into our skeleton here and basically get the hand so that's going to be in let's go ahead and so here's the hips and let me go let me go down i'm going to collapse everything so that it makes more sense i'm going to go into the spine this is going to be the okay here we go i think everything is collapsed so it makes it really hard to see we don't need the neck we don't need everything inside the right hand so these are going to be the two that we're going to be doing a raycast from let me make this a little bit smaller so that we can see more of the player so basically what's going to happen is you're going to have a raycast going that way if you can see my mouse and i'm going to have another raycast going that way that way we can you know we can do a punch so if we go back into here we can say okay which one is going to be your left hand that's going to be your left hand this one is going to be my right hand and then the minimum punch distance i think that's what i said it on my demo actually it was a lot lower than that so i'll do point 25 i think that's the value that i that i use before the rotation is speed and everything else it's okay at the rotation speed i set it to something like like a three and then everything else in here should be should be good okay so we have everything that we need to that we need to associate so let's go back into visual studio so now that we have a raycast now we can do basically the actual the actual raycast so to do that we're going to be using physics the raycast i'll just do physics and then we'll just do that and then you can do a raycast from a specific transform remember in this case i need to use the the actual hand transform that we're going to where we're going to be doing the raycast from so i'm just going to do this is going to be my hand that's going to be where we're going to be starting i'm going to get the position and then i need to get basically the the same if we go hand and then transform and then i need to do the transform direction so it's going to tell me where we're going to be you know which direction we're going to be going to so we're going to do aim direction i also gonna need to get the hit so i'm gonna get that out into that variable and then we're gonna do comma and then when we do the comma we need to also specify the minimum minimum punch distance and that's going to be the variable we set up on the on the very top and then i also need to set up my layer mask right like what are we going to be colliding with when we do when we do is right cast and if we're not currently colliding we can just also show the ray i think that's going to be helpful for the vlogging purposes so for the booking purposes i'm also going to be doing the bug and then we can draw the the ray and then i'm going to say the same thing that i did above it we'll just do i'm just actually going to just copy these values right here i'll just paste that i'm also going to be multiplying this by the minimum punch distance so we can see how far that goes and we can do something like color let me do that again it's gonna be color and then the color of the ray we can just say that that's going to be yellow and then if we're not doing currently if we're not heating anything we can just maybe make it red so that it's obvious that we're currently not hitting anything when we're punching okay so now that we have that we need to actually get the component that we are currently hitting so to do that i'm going to say this is going to be my network object i can just do var this is going to be the the player that we're hitting so player and then in this case i'm going to be getting the hit transform and then i'm going to get the component and that component is going to be of type network object because if we're hitting another player more likely that player is going to have a network object because right now we don't we don't add on other different types so for now that's going to be always true and we can also do a little checking here just to make sure that that is true and if it's not null what we're going to do here once we implement the update hell server rpc is we're going to be calling update health server rpc and basically what we're going to do is going to be incrementing the health i'm just gonna do everything we hit the player we're gonna be decrementing the hell by one so i'll just have a positive number there and then i'm also going to say player he and then basically we're gonna get the owner client id i'm going to be implementing that you know later once we get once we get this working so the other thing that i also need to do so we have the update here but since we're dealing with physics i'm going to be doing the fix update let me do that again fix update just hit enter and the way that it's gonna work is we're gonna be you know we're gonna be checking for the punch so for now and we're not gonna do this on every single frame but for now i'm just going to execute it on every single frame so you guys can see how it works but what i'm going to do is i'm going to make sure that this is going to be the client that owns this and also the the owner so we basically want to make sure that you know if we're punching somebody we're gonna be checking the punch stay if i own the if i am the owner of the player and then what i'll do here i'll just say i need one for the for the left hand and the direction in this one it's going to be the up direction so i'll just do vector 3 and then up and then remember this takes the transform so i need to do transform and then for the other one i'll just do down direction and then this one is going to be the my right hand okay so before i continue i want to i want to show you how this basically looks like if we go back into unity so we're going to be you know testing things as we go so that we know that everything is working and we'll wait for unity to okay to the start and then i'm going to start the multiplayer game we're going to start the host and hopefully everything is going to it's going to show the race you guys can see now that i have it looks like i have i am wolverine right i have to raise they're really hard to they're really tiny but there's a red ray there there's also another one here and then you know if i were to punch well we haven't actually implemented the action to punch but once we do you're going to be able to see the happening okay so we know that code is working because we can see the array actually getting executed so if we go down here i i had a basically an action to when we're running we're we're holding the left shift and also the right shift i'm gonna be doing one more that is going to allow us to determine if we're punching somebody so i'm just gonna say static and then this one is going to be also a ball and then we can say active punch we can say punching action key oh you can call it punch action key however you want to call it we can just say i think that makes more sense and then what i'll do here we're going to be using the space the space key to do that so i'm going to say get key and then this one is going to be a space and there we go so now that we have that we should be able to basically make another check in here so if i go back up to or or client there's gonna be a couple things in here that i want to check for the first one is going to be network layer state well i want to make sure that i am currently punching so let's get the value and then we're going to be doing punch so if i'm currently doing a punch then i know that i'm going to be checking for those that way we don't do that on every single frame otherwise it's going to be it's going to be expensive and then i'm also going to make making sure that we are currently punching okay so that looks good so and then everything in here looks fine so now we need to go into the client input and we need to also detect when we're going to be punching so for that i'm going to so this one i'm going to do for a change motion states so anything with motion it's going to be basically the script right here and then this one is going to be change i don't know fighting fighting states and then anything with fighting we're just going to be putting in here so i'm just going to say if action punch key it's currently so if i'm holding basically the space key and currently the four inputs equal to zero so i'm only going to allow these just to keep it simple when we're not running when we're not gonna be jumping we haven't had a jump but if we were basically when there's no motion we're gonna allow the punch to happen and then what i'm going to do here is i'm going to also be updating the state so i'm just going to need to do update the player stay but in this case it's going to be a new state which we haven't done yet it's going to be punch right one caveat to this though is yeah this is going to change the animation things are going to work fine but if you remember at the beginning of looking at the script we also have a network player punch and the reason why i added this is so we can have more dynamic animation so if we look at the update player state server rpc right now i just change it just takes a state and that's it so what i'm going to do here as well i'm going to say okay if the stay is currently punched then i'm going to be updating the network so i'm gonna do the network punch blend i'm gonna get the value and then i'm gonna randomize this and this one i'm just gonna do a range and i'm gonna get a value i think i did from zero to one that way we can do the blend tree is going to do either animation where we do you know a left punch or you do the right punch or anything in between because we're blending those two and then i do something like this and these are inclusive so that means that it's going to take either it's going to generate a number from zero to one and if you hover over this you're gonna see that that's what's going to happen so this is cool because now we can make animations that are a little more dynamic so if i were to go back now to the actual visualization we're going to be able to make something that it's going to be a little bit different so if we go into client visuals right now this is going to change the trigger to basically to to punch and we're gonna be punching right but it's always gonna be the same so if we wanted to change that what i'm gonna do here is i'm gonna say well if the network if the network play is stay that value we're gonna be doing the same thing that we did below it's currently set to punch then i know that i'm going to be changing the the value of the blend so i'm going to say set flow because that value is going to be a flow and then what i'll do here is i'm going to be basically copying this value here we can get we can basically get this one either one is going to work but the variable that we're going to be that we're going to be naming is going to be blend that way it's going to be applying that to that property and then what i'll do is i'll just get the network player state another player state the actual blame value that way we can we can animate that with basically with a different visual so if we go back to unity if you look at this variable right here is called punch plane so that's basically what's going to happen here it's going to be punch and this is going to be a appending the word blade so it's going to know which variable to update okay so now that we have that completed we need to focus on on couple of rpcs that we're going to be implementing for this video so the first one is going to allow us to update the health of the player so i'm just going to do server rpc and then i'm going to do be doing a public voy update server we can actually say update health server rpc and then the way it's going to work if you remember above we're taking a health right so this is going to be the new health value or i can i can just say take away a i don't know point and then comma you can go we're going to be getting the client that we're actually hitting so this is really important because yeah we're going to be updating the the value of the health of a player but we need to make sure we don't update ourselves we want to update another client because we're basically hitting another client so this is going to be the client id so this is something that i i learned that i thought it was interesting we're not gonna be updating our health which is which will be wrong we need to get the actual client that we're hitting so for the for that we're gonna say player damage or player with damage and then i'm gonna be getting the network manager and it's going to be getting a singleton and i'm going to be able to get because this is executing on the server we're going to we're going to know all the different clients that are getting that are currently connected right so we can get the client id there we can also get the player object and i can also do get component and then this is going to allow us to well actually get the player wii raycast control which is the one that is going to be hosting the the health of the player so once we have that one thing that i would recommend that you do just in case somebody disconnects i'm gonna make sure that this is not null right otherwise we're going to be incrementing the health on somebody that disconnects and with that and that might throw that might throw an error so once we do that i also want to make sure that the the health value is greater than the value that i'm trying to decrement so i'm going to say you know what i need to get the health so let me try this one more time playing with damage it has a network there we go player health and i'm gonna do value make sure that that value is greater than zero otherwise it's going to decrement uh beyond zero and we don't want that to happen and then once we have that we can basically just do the decrementing in here which is going to be basically taking as many as many points as we want in here in this case we're passing a one but if you wanted to change that you can you can also change that okay so that's cool and all and and the player is going to say yeah my health change but the clan is going to say that right but we need to also execute a meta on the clan and that's where the new one is going to so we're going to say something like execute meta on the client getting punch right so this is what we need to do now and we'll go back to the other method so i'm going to say a client rpc and this one is only going to be executing on the client that is getting hit which is something new that i haven't covered in the in in the video so i'm just going to say notify and this one is going to say health change client rpc and you can name that however you think it's appropriate and then i'm going to say take away point that way we can print it in a log and i'm also going to be taking a couple of parameters in here it's going to be a couple parameters i'm going to set it to the default value and and this is the trick right this is going to be you know taking in parameters and this is going to allow us to designate okay which client is the one that we need to notify so that's what's going to allow us to do i don't want to notify myself so i'm going to say is honor if it is the owner then i'm going to be returning but in this case i don't believe this is going to be a problem but just in case just make sure that if for whatever reason might get notified as well we want to make sure that we return because i want to notify other players and then here i'll just do we can we can make it more complicated if i wanted to do an animation me getting hit like the the other player getting hit you can do that for now i'll just do log info and then because there's just so much time that i that i want to go over this video and then i can say something like client e clang hit or get punch and then we can also specify you know how much how many points which in our case is gonna be it's gonna be the one right and then i can say yeah i think i think that's okay another thing that we can also do if we wanted to is we can also get the you know how many points that that client currently has i'm gonna show you that in the inspector so i'm not gonna i'm not gonna do that right now but you can you know add an overlay and increment the the health and then show that as you're hitting the player okay so so that's cool and all but we need to call this guy and to call that guy i need to specify which which client is going to be executed so what i'll do here is i'm going to say okay you know i want to call from the server side i want to call that but i want i also want to take you know these are going to be the points that we're going to be taking now but i also need to determine who is going to be getting this message i'm going to say new this is going to be the same parameters and then this is going to be taking a target client id and then it's going to be an array okay there we go so we have a client promise and you can go into implementation and look at it takes a clean rpc param and also the the rpc receipt params if you wanted to receive a message and then in this case we're going to be notifying if you wanted to notify multiple clients you can in this case we're punching one client so we want to notify that client that you know that client got punched so that's what it's going to that's how this is going to work and i believe i got everything that we needed here just going to double check okay so one thing that we haven't done yet is once we're once we're basically hitting the clan we need to basically call the rpc and if i say if i say basically again remember what you're going to do you're going to tell me in the comments but i was about to say it again but what's going to happen is we're you know we're doing a rate cast we know we put which player is getting here which is going to be another client and then we're going to be calling the update health server rpc we're going to be decrementing you know their health i'm going to be passing in the owner the basically the player owner client id that's gonna be calling into the server the server is gonna say okay i wanna know who which client that is that is getting hit and then i'm gonna decrement their health and i'm to execute a method on the client side to tell that client yeah you're getting he so that's how that it's going to be so let's go ahead and go back and i did talk a lot so hopefully everything that i just show you it's going to work and let's see so the first thing that i'm going to do is i'm going to make sure that our animations are currently working so i'm going to go ahead and hit play and then we'll do a bill right after that to to double check it okay so we can run let's see if we can punch and we can punch it's a little bit slow but i think it we can if you wanted to change it if you wanted to change the speed of the animation you can do that i can also select my layering here you can see how that it's currently working i can you know i can do all of those so let's get closing here because i want to show you and i'm going to be focused on this area right so i'm just going to focus on the i want you to look at the hands right as we're heating there are two two lines that are displaying on the on the character fists and that way we can determine okay are we hitting somebody else or we're not hitting somebody else so that's how that it's going to work so now that we know that that's working i can do file build settings this is going to be the the scene that we're going to be building which is the demo server rpc and client rpc and i'm going to go ahead and do a build select folder and build it there all right guys so i have four different sessions currently open let's go ahead and do the host to be this one right here and then i'm gonna do a client here we'll do another client here and i'll just move it move it around so that they are you know in different locations and i'll do another client plan right here so let's try from the server side right so the server side or the host is going to be this guy right here so if i try to punch that person i'm currently not getting anything and i think because the ray cast might not be you know it might not go far enough let's try that again so we're trying to punch trying to punch and we don't see we currently don't see the the punch okay that's fine we'll just troubleshoot it together i know you guys told me that you wanted to see when i'm having issues well this is how we're gonna troubleshoot i'm gonna go ahead and hit play and i'm gonna be running another session somewhere in here in my computer and i'll just do this one is going to be the host and then this one right here is going to be the client so so what i'm going to do here is i'm going to go ahead and focus on this and i'm going to look at the local player which is gonna be the one that i'm currently looking at here and i'm just gonna put it maybe we can just maybe make this a little bit bigger so we can see what's happening and right now nothing i mean it's not i don't think it's executing because we don't see it let's try let's see let's get closer in here and we don't see anybody currently getting getting punched okay so one thing that i can do here now that we have this i can change the punch distance so we can try something big maybe like 0.6 and we can see if that it's going to fix it and currently that it's not for some reason it's not getting it's it's actually not doing the right cast let's try that again so i am punching the player i have my oh okay i see what's what's happening if you notice i for some reason the the player layer is not set so let's try let's fix that i'm going to close out of this and i'm going to go into the i thought i thought i said it but that wasn't set so i'll just go ahead and set it and nope i'm going to just set it to that object only we'll go back here and then let me try and do a new build all right so i got two different sessions in here let's go ahead and try the host here and then the client here and i'm just going to go ahead and try to hit the player and you can see that this one is not getting locked but this one is getting a lock right and that one is getting punched because this is the one that it's getting notified so what if i what if i wanted to do do the other way right i want to so i am playing in this session but this is the one that needs to get notified so we can go ahead and and you can see that that it's currently working so let's go ahead and try this one more time but let's change the camera view and this one i'm going to add let's go ahead and add a a city machine brain and then i'm also going to be adding a virtual camera all right guys so in this build i have four different sessions so let's go ahead and start the host let's go ahead and start a client another client and another class so we should have player three basically player zero through through three and then i'll just move this one around so let's go ahead and start with the server let's see if we can hit player one and and remember like which ones are which so if i hit that you're gonna see player one is getting punched the other one are not getting notified in fact you can see player one is here and then this is the one that is getting punched what if player one what if we wanted to do basically the other way so i'm gonna go ahead and punch player zero and this one should get notified and you can see let's see let's get closer and you can see that player you know zero is currently getting punch on the face and then in this case let's say player zero oh let me go back here to the group and this will look cooler if i had animations but unfortunately right now so this one i'm basically hitting two players and you can see one is hitting the player one there and then the other one is hitting player zero let me go ahead and hit player two so this one should be notified and you can see that that one is notified so that's everything that i wanted to show you guys today if you guys have any questions about this please let me know in the comments thank you
Info
Channel: Dilmer Valecillos
Views: 1,678
Rating: undefined out of 5
Keywords: clientrpc, serverrpc, unity relay service, unity udp, unity udp server, unity udp networking, unity transport, unity multiplayer, unity multiplayer 2021, how to make a multiplayer game, unity multiplayer game tutorial, multiplayer networking, new unity multiplayer system, netcode for game objects, unity netcode tutorial, setup netcode, network manager, multiplayer game dev, ClientNetworkTransform, cinemachine, clientrpcparams, remote procedure call, multiplayer unity
Id: ktGJstDvEmU
Channel Id: undefined
Length: 35min 1sec (2101 seconds)
Published: Fri Dec 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.