Weapon Switching In Godot - FPS Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you're playing FPS games being able to switch between weapons is a pretty basic mechanic on the discord a lot of people have been asking me to do a video on weapon switching and so that's exactly what I'm going to do in this video for this video we're gonna be starting off with a basic FPS controller like the kind that we built in this video but if you don't feel like watching the video then open up your web browser and go to www.hsn troller this is going to take you to one of my github repositories where I have a first-person character controller pre-made for you that you can import into your game and use to follow along with this tutorial so once you've got a character controller ready to go and I should stress that you don't have to use the character controller that I provide in my github the basic concepts that I'm going to teach in this video should apply to any kind of first-person character controller but anyways once we've got that done the first thing that we want to do is create some new inputs for this tutorial we're going to be showing you how to switch weapons either by pressing the number keys on the top of your keyboard or by scrolling your scroll wheel to do that go to project and project settings and in the general tab scroll down on the left here until you see physics click on common and under physics fps make sure you have it set to 240 next go to input map and in the action box right weapon 1 and click add weapon to weapon 3 then for each of the weapons click on the plus icon on the right select key and press the key that you want to assign to the weapon in this case I'm going to use the number keys so one for weapon 1 2 for weapon 2 and 3 for weapon 3 next we're going to create some guns for our character to hold on to so that we can demonstrate switching between them press this plus button to create a new scene pressing this plus button and we're going to add a mesh instance node I'm going to rename it to gun 1 under mesh we're going to click on the empty box give it a new cube mesh and then I'm going to mess with the size and shape of the cube until I get something that looks kind of like a gun now that we have our gun and I know that it looks basic but that really doesn't matter we're going to add some very basic functionality to the gun just for testing purposes click on gun 1 and add an script and name it whatever you want now that we're in the script we're going to write function closed parentheses if input that is action just press fire oh and before I forget let's go back to project and project settings and in the input math tab let's create a new action called fire I'm going to click on the plus button we're going to select mouse button and we're going to select Left button from the pulldown menu once we're done with that we'll go back to the script and we'll write print gun one fired so in this shoot function that we just created if we press the fire button then it's going to print gun one fired in the console all right so I've gone ahead and created two more guns that are essentially exactly the same as this gun right here the only difference is being that they are different colors and sizes and if you go into the scripts for each one you'll see that instead of gun one you have gun too and your gun three for each respective gun next we're going to go back to our character controller scene and under head we're going to right click in we're going to add a child node and we're going to add a spatial node I'm going to go ahead and rename it to hand click on hand and move it forward a little bit and move it down and to the right so it's in a position that kind of resembles where a hand should be and the reason why we're calling a hand is because this is the location where we're going to be holding all of our guns then we're going to click on hand we're going to click on the chain link button we're going to click on scene gun one here and we're going to click open and so now we have gun one as a child of our hand then we're going to click on hand again click on the chain link again this time we're gonna select gun two and add it and then we'll click on hand a third time and we're going to add gun three so this looks a little weird right now but don't worry it's going to make sense soon I promise also if you want you can click on the camera you can click on the preview window you can click on hand and under the transform tab you can adjust the translation a bit and adjust the position of the gun until it's in a position that you like this is a completely optional step you don't have to do this it's just for visual appeal so now let's get to the part where we actually make the gun switch go back to your character controller scene click on the script and now we're going to add a few there variables under this line we're going to write on ready var gun 1 equals dollar sign head slash hand slash gun 1 then we're going to copy this line and we're going to paste it two more times for this one we're going to change gun one two gun to m2 and for this one we'll change it to gun three so now we have a reference in our code to each of the three guns that we created so the first thing that we want to do is we want to make it so that we can switch our guns by pressing the number keys on the top of our keyboard to do that under this chunk of code right here we're going to create a new function func weapon select open close parenthesis next we're going to go back up to our variables and we're going to create a new variable var current weapon equals 1 and so this is going to keep track of which weapon we currently have active so back here in weapon select we're going to write if input is action just press weapon 1 current weapon equals 1 L if input that is action just press weapon 2 current weapon equals 2 LF input that is action just press weapon 3 current weapon equals 3 so if we press the weapon 1 button in this case it's the number 1 on my keyboard and the current weapon will be set to 1 if instead you press the weapon 2 button which is number 2 then the current weapon will be set to 2 and if you press weapon 3 then it'll be set to weapon 3 okay so now that we've got all of this written down you might be wondering ok so how are we going to turn this into switching weapons well if you go back to any of our gun scenes and you click on the mesh instance node on the right in the properties you'll see a tab called visibility if you open it you'll see a little option called visible and currently it's set to on however if you uncheck this button then it'll make the gun invisible this is going to be very useful if we go back to our playercontroller scene you'll remember that we added all three of our guns kind of stacked on top of each other right here and it looks really weird but once we're done with all of our coding you're not going to be seeing all three guns at the same time and that's because we're going to make it so that whenever you select a certain weapon by pressing one of the number keys one two three we're going to make it so that when you select a gun the other two guns that are stacked on top of it are going to become invisible so you can't see them and you only see the gun that you selected and then if you decide to select a different weapon then that current weapon that you have will be made invisible it will make the gun that you want to see visible so in a sense you're not really switching the weapons because you're always carrying all three weapons but you'll only be able to see one gun at a time to do that we're going to go back into the script and under this chunk of code right here we're going to write if current weapon double equals one gun one visible equals true else gun one visible equals false so let's go through the logic here for a second so if current weapon equals one and remember the current weapon is this variable that we defined up here if the current weapon is one then gun one will be made visible if current weapon is any other number other than one else gun one will be made invisible so now all we have to do is copy this chunk of code and paste it two more times then we'll just change this number to two we'll change this to gun to change this to gun two and we'll do the same for this one as well only this time we change it to 3 so if current weapon is two then gun two will be visible otherwise it will be invisible and the same thing applies for gun three so now in order to make all of this functional we're going to have to tell the game to actually run this function that we just created and so in the physics process function we're going to write weapon select open closed parenthesis so what's happening is that on every single frame weapon select this one that we just created here is running so now if you run the game you'll see that we're currently holding the red gun this is the weapon that you see when you press one on the keyboard however if you press number two then the gun switches to the green gun and the red gun disappears and if you press three then you get the yellow gun and the green gun disappears you can switch between all three guns just by pressing a different so now let's make it so that you can also switch the weapon by either scrolling up or scrolling down with your Mouse's scroll wheel and in order to do that we need to go back to the script and add a little more code now you might be tempted to go into project and project settings and go back to the input map and put in an input like a scroll wheel up or scroll wheel down but that doesn't actually work and if you think about it it makes a little bit of sense when you scroll up or down with your scroll wheel you're not actually pressing the scroll wheel you're just turning it and the input map in Godot only really deals with buttons that you can press and so we need to come up with a way to make Godot recognize turning the scroll wheel as an input without using the input map to do this we're going to go back to the script and under the input function that we have up here we're going to add some new code we're going to write if event is input event mouse button if event dot is pressed so if a mouse button is interacted with and if that mouse button is pressed which again doesn't really make sense because you're not pressing the scroll wheel but that's just the way it works in Godot if event dot button index double equals button wheel so if we scroll the wheel up if current weapon is less than three current weapon plus equals one so if our current weapon variable if that number is less than three then every time we tick the scroll wheel up by one unit we're going to increase current weapon by one but once we get up to number three we don't want it to go to a number four when we scroll the wheel up again and so we'll write else current weapon equals one so if current weapon is less than three scrolling the wheel up will increase current weapon by one but if it is three then scrolling up will set current weapon back to one that way it'll constantly go one two three and then go back to one instead of going to four all right so now we've got code that takes care of what happens when you scroll the wheel up what we also need to do the same for when you scroll the wheel down and that's pretty easy to do all you have to do is highlight this code right here copy it and paste it and then we just need to make a few small changes we'll change this if to Elif will change button wheel up to button wheel down and then we need to change this logic here so instead of if current weapon is less than 3 we'll say that if current weapon is greater than 1 so if current weapon is 3 or 2 then current weapon will be decreased by 1 and if current weapon is 1 then scrolling the wheel down will change current weapon to 3 and so basically this is just the opposite of this and so if you have more than 3 weapons like if you have 5 weapons or 8 weapons it doesn't really matter all you have to do is change these numbers here to whatever is appropriate for your game so now when you run the game scrolling the wheel up will cause you to switch weapons and scrolling the wheel down will cause you to switch weapons as well you might be thinking well ok it appears like we're switching weapons but I know that we're actually holding all three guns at the same time so what happens if we try to shoot the guns do all three guns go off at once and so the last thing we're going to do is we're going to address this very issue if you go back to your gun scenes and you go into the scripts you'll remember that we created a function call shoot where if we press the fire button then it will print in the console gun whatever fired and it's slightly different for each individual gun so that you know which gun that you just fired we want to make it so that for example if you have gun 1 selected then it will only print gun 1 fire and if you have gun two selected then it will only print gun 2 fired etc to do that we're going to go back to our character controller scene we're going to go into the script and we're going to go back down to our weapon select function and under this line of code right here we're going to write gun 1 shoot then under this line right here will write gun to shoot and under this line gun 3 shoot so if the current weapon that we've selected is 1 then gun 1 will be visible and we'll be running the shoot function and if the current weapon is not one else gun 1 will be invisible and it will no longer run this function and then we've just done the same for gun 2 and gun 3 so now when you run the game when we have the first gun selected and we pressure the console will print gun one fired if we select the second gun and we press the fire button and it will say gun two fired and if we select the third weapon and we shoot it will say gun three fired and this is useful because if you switch guns obviously they're going to have different properties they're going to deal different types of damage and so this is just a very simple way of switching between those different properties and of course there are a million different ways that you can do weapon switching in fact not every single weapon switching system is appropriate for every single game for example the system that I just showed you is really best for games that have a fairly limited number of guns in a specific set order but if your game has a lot of guns or if you want to have the ability to change which guns are in which order then you might want to use a different system hopefully one day I'll get around to making videos about these different kinds of systems but in this video I just wanted to show you one very simple and reliable way of switching weapons anyways thank you all for watching I hope you enjoyed the video if you have any questions about anything that I just showed you feel free to ask in the comments below or in our discord server and as always don't forget to like it subscribe it share it Bella and commenting thank you have a nice day [Music]
Info
Channel: Garbaj
Views: 32,495
Rating: undefined out of 5
Keywords: godot, godot tutorial, godot3d, godot 3.1, godot 3.2, garbaj, how to make video games, how to program, how to code games, game dev, game development, game dev tutorial, unity, unreal engine, ue4, valorant, first person shooter, fps, third person shooter
Id: AOlyEbANY90
Channel Id: undefined
Length: 14min 40sec (880 seconds)
Published: Wed May 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.