Godot FPS Tutorial - Projectile Weapons

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in first-person shooters there are two common types of weapons there are hitscan weapons and there are projectile weapons in this video tutorial I'm gonna be showing you how to make projectile weapons because of course we want to make a mning harder than it needs to be in this tutorial we're going to be building off of what we learned in my hitscan weapons video so make sure you've watch that video first if we go into the script and take a look you'll see that we've got all of this code pre-written if you want to make sure that your code is exactly the same as mine here in this tutorial open up your web browser and go to github.com forward slash garbage whitey /video projectile weapons this will take you to my github repository for this tutorial and if you click on the projectile starter GD file it will show you the starter code we'll be using for this project feel free to copy and paste the entire code into your own project and then you can follow along with the tutorial the first thing we need to do is we need to create a bullet for our weapon to shoot when you fire a projectile weapon at a target it shoots a physical projectile towards that target the projectile travels through the air in real time meaning there's a small delay between pulling the trigger and hitting the target so we're going to create a brand new scene we're going to click on the plus icon and we're going to add a new note a rigidbody node I'm gonna name it a bullet next right click on bullet and add a child node and we're going to add a collision shape we're going to click on empty and we're going to give it a new capsule shape click on capsule shape and then the radius we're going to set it to 0.05 for the height we're going to set it to 0.1 next we're going to right-click on bullet again add a child node and we're going to add a mesh instance next to mesh we're going to click empty and we're going to give it a new capsule mesh we're going to click on the preview window here and we're gonna set its radius to 0.05 and it's mid height to 0.1 under the material tab I'm going to click on empty and we're gonna give it a new spatial material we're gonna click on the window and under albedo we're gonna click on the color here and we're going to give it a nice bright red color you can give it any color you want I'm just doing this for demonstration purposes so now we're gonna click on the bullet node and on the right in the properties we're going to set its gravity scale to zero I'll explain what this does in a little bit now we need to program some behavior for the bullet so we're going to click on this new script button and we're going to create a new script name it whatever you want in the script we're going to create a couple of variables we're going to write Const damage equals 50 and cons speed equals 10 we're also going to create a bar called shoes and we're going to set that to false in the ready function we're going to write set as top level and we're going to set it to true then we're going to write func physics process if you apply impulse transform basis z- transform basis dot Z so now we're going to right click on bullet and we're going to add a new node and we want to add an area node right click on area add a child node and we're going to give it a collision shape next to shape click empty and give it a new capsule shape will click on capsule shape and we'll set its radius to 0.06 and its height to 0.12 you'll see that this new area node is slightly bigger than the actual bullet itself next make sure that we have area selected and on the right under the node tab we're going to right click on body enter and we're going to select connect make sure that the main bullet node is selected and then we're going to press connect now you'll see that a new function has been added to our bullets code we're going to delete all this stuff here and we're going to write if body dot is in group enemy body Cal - equals damaged will also write Q free else Q free so right now none of this might make any sense to you and that's okay because as we go through the video I'll be explaining what everything here does but I will explain what this does right now so this bullet object is going to be flying through the air and if some kind of object like a wall or an enemy enters into the area note that we created remember it's this one right here that's slightly bigger than the bullet and if that object is in the group called enemy then we're going to decrease that object's health by our damage amount which is 50 we're also going to delete the bullet from the game if it hits any other object that isn't an enemy then it's just going to delete the bullet from the game without dealing any damage so right now our first person character has a hit scan weapon equipped and so if we click twice on one of these orange enemies here they disappear from the game and that's because we've damaged the enemies to the point where their health has breached zero and the enemy gets deleted we'll get into dealing damage with projectile weapon soon but first we need to actually make the projectile weapon work to do this we're going to go to our firstpersoncharacter and we're going to go into the script the first thing we need to do is add a new variable down here we're going to write on ready var bullet equals preload and from the menu that pops up we're going to select read scenes bullet or whatever you name your bullet so what this does is that as soon as the game loads up we're going to load our bullet into the game we haven't made it visible or anything yet we're just loading it into the game so that it's ready to show up when we wanted to show up next we're going to scroll down a little bit to our shooting code and we're going to delete everything under if aim cast thought is colliding then we're going to write far B equals bullet dot instance so what we're doing here is we're creating an instance of our bullet and we're calling it be when you instance an object what you're doing is you're kind of creating a copy of that object and so this way you can create multiple copies or instances of the bullet without having to create a separate bullet every single time you want to load one up into the game next we're going to write muzzle dot add child and we're going to add B then we're going to write B dot look at game cast get collision point vector 3 dot okay so let's do a little bit of recap if we look at our characters camera which is right here you'll see that there is a ray cast coming out of the camera that we've called it aimed cast so back in the script if we press the fire button and if our aim cast is colliding with something that we're going to create an instance of our bullet called B our character has an object called gun which is this object right here and at the very front there's a thing called muzzle which is right here this is where we want our bullets to show up so if you go back to the script you'll see muzzle dad child B so what we're doing is we're adding that bullet instance B we're making it a child of the muzzle so that it will appear in the same spot that the muzzle is then we're going to say okay B the bullet look at the point where the callate Cass collided with something and this will literally rotate the bullet so that it's facing towards the collision point and vector three dot up just tells the game which direction is up so that the bullet knows how to orient itself so that it can look at the collision point so now if you run the game and you press fire you'll see that nothing happens to fix this we're going to go into our bullet scene and we're going to change a few things on the right under collision you'll see layer and mask the reason why nothing appears to be happening when we click the fire button is because our bullet object which is the red object is inside of the area object that we created and if you go into the script you'll remember that if an object enters into the area then it's going to delete the bullet as soon as the bullet shows up in the game the game detects that there's an object inside of the area which is the bullet itself and so it deletes it immediately so you don't see anything happening to stop the bullet from triggering its own delete command we're going to click on bullet and on the right where we're layers are we're just going to click this highlighted box so that it Sun highlighted and in area we're also going to do the same for its layer I explained layers and masks a little bit more in a video specifically about layers and masks so feel free to watch that if you want to understand more about how it works but what we're doing here is basically we're making the bullet and the area invisible to each other so it doesn't detect that the bullet itself is inside its own area and so it doesn't delete itself so now when you run the game and you click fire you'll see that bullets appear but they're not moving anywhere and that's because we haven't actually activated the bullets yet to fix this we're going to go back to our firstpersoncharacter scene and we're going to go into the script and under this line we're going to write be cheap equals true if we go back to the bullet scene and we go into the script you'll see that we have a variable called shoot and it's set to false and in our physics process function we see that if shoot is true then we'll apply an impulse to the bullet and send it flying forward but because by default shoot is set to false and this doesn't happen because shoot is not true and so if we go back to our firstpersoncharacter you'll see that when we press the fire button if our aim cast is colliding then we're going to set for the bullet to true and so once that happens then the script for the bullet can activate and the bullet will start flying forward so let's talk a little bit more about this apply impulse line for rigid body objects the way you move them around is by adding or subtracting forces from it so think of apply impulse as if we're giving the bullet a single shove that pushes it and it moves it forward inside the parentheses you can give apply impulse two parameters the first one is where on the object you want to apply the force so in this case transform dot basis Z means that we're applying the impulse slightly behind the bullet and the second parameter is how much of a push you want to give the object in this case we're saying that the strength of the push that were giving the bullet is a value of one in the direction that the bullet is facing we're going to go ahead and multiply this by speed so that we can increase the magnitude of the push and at this point we're pretty much done with the bullet now you might be wondering what this set as top level is if we go back to our firstpersoncharacter and into the script you'll remember that we created an instance of the bullet as a child of muzzle meaning that whatever rotation or change in position muzzle takes and the bullet will also inherit that and so normally what this would result in is the bullet gets created in the world and if you were to move your gun around the bullet would also move around in weird ways in real life once you shoot a bullet you have no control over where it goes it just goes and so if we go back to our bullet object and into the script by writing set as top-level what we're doing is we're saying that the bullet is no longer a child of the muzzle so as soon as that bullet appears in the game it then becomes its own independent object and it just travels in a straight line and there's nothing more that we can do to influence its direction so now if you run the game when you press the fire button a bullet will fly straight out towards wherever you're aiming at and when the bullet hits an object it disappears now if you watched my video on hitscan weapons you'll note that these orange columns right here are enemies and in this case if you shoot them four times then they disappear so let's talk a little bit about these enemies so what we have here is our enemy the orange column in this case we named a bad guy if you click on bad guy and look under the node tab if you click on groups you'll notice that bad guy is in a group called enemy and if we go into the script you'll see that it has a variable called health and so down in the process function we write if bad guys health drops to zero or goes below zero then bad guy gets deleted from a game if we look back at our bullet down here you'll see that if body is in group enemy and remember bad guy is in a group called enemy then the body that is in that group in this case bad guy its health is going to be decreased by our damage amount which is 50 and as you recall that guy's total health was 200 so if we hit bad guy with 4 bullets then his health will drop to zero and then he'll get deleted from the game so at this point we're pretty much done you can shoot bullets you can kill enemies but there are a few other things that you can do for the bullet there is a couple of different ways that you can influence its speed in the main bullet node on the right in the properties you'll see a thing called mass if you were to increase the math to something like say 10 what this will do is increase the weight of the bullet which means that it takes more force to get it moving to at the same speed that it was if the mass was 1 so if you run the game now and you press the fire button the bullet moves a lot slower because the bullet is heavier and it took a lot more force to get it moving another way to influence the bullet speed is to go into its script and just change the speed to something else maybe make it 5 and now when you shoot the gun the bullet moves even slower another thing that you can do if you want to make your shooting more realistic you can apply some gravity to the bullet so that in addition to flying straight forward it'll also move downward a bit because gravity is pulling it down to do that go to this property called gravity scale and remember that earlier on we set it to 0 and what that does is we're basically eliminating gravity from the equations so it's not affected by gravity but if you were to set this to something higher say 1 when you fire the gun you'll notice that the bullet doesn't hit the crosshair instead it hits the ground because gravity is pulling it down in your game you're probably not going to want your gravity to be this strong but this is just for demonstration purposes just so you can see what's happening and one final thing right now if you shoot your gun you'll notice that it can only fire if you're actually pointing at an object if you point the gun into the sky and try to shoot nothing happens and that's because our aim casts raycast which is coming straight out of the camera isn't colliding with anything and so you're not able to fire but this isn't very realistic because in real life if you shoot a gun in the air the bullet will still fire so let's make that happen I've created a new scene and we're going to click on this plus button and we're going to add a static body node right click on static body and add a child node and we're going to add a collision shape we're going to click on empty and we're going to give it a new box shape we're going to click on box shape and in the extents for the x-axis I'm going to give it some big value like 300 and in the z-axis I'll do the same 300 so now we've got this really large panel which we can't even see completely so now what we're going to do is I'm going to duplicate the collision shape five times so that we have six collision shapes and then I've gone ahead and adjusted its rotation and its translation so that it creates this big box like so you can't even see all of it this is going to be our Sky box which is basically just a big invisible box that completely encapsulates our entire world and because this Sky box is a physical object that you can aim at our character will always be aiming at an object which means that all the right conditions will be met to create a bullet in the game and send it flying towards the location that you're aiming at so in our main world scene we're going to click on the main node we're going to click on this chain-link here we're going to search for skybox click it and we're going to click open and so now we have a big skybox that encapsulates the entire world now when you run the game you can not only shoot at objects in the world in enemies but you can also shoot into the sky congratulations you know how to make a projectile weapon if you have any questions about anything that I just showed you in this video feel free to comment down below I read all of my comments and I try my best to answer each and every one of your questions also don't forget to join the discord server and if you liked the video like it subscribe it share it Belem and comment it thank you have a nice day
Info
Channel: Garbaj
Views: 22,139
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, overwatch, pharah
Id: IDsoEAj5xG0
Channel Id: undefined
Length: 14min 48sec (888 seconds)
Published: Wed Apr 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.