Hitscan Guns, Weapon Switching and Crosshairs - 3D Godot 4 FPS Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the channel today we'll be doing a tutorial on implementing a headscan weapon for your Godot FPS we'll be covering crosshairs and hit markers shooting animations bullet Trails hit particles and weapon switching we'll be starting off with some parts of the game already complete so if you're curious about how anything else works you can check out my other tutorials but you don't need to have seen them to understand this one so let's get right into it for a headscan weapon we're going to be using this model from sketchfab made by two way right quick new inherited scene and save after that we can click on the root node and select clear inheritance which will allow us to edit the children nodes and clean the hierarchy up now the other gun in our game doesn't really have realistic textures so we're going to downgrade this one with our own materials when that's done we can drag the gun into our player scene and turn off the visibility on the other guns it's a little small so scale it up and then click on the camera and toggle preview mode scale it and reposition it so it looks like we want end game after that we can go back to to the gun scene and add an animation player to create the shoot animation we're going to set the animation length to 0.07 seconds and the Snapping to 0.01 here we will add a position and a rotation property track for the node that contains our meshes just like with the other gun we will create a position keyframe to bring the gun back and want to bring it more forward than before the shot to make the animation look organic and finally one to bring it to the original position then we're going to do the same thing for rotation bring up the gun to 2 degrees on the x-axis then down to negative one kind of over compensating for The Recoil and then back to zero the final thing we're gonna do for this animation is ADD muzzle flash we're going to need a mesh that we're going to pair into our meshes node so it follows the gone recoil animation make it a point mesh and add a new Surface material in our obedo section we're going to use a texture I got from kany assets then set the transparency mode to Alpha and in the mesh settings set the orientation to face X if you can't see the mesh from the left side make sure to toggle flip faces then we're going to turn on emission and use the same texture we used for our Albedo raise the energy multiplier to make it a little brighter and scale up the mesh at scale and position property tracks from the mesh to the animation player for the scale keyframes we're going to start out at 0 then go to 3 when the gun is kicked back scaling the mesh messes up its alignment with the muzzle brake so we're also going to need to go through the animation and make sure that it's positioned properly click on the muzzle flash node in the tree and reposition it in the editor then click on this little key icon next to the position property on the right side to save that as the keyframe go through the entire animation and make sure that the flash is always on the barrel now we're ready to start on the code right now we have this chunky block which handles our projectile pistols which you don't need to understand we're just going to move it over to its own function to make implementing a weapon system a bit easier we're going to make a separate function for shooting our machine gun in a second but first we need to make sure to create an unready variable for the gun animation player after you drag the gun into the code Type forward slash animation player so down at the bottom of our code we're going to create a new shoot Auto function and the animation part is going to be similar to the projectile gun we're going to put this function after a check whether the player pressed the shoot button and then here we're going to check whether our shoot animation is playing if it isn't it means we finished our animation and can shoot again so here's the result of that code and the gun animation now we just need to make sure that we're actually hitting stuff first things first let's add a Crosshair by going into our 2D View and adding a texture rectangle node to the UI inside of the game scene I got this Crosshair texture from Kenny assets and let's drag it into our texture property let's check the size of the texture rectangle because we're going to need it to position the Crosshair and code and the game script create an already variable for the Crosshair and in the ready function let's set the x position of the Crosshair to the size of the screen divided by 2 minus half of the size of the Crosshair then copy paste this bit and repeat the process to position the Crosshair on the y-axis that's our Crosshair in game and we're also going to add a hit marker across here duplicate the first one add the texture to it and here's a demonstration on how it's going to work that's beautiful let's go through the same process and add an unready variable for it as well as Center it on the screen just like the other one leave it says ability turned off for now after that let's get working on the heads we're going to add a raycast as a child of our camera and if we set its direction on the negative z-axis it's going to be aimed right in the middle of our Crosshair we're also going to move the raycast slightly outside of our player because I think it's kind of weird if we're heading stuff that's behind our gun rename the raycast and switch the Collision mask from one to two since that's the Collision wire where our enemy body parts are also don't forget to turn on collisions with areas on their raycast for coding the bullet heads you already know the drill we need an unready variable then in the shoot Auto function and when we're starting the shoot animation we're going to check if our aim raycast is colliding with anything if it is we can check if our array is colliding with an enemy and if that condition is true we're going to call the head function on it that applies damage and kills them when their health reaches zero now that we can shoot the zombies let's get the hit marker working we're going to do this by passing a signal from the zombie to the game scene that can then toggle the visibility on the hit marker so let's submit the signal when the zombie takes damage back in our main scene let's create the function that will connect the signal to pass for now and then the code block where we spawner zombies let's connect their zombie head signal to this function since we're only referencing a function and not actually calling it the brackets are unnecessary here now once we get this signal we can turn on the visibility on our hit marker then create a short timer for 0.05 seconds and then turn off the visibility on the head marker with that done we now get a little bit of feedback but we can definitely take it further let's work on some some boa traces let's create a new scene for this and change the root note type to a mesh we're going to call it bullet Trail make sure to save it and then attach a script to it and the script we're going to generate a mesh at runtime using this initialize function we're going to pass the position of the gun barrel and the position of where the bullet head to it first of all we're going to initialize a new immediate mesh which is a kind of mesh you can draw with code then assign it to be our mesh resource let's also add a material override so we can change the color of the bullet Trails later so the way that we draw this mesh is first we need to call surface begin on the immediate mesh which allows us to Define what type of mesh we want to draw and pass it the material we want to use which is our material override then we Define all of the positions of the vertices by calling surface add vertex which is just our two positions on the line after the vertices are defined we need to call surface n to let godon know that the mesh is ready to be drawn let's also add a timer to make sure that the trails are despawned after after a couple of seconds turn on one shot and auto start and connect the timeout signal to the script Q3 when the signal goes off now in our gun scene let's add a node 3D to track the position of the barrel we're going to pass this position and the position of the raycast Collision point to the bullet Trace but if the raycast isn't colliding with anything we still need to aim the trails somewhere so we'll add a node 3D at the end of the aim raycast by setting its position to the raycast's position plus its direction Vector now to add the bullet traces we're going to load the scene in our character script and when we instantiate them we're going to store them in the instance variable that we already have here then you know it I know it we need on ready variables for the end of the aim Ray as well as for the gunbarrow node 3D just like with the gun animation player we can drag the gun and then type forward slash Barrel to specify the full path now in our shoot Auto function when the animation starts playing we're going to instantiate a new bullet Trail scene then if our aim Ray is colliding we're going to call the initialize function on the trail with the parameters of the barrel Global position and the Amory Collision point if the aim Ray is not colliding though there isn't going to be a collision point so we're instead going to call the initialize function with the same Barrel parameter but now instead with the position of the end of the aim Ray after the bullet Trail is initialized we can get the parent of the player and add the bullet Trail to it now that the basics are working we can make it look better by changing the material and making it fade away we'll start off by setting the Albedo and the emission of the material and then making sure that the transparency mode of the material is set to Alpha after that we can create a variable for the alpha that will be lowering every tick of the physics process then type material override.obedocoller.a which corresponds to the alpha parameter of the material and set it to our Alpha variable now the problem we're going to run into if we leave it at this is that all the boa traces share the same material so setting the alpha on one will set the alpha on all of them so in the ready function we're going to duplicate the material override and set the material override to the duplicated material which ensures that they're unique on each instance and finally to complete the visuals on the shooting let's add some particles we're going to attach them to the bullet Trail for simplicity's sake and we're going to start with the blood splatter and draw passes we're going to create a new sphere mesh for them and change the radius to 0.04 and high to 0.08 and the material override will create a new material for them and set the Albedo color to a nice red also enable emission with a strength of 0.4 and set the color to a similar red just to make the splatter stand out a little bit to make the material more reflective we're going to change the roughness value to 0.1 then we're going to create a new process material for the particles and in our time settings we are going to change the explosiveness to one set the Z direction to negative one and don't forget to set the x direction to zero like I did here then we're going to set the the minimum and maximum velocities to 6 and 8 respectively and the scale settings create a scale curve going down in size to make sure that the bot particles grow tiny before disappearing when we're done make sure to turn on one shot on the particles next we'll create a similar particle system with brown particles for when we had anything other than the zombies if you're duplicating the first particle system make sure to remember to make their process material and material override unique because otherwise you'll be editing both of them so now in the bullet Trail script we'll create a trigger particles function that we will call from the player script if the bullet hit anything depending on whether we had an enemy or not we will either trigger the blood particles or the terrain particles we'll assign the position of the particles to the Collision point and will make them face or gone before we set the emitting property to true and then if we hit anything other than an enemy we will repeat the same process for the terrain particles then we'll go into our player script and before we call this function our bullet Trace needs to be a child of the map already so we'll move the ADD child call up a bit then if the Amery is colliding with an enemy we'll call the trigger particles function by passing the Collision point from the ray the gun Barrel's Global position and true for enemy Collision then we'll copy this bit and add a mouse block to if we collided with an enemy and paste it in there that's our particles in action and I'd say that the shooting looks kind of decent now we're ready to work on weapon switching to create a quick weapon manager we're going to have a current weapon variable that will set to one of the weapon constants from a weapons enum enums are basically a quick way to create a bunch of constants with integer values and ascending order so when resetting weapon to weapons.auto we're just setting it to zero but it helps with code readability later on now what we're going to do in our shooting code block is we'll match our weapon variable to one of the weapons and the enum and depending on which one we're holding right now we're gonna call the shoot function after that we'll create a new animation pair attached to the character that will handle weapon switching animations we're going to create one lower gun animation for each of the weapons using a position property track for these animations we'll set our animation length to 0.3 and zoom in here we're just going to insert two keys one for the starting position and one for the gun going down below the camera's field of view as how it looks and now let's code it get the weapon switch animation player in an unready variable and the first thing we're going to make is a function which checks which weapon we are currently holding and lowers it here we're going to use a match statement just like in the shooting logic which checks our weapon variable against all of the possible weapons and the weapons enum when it matches our current weapon to one of them it plays the lower weapon animation for that specific gun in this case we only have two weapons and two animations but for an extensive weapon roster this code block would get a little more bulky the next step is raising the weapon we are switching to and before we write this function we are going to create a variable which tracks whether we are currently switching a weapon and shouldn't be able to shoot so now back and raise weapon will first set this variable to false and then call lower weapon we know that's going to take 0.3 seconds so we'll set a timer for that long after that we'll match the weapon that we're switching to and play the same lowering animation but in Reverse which saves us making an extra animation for each weapon for now but if you have a raise weapon animation you'd plug it here after that we'll set our current weapon to the weapon that we're switching to and finally Setter can shoot variable back to true now we're ready to get input from the player so let's go into the input map and add keys for weapon switching once that's complete we're going to go back to the players physics process and check if the player just pressed the weapon switch Button as well as if the weapon we're trying to switch to is not our current weapon if both are true we can call raise weapon and pass it the weapon we want to switch to then copy paste this bit and repeat the process for all of your weapons with that our weapon switching is working and the last thing I want to do for this video is make sure that the projectile guns that we made in the last video work properly with the Crosshair at the moment the bullets shoot in the direction where the guns are facing but if we want them to fly directly at the Crosshair we have to switch up our coats slightly before we do that though please remember to like the video because it helps a lot so in our bullet scene from the previous video we're getting our velocity from the direction that we're facing but instead we're going to set the velocity when the bullet is spawned we're going to create a set velocity function to which we will pass the Target that we're aiming at we're going to rotate the bullet to face the Target and then set the velocity by multiplying the direction to the Target by the bullet speed then we will move the Bullet by adding velocity times Delta to the position then in the shoot pistols function we will get rid of the line that sets the rotation of the bullet and instead check if the aim Ray is colliding if it is we'll call the set velocity function and pass it the Collision point of the aim Ray if our aim Ray is not colliding we still need a Target to aim for so we'll instead pass the position of the end of the aim Ray copy paste this bit for a second pistol and finally I want to change the color of the head marker change its modulate color to red and make sure to drag it above the Crosshair and the tree and that's it consider supporting the channel on patreon so I can make more tutorials and let me know what you want me to focus on next for this series and the comments the link to the GitHub project is in the description and if you want to check out any of the other videos about this game you can click on them here I'll see you next time
Info
Channel: LegionGames
Views: 24,882
Rating: undefined out of 5
Keywords: Godot, Godot 4, First-Person, Godot tutorial, Godot 3d Tutorial, Legion Games, adventure game, first person game, game development, godot engine, survival game, horror game, Godot 3d, zombie game, collision shape, environment, gamedev, enemy ai, 3d enemies, FPS collision shape, animation tree, animated enemy, 3d animations, fps mechanics, body part collisions, projectile bullets, pistol, rifle, Godot 4.1, hit scan, bullet trail, weapons manager, gun switching
Id: tryYXX30FGg
Channel Id: undefined
Length: 15min 28sec (928 seconds)
Published: Tue Jul 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.