Unreal Engine 4 - Weapon System Tutorial (1/4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my tutorial on how to create the basics for a weapon system in your game I'm just gonna go ahead and run this to show you what its gonna look like when we're done so you have a character here and you can walk around and you can walk over to guns and pick them up pressing the F key and when you have a gun in your hand you can aim down sights with it and if you look down you can see player looks down towards the ground and when you look up he looks up towards the air and you can also fire and it fires these little balls I just created just you can kind of see the bullets better and it also has a little muzzle flash and you can see the character also plays a little animation of him shooting and you can walk over to other guns and pick them up so the way I have it set up is that there's two slots so if you already have one in your hand the second one will just go on your back and you can switch weapons at the press of a button and again you can fire this gun and if you try to pick up a gun and you don't have any slots left it will drop the gun that you currently have and you'll pick up the new one and you can see it has some nice physics involved when I drop up or when I pick up and when I drop the guns and you can also Crouch and shoot as well I forgot to mention that but yeah so this is just the basics obviously there's things that you can add to this to make it even better you know like animations for switching guns and all sorts of stuff but this tutorial is probably already gonna be sort of long so I didn't want to make it any longer I just wanted to give people a good idea of how they would get started on something like this and it's using all free content so you should be able to follow along just fine and so yeah with that being said let's go ahead and jump right into it the first thing we're going to do is actually get that free content so if you open up the epic game launcher and go to the marketplace I'll show you which ones you need and I believe they're all or I believe they're both free so the first one you want is called FPS what bundle I think yeah FPS weapon bundle so go ahead and download that one so you can see it's free and then the other one you need is I think it's called anims pack yes this one so animation starter pack it's from epic themselves and go ahead and add that to the project as well or download that as well and then once you have them downloaded you can add them to your project so we're gonna create a new project from this so we can just do it totally from scratch so come here and watch whatever version of unreal you're currently on I'm on 4.2 4.2 I'm gonna go ahead and watch that one and then we want to create a new project so looks like games next and we're gonna be using the third person as you saw so it's like third person okay next okay I don't think we need any starter content so you can just leave this blank and we have to give it a name I'm just gonna call it weapon system and create project and go ahead and wait for this to create alright so now we have a new project here and the first thing we need to do is add the two content packs that we just downloaded so open up the launcher again and in the marketplace or sorry the library tab scroll down to where you have your vault and so inside here will show you everything that you've downloaded and we want to add this one the animation starter pack so hit add to project and search for the weapon system and add it to the project and do the same thing for the FPS weapon bundle oh why did I guess it decided to close open it up again come back here let's go down I can search FPS we go add project add it to the weapon system projects and you can see now it's loaded alright so if we come back to our project now we have our animation starter pack and we have our FPS weapon bundle both added to you our project and they are ready to use so I'm gonna real quickly move this over here just like in references and we can get started so the first thing we're gonna need to do is sweeps swap out this character for a different character because you can see if you've run it right now he doesn't quite walk the way we want him to walk we want him to always be facing away from the camera since it's gonna be a shooting game and fortunately we already have a character that does that since we downloaded this annum starter pack so we can go ahead and just delete this guy and the one we want to use is inside here if you scroll down to the bottom there's this Yui for a SP character and this is the one we want to use for this tutorial because it already has support for a lot of the stuff we need so to make him the default character we want to go to edit project settings and then on the left go to maps and modes and under selected game mode we want to change the default pawn class to our ue4 ASB character so now if we go back and we run this you can see I start out as that character and I can walk around and it looks pretty good so let's go ahead and open up this character there's a few things we're gonna need to do in here right off the bat you can see if we try to compile it we're getting this morning and it's saying that there is no Crouch action defined so let's go ahead and define that real quick so we can Crouch so back in the project settings go over to input and under action mappings we want to add one for Crouch so click this plus button and we'll call it Crouch and I'm going to bind it to the C key feel free to find it to whatever key you want where is the types keyboard nope that's not gonna work see there's okay and while we're here we might as well create the bindings for the other things we're going to need in this game so the other actions we're going to need writing one for aiming down the sights so I'll call this a TS and I'm gonna bind this to the rice the right mouse button we need another one for firing or shooting so it'll hold fire and we'll bind it to the left mouse button and we need another one for switching guns switch gun guns and I'm just gonna use cue for this all right so that's should be all we need to do here oh wait we do need one more for interacting with objects like picking up guns so click this plus button again and make one more just call it interact and I'm gonna bind it to F so alright so now we can go back to our character yeah if we go pile this and you named it correctly it should just give you that green checkmark to say everything's good and if we run it now we press C you can see we are already touching alright so now we have our character transitioned over to use the new character and we have crouching working we can get started on the gun related stuff so the first thing we need to do for guns is be able to actually pick them up so to do that I'm just gonna create a interaction interface and I've done this on many my other tutorials so if you've watched them before this should be very familiar to you I'm basically gonna create an interface that lets you interact with items so right click and I'm just gonna put this in a folder so a new folder call it interaction interface and inside of here we will create and you're gonna pretty so right click go to blueprints and it's like blueprint interface and call it interaction interface and then go ahead and open this up and just edit it real quickly so we want to add some functions over here so on the right there's going to be this new function by default just go ahead and rename this one to on interactive and we want this to take in the character that did the interaction so like the player so add two new parameter and the type we want for our character since we're using the Yui for character is UV for ASP character if you're using a different player character in your game then you would set it to that and we'll just call this player and then we want another one for when you enter the range of an interactive object so like when you get close enough to pick it up for example so make another one and call it on interaction range entered and again this one is going to take in the player so click the plus button make sure your player type is selected and we'll call this player and then we need one more for when you exit the range so plus button again on interaction range exited and this one doesn't need any parameters at least not for our purposes all right so we can compile and save all of that and then just go ahead and close it alright so back in the content directory we have our interaction interface the next thing we're gonna want to do is add this interface to our gun and we don't actually have a gun yet so we're gonna go ahead and create one of those so to actually content directory right click and make a new folder and I'm gonna call this items and inside the items folder we're gonna make another subfolder for the guns course call it Jones and then next to the gun store we're gonna make a blueprint for the items so make a blueprint and it's up type actor and we'll call it B P underscore item go ahead and open this up real quick and for now all we really need to do is let's change the if you go do the class defaults at the top and you drop down this drop down we can't no that's not where it is class settings and drop down this drop down yeah there it is so we want us to select this generate abstract class checkbox and I will make it so you can't spawn an instance of the item in the world and that's because the items gonna be a base class so we don't actually want to be able to spawn it and the other thing we can do is we can disable tick because it never needs to tick so if you search for tick I think it might be in the class defaults yeah collects defaults and then search for tick we want to uncheck start with tick enabled because we don't need it to tick so it's just a little optimistic optimization that we can do all right so now back to the interface we want to implement the interface in this item class so that items can be interacted with so go back to class settings and under interfaces we're going to add the interaction interface and you can see it added over here and also adds our three functions over there on the left and it gives us the option to implement those functions so for the item base class we only want to implement these top two the on interaction right exited and on interaction right entered and we're gonna let the gun class that we're going to make later implement this function so right click on this top one and say implement function and do it for the middle one as well so then we have our entered and exit and I'm just going to move the interred on top because I think it makes a little bit more sense and then we go ahead and delete these other ones because we don't actually need them all right so we can start writing these and we're gonna have to do a little bit more before we can totally finish writing them but we can at least get get it under way so the first we're gonna want to do when an item is interacted with so a gun for example or when you get close enough to interact with it which is what this event is we want to show that little pop-up that says hey press F to interact or pickup or whatever so to do that we need to spawn a widget and attach it to the item so right click and say great widget and it wants to know who owns this widget and we can get that from the player here so we can say get controller and since it's a player we know that it's going to be a player controller so we can cast - player controller and again since we know it's gonna succeed we can right click and convert it to a peer cast and just hook it up like so and that's that up like so now the one thing we need to do before we can continue is actually make a class specify here for our interaction which is so let's go back to our content directory and go back to the interaction interface folder and we're just gonna create it inside of here so right click and under user interface select widget blueprint and call it interaction widget and go ahead and double click on this so that we can edit it so this is gonna be a pretty simple widget it's just gonna show like you know press f and then it's going to show what f will do so what we're getting the left we can start out by deleting this canvas panel we don't need it we're gonna make a size box and drag that in and we're just making a size box so that we can control the size of the widget that shows so go ahead and select widget with override and height override so we can override these values and for the width I'm gonna set it to 250 and for the height I'm gonna set that to 100 and just so we can see a better visual representation of these values let's go ahead and change this to custom and type those values in up here as well so 200 and 100 all right so the next one we want is a uniform grid panel to store everything in and we want a horizontal box and let's make the sorbonne two little box stretch in both directions and then let's add a text so it's gonna be on the left and it will add another one and that's gonna be on the right so for this left one we just want it to be the F key so the text will just say F and we'll put it centered and centered and this text we are basically gonna bind to this cuz it's gonna say different things depend on what the item is so for example for the guns it's gonna say pickup but if you had like a health kit or maybe a door you know you'd want to say something different you know what the door to say open you know what the health kit to say he'll maybe so we're gonna make this bindable so just for display purposes I'm gonna set this as interact and I gonna send it to you centered and centered and set it to fill and I think this one may be set to fill as well now let's set this one to auto and then on this one we will leave it as fill and then we'll just add some padding to the F so back in the padding just on the left side add like 15 or 20 or whatever you want all right so let's make the F a little bit bigger just so it stands out a little bit more so font will set this to like 40 that looks good we'll keep it bold and then this let's just change this to regular text so it doesn't stand out as much and the last thing we want is just a background so we'll add a border and add that to the uniform grid panel and again we want to stretch this stretch stretch and for the brush color let's just set it to like some transparent black color so zero zero zero and like 0.5 and there we go that is the basic of our widget and the last thing we need to do is just make it so it's interaction text is something that we can bind to so let's go back to our graphic real quick and over here under variables create a new variable and call it text and it is going to be a type text and we want to say that it's instant editable and expose on spawn so that way when we create an instance of this widget we can specify what we want this text right to be and we can also set it to blueprint read-only and private since it's not going to be changed at runtime so we can compile and save that and then if we go back to our designer tab and we click on our interaction text you see next to text over here on the right we have a bind option and we can select the text variable so this is essentially just going to bind that text to whatever the text of the text variable is I'm gonna click that and save and now if we go back to our BP item class and we select our interaction with you that we just created you can see since we said that it exposed on spawn you can see the text variable is exposed on spawn so now we all we need to do is pass what we want it to say so you might be thinking okay we're just gonna pass pickup here and we could do that but then every item no matter what it was even if it wasn't something you were going to pick up would just show pickup so a better way to do this is to make a function that our children like the gun class will be able to override and so that they can specify whatever they want for this text show so just really quickly on the left here I'm gonna create a new function called get interaction text and we're gonna set this to protective so that actually we can send it to private and we can send the private because we're the ones going to be calling this function and we can make it pure and constant as well and I'm just gonna add it to a private category all right so it's come over here we need to return something by default so the outputs will add an output for the text and we'll just call it text and by default we'll just return and interact and again this will never actually show unless the child of the class doesn't actually define this function so this is kind of like the default text that it will fall back to you if it doesn't know so we come back here to re-think graph and we can drag in this function and set up and again this is just so your items can show different text if they want to alright so now we have the widget being shown the next thing we want to do is you want to add the widget sort of on top of the item so that wherever the item is the widget will also show so to do that you need to add a widget component so we'll say add widget component and we can leave this as self and we can leave this unchecked as well but the relative transform let's just go ahead and split that so that way we can't compile it and we want to save this value so that we can later remove it so right-click and promote to variable and I'm just going to call this the interaction which is component and we can stuff this to private category and so it's private so now we have our widget component created we need to associate this widget with this widget component so if we charge up this and we say set widget it will allow us to do that so we just basically need to hook from here to here so let's go ahead and drag that across and I'm gonna make some reroute notes just by double clicking and kind of move it around here so it doesn't look so ugly all right that looks good so now we're pretty much done creating the widget but there are some properties on the widget that we want to set so that it's displays correctly so let's go ahead and take this and drag it down a little bit so we can create a route node and the things we want to set first and foremost is we want to set the widget space so say set which is space and we want to set this from world to screen and what screen space does basically means that it's going to stay visible even if you know the player walks in front of it it's like a screen space widget so it's not gonna do any depth testing so it's really good for things like you know text or icons in your world and the next thing we want to do is we want to set it so that it draws at the desired size so just set draw at desired size we want to set this to true and we're just doing that so that it drawls at that to 50 by 100 resolution that we set it to and it doesn't try to do anything else and we also want to set the collision of two disabled collision or set collision enabled and then you have to set it to you know collision and that's for obvious reasons we do obviously don't want to be able to collide with this widget in any way it's just purely a visual thing and then finally we want to attach the widget to the item so drag off this and say attach to component and up oh wait that wasn't the right one so attached to component I think it's the bottom one you want to make sure that it's going into the target and not the parent so attached to component and the parent we want to attach to is gonna depend on what the item is so just like we did for the interaction text we want to be able to get that from the item like the child item so we're gonna make another really simple function and it's just gonna be called also click the function button and call it get interaction text attached so this is where the text is going to attach to and we can drag this into the private and set it to your private so by default we're just gonna return our scene root because that's really the only option we have so drag that in and you just talk about like that and then drag it in and I'm gonna rename this to attach component and will override both these functions in our gun class so that will make a little bit more sense once we do that and we can side to pure and constant as well you compile and save that and then back in the bank graph we can drag in the get interaction attach and hook it up to there and finally we want to change all of these to snap to target so that it snaps directly to where we're telling it to attach to all right so that is our on interaction range entered function finished the next one we need to write is the on interaction range exited so this one's going to get called whenever the player walks away from an item so he's no longer close enough to pick it up and the first we're gonna do is drag in our interaction which a component and we really want to see if this exists and if it does exist we want to destroy it so that it doesn't show anymore so a really handy way to do this is you can right click here and say convert to validate kit and you can see it changes the note a little bit so we now have an is valid pin and it is not valid pin and it also gives us the variable itself so if it is valid we can drag off this and say destroy a component like so now one thing we can do we're technically like finished here but one thing we should probably do just as a safety precaution is when this one should gets called for on interaction range entered and we go and we create the widget before we do this we should probably make sure that the widget doesn't already exist just in the rare case and I don't know exactly how this would happen but just in case this gets called twice and widgets already created we don't want to create the widget again so just like we did on here I'm just gonna copy this and paste it up here and hook it up but instead of is valid we want to drag off of is not valid so we're just saying if the widget doesn't already exist then go ahead and create it and again that'll probably never actually be a problem but just in case it is it's just a really simple safety check okay so now that we have these functions finished we just need to call them from somewhere and the location we're going to call them from is the player because the player is the one who's going to be entering and exiting the interaction range of these items so we go back to our player and the collision we want to use to do the detection of if it's close enough to an interactional object or not is our capsule component this capsule around our player whenever that overlaps with an item we want to trigger these events so we're going to right-click on this and say add event and add component begin overlap and we're gonna also add one for on component in overlap just check my notes so I don't mess this up just give me one second okay so in the on component begin overlap the first thing we want to do is we want to see if the actor that we overlapped with implements the interaction interface so we can just turn off this and say does implement interface and the interface that we're checking for is the interaction interface and we can drag off this return value into a branch to check if it's true or false so if it's true we want to call we want to call this on interaction range in turn event so drag off this guy bring it down here a little bit and say on interaction range entered kind of like so just clean up a little bit and of course the player is our so so get rough - so like so so that's pretty simple that's all we need to do there basically want to do the exact same thing for the on exited so I'm just gonna go ahead and copy all this and paste it here shut up but instead of calling on interaction range entered we want to call on an arranged exited so on interaction rein exited and we no longer need this either alright so now we have our interaction rein entered and exited so at this point we can pretty much test it the only thing we really need to do now is create an instance of one of these items so that we have something to test with so back in the content directory and back in items we have our item class and we made its gun smolder so let's go ahead and actually create the base gun class so right-click and create a new blueprint class and select actually select all actors and because we want to make it of a Type B P item so search for B P item so this is gonna be the parent and it select and we're gonna name it B P gun and we can go ahead and double click on this dope so double click on it to open it up and inside of here we want to add a few components that all of our guns are gonna have and share so click Add component first we're gonna want as a skeletal mesh and the skeletal mesh is gonna be or they go in it so like the gun wallet so and the next thing we want is a sphere collision and this is gonna be used to detect collision on if the player is close enough to pick up the gun or not and I'm gonna go ahead and drag the sphere onto the skeletal mesh just so it becomes a child of it like so and the last thing we want to do is change I would already a set nevermind so I guess we don't have to do that and I'm gonna just make sure this is set to over that ball okay it is so the defaults are correct we don't have to change anything there yeah I think this is all we need to do for right now for this gun class we'll add more stuff to it later but we have the base gun class now so now we can actually create child's of this for our different types of guns so back in the content directory we're gonna make a new blueprint and it's going to be of type B P underscore gun and you can make as many of these as you want for as many different guns in your game that you have so let's select and the first gun is going to be our it's the m4 so say BPM for and if we double-click on this now and we select our skeletal mesh currently it's not set to anything we want to change it to the m4 so we'll search for SK underscore I guess it's called a r4 in here but whatever close enough so SK underscore a r4 and you want to select the one that has underscore X at the end and all that is is the saying that it points it along the x-axis you can see how it's aligned with this red X is here if you don't select that one it's gonna be aligned along the y-axis which is not what we want so make sure you select the ones with underscore X and we compile and save that and we go ahead and create the other ones while we're here so let's just copy and paste this and copy and paste it again all right so let's rename this one to BP underscore ak-47 I think that's one of the guns and pp underscore it's v al is the other gun I'm not sure so let's open up the ak-47 and open this guy up and for the skeletal mesh we want to change it to the a K so to find the a K and again make sure you select the one with the underscore X at the end like so you can see our viewport now it's showing me okay and finally we want to do it for this other one the VA oh I don't actually know what a VA l is maybe somebody in the comments below and could tell me but oh let's find it where you go here we go and again make sure you get the one with the underscore X and yep that looks correct IO and say this and one thing I'm just noticing here real quick is that this sphere collision is a little bit too small we want to make a little bit bigger so that you want to be like literally right on top of the gun to pick it up so let's go back to the gun base class and let's just change this sphere to have a radius of like 128 and recompile and save that save all and then we can add these guns to the world so let's go ahead and just drag one of these in or while all these in so let's drag a name for the a K and the VA o so hopefully if I did everything all right this is going to work let's go ahead and play it and if it over and we walk over the m4 you can see it says F to pick it up and I walk away it goes away and the other one should do the exact same thing because they're all sharing the same base class yeah that looks pretty good so far so I think in this point I'm gonna continue the tutorial and the next part of the video so I'll see you in part two
Info
Channel: Reids Channel
Views: 105,509
Rating: undefined out of 5
Keywords: Unreal Engine 4, UE4, Unreal Engine, Weapon System, Gun System, Shooting, Firing, Gun, Rifle, Aiming, Third Person, Bullet, Tutorial, How To
Id: GtylB1An820
Channel Id: undefined
Length: 31min 9sec (1869 seconds)
Published: Mon Mar 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.