How To Create A Multiplayer FPS In Unity, Part 2: Adding The Gun Animations + Setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another video today we're going to be working on the second part of the FPS controller tutorial so today we will be doing the shooting and the guns in the game I'm only going to be putting one gun in the game for now and then obviously we can expand on it later um for this tutorial you will need a gun asset I've gone and uh got one or made my own um you can find plenty of them on sketchfab if you do not want to make your own and I've just got a real basic simple animation um which and also just a Reload animation that I put together in blender it's nothing to there's even some you know clipping there but it's it's good enough for this tutorial before we start the video I'd like to thank my patrons Irish Oaks 16 Thomas warrell Brandon Springs Carl Sterner Jayden Barber Jared Budd and David W Robinson for their support on patreon it really helps if you would like to become a patreon yourself links in the description of becoming a patron uh gives you video shout outs access to a role on disc on my Discord server links also in the description for that and our project downloads and one-on-one help and also don't forget to subscribe before we start the video it really helps out we're gonna try and get the 500 before the end of the year that would be awesome so I'm just gonna go ahead and import the 1911 model into our Unity project and I'm actually just gonna make a new folder called full folder and I'm just going to call it models cool so if we drag the 1911 in it is huge right um and also it's not on our plan all right so what we're gonna do is underplay a camera we're actually going to create a empty and this is going to be the gun parent uh and the reason for this is because this is what we're gonna move when we don't want to move the gun itself um and the reason for this is because the origin points of the guns can be wildly different so if we have it under the gun parent we can essentially place this the gun exactly where we want to be also if you want to change where the the gizmos I guess you'd call it for the transform of the gun uh to the pivot point you can actually just set it to Pivot here instead of Center it really helps cool so we'll get this into a position that makes sense for us I think about there and that means that when we move it it moves from the handle which makes about it it makes sense for a gun next we're just going to scale down our gun a bit so I'm probably 0.2 I know it's uh that's quite low but it's the gun is very big that even even that's a bit too big I'm gonna put it down to 0.13 cool and I'm gonna have to reposition [Music] okay now if we think about how we are going to move the gun to the position of the the the the scope and position okay the ads position and the kind of uh hip firing position we want to create two points where that's going to happen so a good way to do this is to click play on the game and actually figure out uh where they would be there also let's let's find this gum all right it seems like it's kind of opposite to our position of our camera so we can just set it to negative 90 on the Y and it should be yep there it is it's there in our inspector so now that we have it right in the middle we can actually find a perfect position for it to be at so we can grab the gun parent and move this around until we find a position for the hip firing that looks about right there we go that looks roughly correct I don't know you can always tweak around with it now I'm actually gonna go into the transform and copy the component now when we come out of play mode it's not going to save at all obviously because there was a instance of the player and instead we're gonna copy the and we're going to paste the component values into the uh gun parent when we go into our resource player resources so now if we go play again what happened there we obviously didn't do it correct I know always a little bit of tweaking I've got it that looks decent I'm going to copy the component values and the gun parent we're gonna paste the component values and let's just try this one more time awesome looks decent okay so now that is the position for the hip firing so let's go over here to another player camera let's create a create empty and we're going to call this hip fire position so we're going to copy the kimono values and then paste them into the hip fire position and we're also going to create an aim down the site's position now a good way to do this is again we're just going to click play host and a really easy way to do this is under hip fire position we call this 80 I should probably do that in um here we go adsp and we're actually going to reset the transform on this and the only thing we're going to do is where's the play camera pointing that way okay we're gonna drag the adsp forwards a bit so we can set the gun pair into the adsp and reset the transform all right let's go into our game post and then it's right smack in the center so now what we can do is we can bring down the gun parent just we can bring that ads sorry just a little bit to be right smack in the center of our camera you just gotta tweak it until you find what looks about right that looks about right for the scoping so I'm going to copy the component values and paste them into our adsp position and we're just going to uh sit the gun parent off from the adsb and we'll just reset the position and we're actually going to set it as a hip fly position so let's copy the Commodore values and paste them into the gun parent cool so now we can actually work on a manager for the weapons so we can create a new C subscript and call it weapon manager and what this is going to do it's going to control all of our weapon movement I'm not going to worry about making it multiplayer just yet we'll be doing that afterwards but we will be adding the using mirror dependent uh reference up top I'm going to make a reference to the ads position so and the hit five position so we're going to call it uh public transform hip fire pause and a public transform called adsp or ads pop edius pause we're also going to make a public transform uh for the gun parent and let's also make a public float called the aim speed and this is and the aim speed is going to be how fast it uh goes to the aim uh position we're also going to be making a using a Unity engine at the dot input system and we're also going to be making a header up here called uh input and make a public input action and let's call this our aim action now we're going to make a public input action called reload action and a public input action called Shoot action and let's also make a public weapon and let's call this current weapon now as you can see uh when we do this it's gonna come with an error and it's going to say type of name save weapon cannot be found so let's create a new c-sharp script called weapon and this will contain all the data for our weapons to use so first of all I'm actually just going to remove everything and I'm going to make a public and here we can go a string called the name not like that will really matter in our case because we only have one weapon but for a big multiplayer FPS games you'd want to probably have different names for them probably a public int called the ID so we can call this from different places a public game objects and this is going to be no actually a public um and here we can go fire type fire mode um and call this fire mode now down on underneath we're going to create an enum and we're going to call this the fire mode and we'll actually make it a public fire mode and here we actually going to create Simi Auto and burst the three kind of fire modes that you that you'll want for all your different guns then let's just create a void called Shoot and let's actually make this a public void so it can be called all different places now a really important and really interesting way of why we're doing putting the shoot inside the weapon and not inside the weapon manager is because if we do inside the weapon it makes it a hell of a lot easier to be able to make things like turrets tank uh tank turrets or whatever you want to call them and mounted guns uh because that means we can call the shoot function from anywhere cool let's now go ahead and enable our input actions so we can go aim action dot enable dot enable the reload action dot enable and I'll shoot action dot enable and then on disable similar to like our last one we all reload action dot disable aim action.disable and shoot action.disable so the aiming is a little bit different for the input action because we actually need ways of knowing whether or not we're aiming or not aiming so we're gonna make a public pool not a public ball just a ball called is aiming we're also going to make a Boolean called is shooting and this will control to see whether or not we're shooting and whether or not we're aiming because those will be very important uh later on now on a week we can actually call some functions which are going to help um with the aim actions and setting the is aiming so we can check aim action dot starters plus equals to subcontext and we can equals more than and we can now give it the context and we can see it is aiming is equal to true not start not stated started we can then give it aim action dot dot performed performed plus equals to context equals more than is aiming you go true I'll get over what these are doing in a second then we'll give it aim action dot canceled plus equals to context it's more is same is aiming and this time we're actually going to do false so what we're doing is we're checking if the aim action so let's say we are using a right click for our aim action If We Hold right click it's going to say it is Amy equal true if we are performing the aim action if we're holding down right click it's going to say it as true and then as soon as we let go of our right click a here we're canceling it it's going to set it as false this is giving us an on off effect when we hold down the button and we can also do the same thing for the shoot so we can go shoot action dot started plus equals the context and we can get give it context is shooting equal to true and it's the exact same as the before performed context is shooting equal to true and then shoot action canceled context is shooting boss awesome so now we can actually get on to the update and uh using our aiming so what we can do is if is aiming is equal equal to true [Music] and an else statement so what we can do if else if as aiming is equal to true we can set um we can move the gun parents position to the hip fire pause and the ads pods [Music] and then we can also uh move it at the speed of aim speed so let's do that Bingo gun parent dot position is equal to Vector three dot love and here we can alert between uh the values of gun parent.position so we're moving the gun period off position and we're moving it towards the ads pause dot position and here for the uh the time we're gonna put in time to Delta time uh multiplied by our aim speed or up and then we also need to set the rotation because it may be different so we can go gun parent.rotation is equal to quaternion dot slurp so we're going to be using the slurp function for this and we can go gunparent dot rotation and we're going to set it to the ads pause dot rotation time dot Delta time multiplied and here I'm actually going to set it to just 10. um we can also just create one here public float aim rotate speed multiplied by our aim rotate speed instead of 10. and we're doing the exact same in the else statement however instead of the ads pause we're using the hip fire pause awesome let's go back to Unity and see if we've done anything wrong it looks like we're all good now if we go on to our player uh just don't worry about this we will get to is shooting in a second so as you can see we do not have our weapon manager on our player so let's go ahead and add that awesome now we have a bunch of stuff that we can do we can add in the hip flying position we can add in the ads position uh we can add in the gum parent aim speed as we'll say three for now and 15 or 5 for the rotiss B maybe current weapon we don't really have a weapon yet um and let's set up Sim aim actions cool so let's check everything cool okay let's click play we have our prefab open let's play host now as you can see when we right click it should go between our different sessions it is quite slow so let's speed that up a little bit we can set our aim speed we'll set that to some more something more like 10. that looks about good and the aim routine speed that that is whatever it looks fine cool now let's actually go ahead and apply these changes and I'm actually going to change the rotation on the hip fire position slightly foreign now as you can see there is gun clipping and we will actually be doing something a little bit different for the gun rendering so let's go ahead and go into our player now under our camera we are actually going to be creating another camera and this is going to be called the FPS render cam the reason for this is currently if we run into a wall there's actually clipping that's going on with our gun into the wall in most FPS games you will not see this and therefore we need to fix them however the first thing we're going to do is actually fix some clipping of our own camera and we can set the near clipping plane on our camera to point zero one let's also make sure that the gun parent the layer is set to a new layer and we'll call it the guns and let's set the gun parent layer to the gun and also yes change children now on our actual player camera let's turn off the culling mask for the gun this can be found under camera culling mask and just click the gun layer to make sure the text are not there then on the FPS surrender cam we're going to be doing nothing but the gum and it's clear Flags uh to down clear so now if we click play and click server now if we go it's actually not clipping because the cam it's getting rendered in front of it it can look a little bit weird to other players however to the your player the FPS player it shouldn't actually clip at all at any point and as you can see our camera is no logging clipping no longer clipping with the big box as well cool so now let's set up some animation for our weapon in under a weapon let's also create a public animator atom and let's make a public Voyage called reload let's set Adam dot set trigger reload and also in the shoot let's also go anim dot set trigger shoot [Music] cool let's set up some animator now let's go window animation animator and let's open it and it will open up a new animator tab let's also create the 1911 as a resource in our resources folder AKA making a prefab let's also add the component of type weapon and let's give it a animator as well let's drag the animator and set it as semi ID of zero and call it uh 1911. now let's create an animator for the 1911. so we'll go models 1911 create and here we're gonna select animator controller and let's call this the 1911. and as you can see a new animator window is going to show up inside the animator window and let's go ahead and go under parameters and create a trigger called reload and a new trigger called Shoot cool so I once I have uh imported my 1911 into my project and had have all of these in um we have idle reload and shoot now if you do not have animations I would highly recommend make some there are a bunch of really easy tutorials on YouTube um of making uh animations for your guns I will link to to some rigging tutorials in the in the video description which will really help you out cool now I'm going to have an idle state for my gun a shoot State and a Reload State now all of these will just happen from one another so uh based on the triggers so from the idle let's make a transition to the shoot and a transition to the reload and vice versa back from the idle to the shoot we're going to set the trigger as being a shoot and this is going to have no X at time whereas the one coming back is going to have exit time next the idle to the reload is going to be the reload trigger without extra time and it will have exit Time coming back now let's just make sure that our uh information from our 19119 old player has been submitted to our prefab asset which it hasn't so let's go ahead and overrides apply all now whenever we make uh changes to the 1911 prefab assets on in itself we can actually it will change for the player let's then drag in the 911 animation controller to itself and now we can call the shoot and reload from our weapon manager cool so an update let's create a switch statement and here we can actually um sit we can check some data so let's check our current weapon dot fire mode and in a case let's create a fire mode dot Simi break [Music] case fire mode dot Auto break and case fire mode dot burst break cool and let's just remove default [Music] so this video is already going to be long enough so I'm actually going to split the guns into two videos this one is going to be the animation and the second one will be the uh shoot actual shooting of the gun cool so let's just set the semi now so we can go current weapon dot shoot and before the switch game and we're actually just gonna go reload action dot performed plus he was the con to context equals more than and here we're gonna call the current weapon Dot reload now we will have different ways of it working based on uh all of these because you know if we are shooting uh if we if our current is shooting and all that kind of stuff it's it's kind of complicated anyway let's get let's get back to Unity uh let's see what we screwed up weapon does not contain a fire mode can I spell it I spelled it no um no Capital awesome go back to our Unity editor and let's see if our animation works current weapon let's go to a player guard weapon is equal to weapon and let's make sure our weapon has applied changes to it it does cool let's click play and see what happens and as you can see it is looping okay uh let's change some stuff and let's see if we can fix this [Music] what we're actually going to do instead is we're going to set if current weapon dot fire mode is equal to fire mode not Auto and this actually needs to be equal equal and that's a lowercase fire mode [Music] um then we can go if is shooting is equal equal to True uh that we can call current weapon dot shoot um else else down here we can go shoot action dot performed plus equals to context and we'll give it the context of the car weapon dot shoot awesome and that will make a little bit more sense in the next episode where we fix um all this and make it actually work as intended with actually shooting a projectile let's click play and click host server and it shouldn't be constantly shooting anymore as you can see we can shoot and we can reload and if we we can also shoot while it is here now I just realized there's some gun clipping and let's quickly fix that before we end the video so what we the way we can fix that go into our player and it's just simply our FPS render cam then the eclip plane is way too close this will play and see if this works it does and let's quickly turn this into some multiplayer code so let's just go if is local player then we can return out of the loop oh we need to make the class of network Behavior that's mine go back to Unity compiling and it should all work the same yep we can do everything cool stay tuned for the next episode where we fix everything and why is my gun clipping again apparently the clipping planes had something to do with the gun clipping to the into the thing let's see if they've fixed it it did just turning up the clipping Plane Slightly to point two seemed to fix it perfectly anyway guys hope you enjoyed this video in the next episode we will be getting the guns working and shooting properly if you guys enjoyed the video please give it a like Please Subscribe and I'll see you guys next time [Music] [Music] [Music]
Info
Channel: Joe Rickwood
Views: 2,471
Rating: undefined out of 5
Keywords: Unity3D, How to make an FPS, FPS Controller Unity, How to make a game, how to code, c#
Id: LbImBjqF6bM
Channel Id: undefined
Length: 26min 51sec (1611 seconds)
Published: Thu Oct 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.