Complete 3D Shooting Mechanics - 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 shooting Mechanics for an FPS game and Godot we're going to cover gun animations bullet movement complex Collision shapes with body parts and weak points for our enemies as well as dying animations and enemy despawning this video is most useful when you already have a character controller a map and some enemies all of which I cover in my other tutorials but you don't need to have seen them to understand this one so let's Dive Right In the first step is finding a nice gun model that's already in a Godot supported format or converting it with blender get it into the project files and right click new inherited scene save this scene and drag it into our player character the gun should be a child of our camera and the next step is to make sure that it's positioned correctly if we select our camera and click the preview ticker we get to see what our camera sees this way we don't have to run the game every time when we're happy with how this looks let's turn off the shadow on the gun go into our gun scene and click on the map and the geometry section there's a setting for cast Shadow which will turn off you may like how your gun model looks or it could already be textured but I feel like we should change some of the materials on this one to make it fit the vibe a little better and the surface material override will create new materials for the different parts of the gun and Beyond just changing the color we will also play around with the metallic specular and roughness values metallic is how much of its own color light a material reflects specular as how glossy it is and roughness is how rough the surface is and how much light it absorbs for example a metal would be very metallic not very specular and not very rough as opposed to Wood which would be rough but not metallic or specular so now that we're satisfied with how our gun model looks let's add a shooting animation to it we're going to need an animation player node and a new animation called Shoot click on ADD track and Select Property track click on the mesh and search for the position property on the right hand side set the animation length to a tenth of a second and then set the snapping to a hundred for the animation we can add three keyframes one for starting in the original position one for the gun going back and one for coming back to their original position we only need to change the position on the middle keyframe which gives us the whole animation now that we have it it's time to get input from the player go into the project settings input map and create a new shoot action then bind it to the left Mouse button and the player script create a new on ready variable for the gun animation player then in the physics process we're going to check if the pair pressed the left Mouse button if the shoot button is pressed we want to check if the shoot animation is not playing if it's not we're going to play the shoot animation because we either just press the button or the animation already finished and we should shoot again this looks pretty funny without any actual bullets so let's add those go back to our gun scene and add a raycast 3D set its direction to negative Z1 since that's the forward Direction in good Dell coordinates drag it towards the barrel and we have our bullet spawn we're doing this to make sure that our gun model is facing in the right direction because it might have been made with the different coordinate system in mind if it was make sure to rotate the gun towards where the raycast is facing next create a new scene for the bullet with a node 3D as the root save it and add a mesh instance as a child of the bullet we're going to make this a box mesh and change the Y and X dimensions of the box to 0.05 then in the surface material override go ahead and create a new material for the mesh we're going to add a new Albedo color as well as an emission color for the boys set the emission strength to 5 and that's how our bullets will work now at array cast for collisions and just like with the barrel set its direction to the negative z-axis we'll set its length so it's slightly outside the mesh let's add a script to the bullet and the first thing we'll need is a speed constant set this to 40 and will also create on ready variables for the mesh and the ray cast since we'll need them later anyway and the process function we will move our Bullet by multiplying our rotation bases or where the bullet is facing by our velocity or a vector 3 with the length speed facing in the negative z-axis which is again the forward Direction and Godot multiply that by Delta and the movement is done in the player scrubbed let's start spawning the bullets first we'll need the barrel raycast from the gun so let's make an unready variable for that we'll also need to load the bullet scene and a new variable and also create a variable to hold individual bullet instances before we add them to the map and the physics process after we play the shoot animation we will assign the instance variable to the bullet instantiate function which creates a new object out of the bullet scene that we loaded then we're going to set the position of this new Bullet to the global position of the gun barrel raycast and set its transform bases or rotation to the global rotation of the Gun Barrel or using Global positions and transforms for the barrel because we want to take into account where the play where is positioned as opposed to just where the gun is positioned within the player after that get the parent of the player which is the map and add the new bullet as a child of the map now the bullets are flying but not colliding with anything so let's get working on that we're going to add a new GPU particles node to the bullet scene then turn off the visibility on the raycast and the mesh so we can see the particles better in pass one we will add a new box mash for the particles and change its size so it's 0.04 units on all axes and then the material override we will create a new material similar to our Bullets by setting an Albedo car and a mission color and setting its emission strength to 8 then we will add a new process material to the particles and in the direction section we will set the direction to positive Z which is opposite the direction of our bullet Movement we will set our velocity minimum to 3 and maximum to five and then in the time section we will turn up our particle explosiveness to one we can turn the visibility on the bullet mesh and rake as back on and then position the particle node so it's at the end of the raycast once we're done turn on one shot on the particles back in the bullet script process function we're going to check if the raycast is colliding with anything if it is we're going to turn off the visibility on the mesh and turn on the particle emission after we make an unready variable for the particles real quick so that the particles emitting property to true and then we want to wait until the particles are done emitting so we're going to create a timer for a second and then after that timer is done we can delete this bullet we also want to get rid of any bullets that don't hit anything so they don't keep flying infinitely so we'll create a new timer node with a 10 second wait time and its auto start property on we'll connect its timeout signal to the bullet and when it goes off we'll delete the bullet as well so now our bullets are properly colliding with things like they're supposed to but beyond the fact that the zombies are in dying the bullet collisions with them are weird this is because right now we're using simple capsule shapes for the zombies but ideally we want to have separate Collision shapes that follow the zombie animations in my previous video where we build these zombies from the ground up we used godot's physical skeleton feature to create Collision shapes that do that but there are multiple issues with that first is that they broke for no apparent reason and the second is that they don't follow the animations if they are played from the animation tree which is okay but it means we'll have to do it ourselves first things first let's get rid of all the physical bones that we have at the moment then click on the skeleton which any rigged an animated model will come with and add a bone attachment note to it the bone attachment has a bone name property which determines which bone in the skeleton this node will follow basically like making it a child of that bone we'll start with the head and we'll add an area 3D as a child of the bone attachment and a collision shape as a child of the area we'll go for a sphere here and we'll set its size and position so it's as close to the head as possible then we can duplicate the head bone attachment and figure out which bone we can attack match that torso to after quaking on the skeleton and looking through the bones it looks like the spine is the best candidate for this so go back to the Bone attachment and select the spine bone from the wrist repeat the same process of approximating body parts with simple shapes until we have the whole model covered one thing to look out for is editing a duplicated Collision shape will edit their original as well unless we click on it and select make unique after you go through this process our whole model will be covered with cohesion shapes dot follow the animations but most importantly it will also work with an animation tree the next thing we want to do is attach the same script to all the areas within the bone attachments since they will be colliding with our bullet and the script will have an export variable for how much damage the body part takes so we can double the damage for headshots we'll also have a signal for when the body part is hit which will connect to our zombie script this signal will be emitted when we call the head function when our bullet figures out that it had an enemy once that's done we will connect this signal from every area to D to the zombie script in the zombie we will add a health variable and back in the function that's called from the head signal we will subtract the damage that the body part passes from our health when our health reaches zero we will just delete the zombie for now before we forget click on the head area to D and raise the damage it takes to two before we write the Collision code and the bullet make sure to set the group of all the body part areas to enemy then in the bullet script when our bullet raycast is colliding with something we're going to check if the collider is in group anime and if it is we're going to call the head function on it this is that hit function and the body part that emits the signal for the zombie that then subtracts Health on our raycast we want to make sure that cohesion with areas is turned on and that after the ray collides with something we disable it now that we run the game the zombies take extra damage from headshots and disappear when their health reaches zero but you'll notice that sometimes they take a lot of hits to kill this is because the bullets are colliding with the zombie character body collision shape instead of the body parts so what we're going to do here is separate these Collision shapes on different layers so that the bullet only scans for body parts and obstacles but not the navigation Collision shapes select all of the body part areas and switch their Collision layer and mask to two do the same thing for the bullet raycast and for our obstacles and terrain we want to turn on both wears one and two now we're just missing the dying animation for the zombie we downloaded this one from miximo but I want to change it a little so that the zombie disappears into the ground after it falls we're going to add a position property track just like with the gun and then change the length of the animation to 4 seconds we're going to add a position keyframe at the end of the original animation and one at four seconds by the end of our new animation the zombie is going to be 0.5 units below ground the plan here is that once we can't see it we can just despawn the zombie so we don't quarter the memory so let's add this animation to our animation tree and add immediate transitions to it from getup run and attack animations then add Crossfade and advanced conditions for these transitions if you're curious about how we set up this animation tree and the zombie AI or any other part of this game not covered in this video check out my other tutorials on the channel but for now all you need to know is that the zombie won't enter into the dying animation unless we set the dying condition to true and code which is what we're going to do once our health reaches zero after that we know that the die animation will be playing for 4 seconds so we'll create an await timer for 4 seconds and after itself we're going to despawn the zombie well I'd say it turned out pretty well but do you know what's better than one gun you leaving a like on the video because you liked it just kidding two guns that's pretty neat but do you know what's better than two guns supporting the channel on patreon so I can make more tutorials just kidding four guns but do you know what's better than four guns I can it's eight guns anyways thank you for watching I hope you enjoyed the video the link to the GitHub or the Godot project for this game is in the description and if you want to check out how any of the other parts of this game were made you can click on them here alrighty I'll see you next time
Info
Channel: LegionGames
Views: 61,595
Rating: undefined out of 5
Keywords: Godot, Godot 4, First-Person, Godot tutorial, Godot 3d Tutorial, Legion Games, adventure game, first person game, Godot FPS, FPS, game development, godot engine, survival game, horror game, Godot 3d, zombie game, terrain, collision shape, map design, environment, gamedev, world building, enemy ai, 3d enemies, FPS collision shape, animation tree, animated enemy, 3d animations, fps mechanics, body part collisions, enemy weakpoints, projectile bullets, pistol, rifle
Id: 6bbPHsB9TtI
Channel Id: undefined
Length: 12min 14sec (734 seconds)
Published: Thu Jun 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.