How To Program A Gun In Unity! - Programming Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so the first thing I'm gonna do is create a plane and just kind of get the scene set up okay now that we have a plane up I'm just gonna call this ground and now we can start actually setting up the player so what we're gonna do is create a capsule bring it up to zero on the y-axis and call it player I'm gonna give it the red texture and we're just gonna get rid of this capsule Collider okay we're going to grab the main camera and put it on to the player resets and give it a Y of around the 0.8 okay so now that we have the the main camera on the player we're gonna grab this model it was made by jovis and if you want to know how to make this exact model or where to get it I'll leave a link in the description in order for you to go watch this tutorial when you drag it in it might be a little bit bigger just go ahead and give it a scale factor of 0.5 and it'll be this size what you might notice and what you might not like is that there is no arms attached to this gun so what I'm gonna do is I'm gonna use a old model that I made back in the day and if you'd rather use this one over Joyce's model just go ahead and look in the description I'll give you the link straight to it but I'm gonna show you how to use Jo vistas model with my hands so go ahead and grab my hands and unpack the prefab you can delete the mag handle and front metal and as you can see the a K is entirely gone so now grab the a K and drag it onto the player arms go ahead and give it a position of negative 0.05 negative 0.02 five and negative 0.1 as you can see it almost perfectly fits into the hands okay so now for the hands themselves we're gonna go ahead and put it onto the main camera scale it down 0.75 and give it a position of 0.5 negative zero point four one on the Z one on the rotation and 0.75 for each of the scales now for all the values I just gave you it doesn't really matter the positioning of it too much if you prefer the v-model to be up here or you know like this that's perfectly fine what you might notice is the camera is actually clipping into it so you want to click the camera go to where it says clipping planes and change it to zero now you can actually see that on the camera of the gun goes all the way to the camera okay so now into the tutorial part um first thing you want to do is you want to go on to the gun let's just go ahead and call it a K 47 and add a component called gun controller so the first thing we're gonna actually do is get rid of all of this and we're just gonna do the default variables that are gonna like differ from every single gun so we're gonna go header gun settings what kind of variables does a different gun have from each other they all have different fire rates so we're gonna do a public float fire rate they all have different clip sizes so public int clip size so we're going to go public and reserved ammo capacity and since these are static things that won't change throughout the code now we want to create the variables that will actually change such as how much ammo there is in the clip how much he has extra and just stuff like that so we're gonna go ahead and go variables that change throughout the code we're gonna do bull canned shoots we're gonna go int current ammo and clip and int ammo and reserve and in the current state these things don't actually do anything so let's go ahead and give them values inside the start function so we're going to go current ammo and clip is equal to clip size then we're going to go ammo and reserve is equal to reserved ammo capacity and finally we're gonna go can shoe is equal to true okay so what we're doing here is we're assigning these variables but we're not actually using them at any point so we're just gonna set up a very simple shoot function so we're gonna go forward updates the reason we want to do this and the update function is because we're detecting input so we want to go if input get mouse button zero so if the player is currently holding left mouse button and the player can shoot and the current ammo in clip is greater than zero then we want to execute this code so whenever the player shoots we want canned shoots to be false we want current ammo and clip to subtract 1 and finally we want to start what's called a quarantine so quarantines are cool because they actually run off of main thread which means that we can work with timers and different things without it actually freezing the Unity game and we're gonna be using it in this instance in order to shoot the gun so we're gonna go start quarantine shoots gun now to actually creates a quarantine if we want to go ie numerator so as you can tell ie numerator doesn't exist so what we want to do is to using systems that collections up here I think I removed it earlier but just add it up there if you don't have it call it shoots gun and what we're gonna do is since this isn't taking up the main thread we can actually do yield return new waitforseconds and then we want to wait the fire rate so what this is gonna do is it's gonna run this function and then it's gonna wait however long our fire rate is and for this instance it's 0.1 seconds and then we're gonna tell the player hey you can shoot okay so if we go back into the editor and go to debug mode we can hit the play and you could see the player can shoot the current ammo he has in his clip is 30 and he has 270 bullets in reserve now if we go into the game and hold left click every 0.1 second it'll remove one bullet from the clip and you can see it goes all the way down to zero and stops so what you might notice is that once you hit zero we actually have no way to reload the gun so we're gonna implement that right now we're going to go inside the update function and simply add else if so we're gonna check if the player hits the R key so if he hit R and the current ammo and clip is less than the clip size meaning that let's say he has 30 bullets in his clip in the maximum amount of ammo he has in his clip is 30 he won't be able to reload his gun we also want to check if the ammo in reserve is greater than zero so what we're gonna do is we're gonna create a integer and it's actually going to hold the amount that we can add to the clip so we're gonna grab the clip size for example the a K is 30 and we're gonna subtract how much ammo there is currently in the clip so let's say our clip size is 30 and there's 27 bullets this will mean that we need to add three bullets to the clip so what we're going to do is we're going to go if amount needed is greater than or equal to ammo in reserve we're gonna go current ammo and clip plus equals ammo in reserve and then we want to subtract what we just added from the ammo and reserve and let's say that the amount needed was actually less than the amount in the reserve we're gonna go else if oh sorry we're gonna go else current ammo and clip equals clip size and finally ammo and reserve minus equals amount needed now that we're back in unity we can hold left click you can see there is 22 bullets hit R and it'll remove 8 bullets from the reserves and add eight bullets the current ammo if we go down to 0 if we are 30 bullets are removed and if we keep hitting our nothing happens because this clip is full visually there isn't anything telling you hey I'm shooting a gun so I'm gonna do is I'm gonna actually make muzzle flash the way we're gonna do this is by going to the ak-47 going to UI and adding an image so what you can see right now is that the image is displayed over his main camera and what we want to do is we want to actually give it a point in world space so the way we do this is by going to the canvas and setting render mode to world space finally we can reset its position give it a width of whatever you want I'm giving it 10 and now you can see that this image now renders in world space it's right in front of the player so I'm gonna give it a width of 0.1 now let's try 0.25 and a height of 0.25 I'm going to grab the image put it in front of a players muzzle and as you can see that's actually all we need to do so now inside of code we want to go back to the top and create a comment saying muzzle flash and what we need is we need to go to this canvas and we need to pull out this image component so we're gonna go public image and call it muzzle flash image you can see that we don't have the name space so what you need to add is unity engine UI in the top okay so on top of that image we actually need the sprites of all the muzzle flashes that we're going to be using so we're gonna go public sprites and we're gonna call it flashes so what we're gonna do is right when he shoots the gun we're gonna start another Quarantine but this time we're gonna call it muzzle flash now if you remember the way that you creates a quarantine is by going I II numerator and calling it for quarantine in okay so what we need to do is we need to give this image of sprite give it the Alpha of 255 and then 0.05 seconds later we want to give it enough of zero and the way we're gonna do this is by going muzzle flash image the sprites and we're gonna set it equal to flashes we're gonna give it to the index of random range [Applause] between zero and flashes dot lengthy then we're going to set the alpha color to 255 so we're gonna go muzzle flash image dot color and set it equal to a color dot white then after we give it a color in the muzzle flash we're gonna yield return new waitforseconds and then we're gonna wait 0.05 seconds so after point zero five seconds has went by we want to actually get rid of the image and give it an alpha of zero so we're gonna go muzzle flash image dose rights is equal to null and muzzle flash image color is equal to new color zero zero zero zero so that's actually all we needed to do we can go back into unity and if we go to the ak-47 we can see that we require an image and some flashes so we're gonna drag the image into the image and let's call this muzzle flash and we need to put in some sprites okay so I have my images and we're gonna go ahead and select all of them and give them a texture type of the sprite okay so go to the ak-47 press this little lock here select all of your sprites and drag it into the flashes so now if we start the game you can actually see that as we shoot the bullets different muzzle flashes appear the next thing I actually want to do is I want to be able to aim the gun so we're gonna go to visual studio and we're gonna create some new variables this is gonna be for aiming we're gonna do a public vector three call it normal local position hit public vector3 call it's aiming local position and then a public float called aim smoothing we're gonna give it a value of 10 okay so now in the update function we're gonna write determine aim so the reason that it's red right now is because we actually need to create a function called determine name so we're gonna go down here we're gonna say void determine aim so this is gonna be relatively simple what we're going to do is we're gonna store a target position and give it the normal local position and this is what the guns going to default to and then what we want to check is if the player is holding down the right mouse button then we want to change this target position so we'll go input gets mouse button 1 and this checks if the player is holding the right mouse button and if he is we'll set targets equal to aiming local position finally we want to go vector3 desired position and set it equal to vector $3 and what we need for a loop is we need to give it a starting position ending position and then how long it'll take to actually get to that point so we're gonna pass in transform that local position give it two targets and we want to do it over a period of time Delta time times our smoothing okay so now that we created where we want the gun to be we want to actually assign our gun to that position so we're gonna go transform that local position and set equal to the desired position so here's when things get tricky what you want to do for the normal local position is assign it your default view model so for me it's at 0.5 negative zero point 4 and 1 now you're aiming local position is wherever you want to put your gun whenever the player is aiming for this specific model in this specific gun we're gonna give it a position of zero point zero three seven five zero point two four sorry negative zero point two four and zero point seven five if we hit to play you can see it is in its default position and if we right click you can see it's aiming exactly in the middle of the screen okay so what you notice is that while you can right click there's no there's no way you can look around with the player so we're gonna implement that now what we're gonna do is go back to our variables creates a header called mouse settings and we're gonna make a public floats mouse sensitivity and a vector2 that'll hold the current rotation of the player inside of update we'll create a function called determine rotation and right above the term a name will go void determine rotation so inside of here we want to get the mouse input so we'll go vector to Mouse axis and we'll set it equal to a new vector to or passing input get axis raw mouse X and input.getaxis raw of mouse Y ok so now that we stored the mouse axis we want to multiply up Mouse axis by the sensitivity and then we want to add it to our current rotation so we'll go plus equals Mouse axis now we're actually changing our current rotation but we're not applying it to our player model so we're gonna go transform route the local rotation set it equal to quaternion angle axis and we'll pass in the current rotation X and apply it on an axis of vector 3 up now we want him to be able to look up and down so we're gonna go transform dot parents the local rotation instead equal to quaternion angle axis we'll go negative current rotation dot Y on an axis of vector 3 dot right so this is going to allow us to actually look around but there is an issue yeah let's fix that so the way we're gonna fix this is by going to this rotation and we're actually going to go current rotation dot Y is equal to mass F dot clamp we're gonna pass in our current rotation dot Y and we're gonna clamp it between negative 90 and positive 90 now what this is gonna do is when the player looks up 90 degrees he cannot go past that and when the player looks down 90 degrees it will not go past that now if we go back to unity you can see it stops both 90 and 90 so what I actually want to do is add a little weapons way whenever you look around I feel like it'll make the game look really good so the way we're gonna do that is by going to the determine rotation function and going transform dot local position plus equals of vector three of mouse to axis and we're gonna multiply that by a variable called weapon sway amount and then finally divide by 1000 so we need a variable now call the weapon sway amount so what we're gonna do is go to the top we're gonna go public float weapon sway amount and I'm just gonna give it a value of 10 so back in unity you can see that whenever the player looks around it'll go so I'm looking left it'll move to the left I'm looking right it'll move to the right same with up and down now if you don't like this you can actually switch the weapon sway to a negative number so that it goes in the opposite direction I personally like that much better and I feel like it's a little exaggerated so I'm actually gonna give it a value of negative one and I think that looks much much better so the next thing on our list is we actually want to add some recoil whenever the players shooting so the first thing we're going to do is go back to the top we're going to create a comment called the weapon recoil and we're going to make a bull called public bull randomize recoil a public vector - called random recoil constraints and finally public vector - recoil pattern let's also make a comment you only need to assign this if randomize recoil is off so inside of the shoot gun function we're gonna add a function called determine recoil I'm gonna move this above because I prefer that and now let's actually create a function called determine recoil we're going to go void the term in recoil so the first thing we want to do is we want to move it towards the camera and we can do this by going local position - equals vector three dot forward times zero point one F what this does right here is whenever we shoot you can see the gun actually moves back but there's no actual recoil being applied to the camera so the way we're gonna do that is by checking if randomise recoil so if the player is wanting random recoil we go floats X recoil is equal to random dot range between negative random recoil constraints X and positive random recoil constraints X and then now we want a random recoil on the y axis so we'll go float Y recoil and we'll set it equal to a random dot range between negative random recoil constraints dot Y and then positive random recall the constraints that water finally let's pass this into a vector to call its recoil and we'll set it equal to a new vector to will pass in x recoil and Y recoil and finally we'll add it to the current rotation of the player so back in the editor you can see that there's these two vector two's here once called random recoil constraints and I'm gonna give it a value of negative 1 and positive 1 and I'm just going to enable random recoil but you can see that I'm not moving my mouse right now but the player is getting thrown everywhere now what if you want to predefine your own recoil pattern well the first thing we're going to want to do is change this recoil pattern vector 2 to an array and go to the determined recoil function and say else so if randomize recoil is off then we want to create our own recoil the way we're going to do this is by going into current step we're going to set it equal to the clip size plus 1 minus the current ammo and clip we also want to go current step is equal to mass F dot clamp current step and give it a value between 0 and recoil pattern length and since math F dot clamp is inclusive we want to subtract 1 from the length finally we want to pass a recoil pattern get it in index of the current step and we want to add that to the current rotation so now that we're back in unity I have random recoil off let's go to the recoil pattern and for the sake of the video I'm just gonna give it 5 steps the first step will move it 0.25 writes and 0.25 up and then for the remainder of the pattern I just wanted to continue in one direction going one up and a 1 to the right so now you see it's a predefined recoil pattern so that's actually it we're done with the gun but in order to make things a little bit more fun I want to be able to actually hurt the enemies so inside of the shoot gun function we're gonna go raycast for enemy and now let's create a function called raycast for enemy and we're just gonna do a simple ray cast hit call it hit and we're gonna check if physics thought ray cast we want to go from the camera so transformed parent dot dot position and we want it to go transform parent dot forward so directly in the middle of the screen we wanted to push hit into our variable hit so whatever it hits we want to give it a layer mask and the way we do this is by going one these two arrows and then layer mask dot name to layer and we're going to call it enemy so we're gonna go ahead and do a try-catch loop and what a try-catch loop does is it tries to attempt it attempts to perform all the actions inside of the tribe but if there's any errors throughout it it'll jump to the catch loop so first of all we're gonna type debug log hits an enemy we're gonna grab that components rigidbody if we can we're gonna set the constraints to none and finally we're gonna add forests away from the player so transform dot parent dot transform forward times 500 we can go back to unity and inside of unity creates an object I'm gonna make it a capsule reset its position give it's the red texture and we're gonna make a new layer call its enemy and we're going to assign this capsule the enemy we also want to give it a rigidbody and if we hit the play and shoot it you can see it launches away from the player
Info
Channel: NikNax
Views: 20,623
Rating: undefined out of 5
Keywords: Unity, Blender, Programming, Tutorial, How To, Beginner
Id: om-SS-CBZ8g
Channel Id: undefined
Length: 30min 48sec (1848 seconds)
Published: Mon Feb 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.