Weapon Camera & Aim Down Sights - Make an FPS in Godot Part 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
having the ability to walk around your 3d scene interact with staff and fire a weapon is fantastic yeah but we still don't have a gun so in today's video I'm gonna be teaching you how to add a gun into your first-person controller with aim down sights and the gun isn't gonna clip with any geometry guaranteed so if you're ready to get started with today's video make sure you click on that like hit subscribe if you haven't done so already and let's get started okay so we are in Godot and I've got my FPS tutorial project open so if you haven't followed any of these so far the link is in the description below go on head over and check those out all the source code is available on my github so if you don't want to follow along with all of those you can download the latest version of the source code and get started straight away the first thing we're gonna do to add a gun to our player character is we actually need a gun model so I'm gonna import something that I've created in asset forge now if you haven't got asset forge then I would highly recommend you go over and purchase it it's by Kenny the guy that does all of the prototype graphic stuff he's released this awesome piece of kit which is a basically a 3d modelling package that has a bunch of prefab stuff inside it and you can just drag and drop it it's all modular little building blocks and they have a specific section here for blasters and I've just put this piece together you can see it's just made of these modular parts and I've just whacked them all together and you can see we've made it quite a nice looking sort of sci-fi rifle here and what we're gonna do is we're going to come up to file and export model and we're gonna change the settings here to gltf and we're going to change the scale to 20 now this isn't a tutorial on how to use asset forge so please feel free to import your own models here this is just why I'm going to use for this tutorial we're gonna export this into our fps tutorial assets folder and I'm just gonna call it gun for now and let's save that but minimize asset forge and you can see here that immediately inside our assets folder we now have a gun GLB file and we have the three materials that make up the gun if we drag that into our scene and just zoom in on it there you can see that it's come in nicely and it's all textured fantastic let's add this to our player so let's delete it from this scene here and we're going to come on over to our players seam so you can see here we've got our capsule our lovely capsule and our camera here the first thing we are going to do is we're going to add in a basically a placeholder for all of our weapons to go so I'm not going to go in this tutorial into equipping multiple weapons but we're just gonna set the scene basically for allowing us to do that in the future and basically we want our weapons to be a child of our camera because the weapons are going to be displayed where our camera is where the players head is so it just makes sense to make it as a child of the camera so let's go add a child node to this camera let's make it a speacial let's create that and let's just rename it to weapons okay and inside our weapons here let's drag in our gun model all right so now we have our gun model you can't actually see it because it's inside the players capsule but if we just drag it forward slightly and zoom in a little bit you can see we now have the gun sitting nicely in front of the player there but if we come over to our camera and click on the preview you'll see that it sort of looks a bit odd that doesn't really look how we want it to so with this preview selected let's come back down to the gun and let's go over to the transform here let's start messing around with some of these settings so let's move it down a little bit on the y-axis let's bring it back towards the player here let's say maybe we don't really want the the stock of the gun in view so let's bring it this far back and let's move it across on the x-axis I think that looks okay so let's leave it at that for now and if we save the game there and just press play let's just see what it looks like so it looks like that in the game I think that looks alright but there's a lot more that we can do to make this gun look really really good in our game so let's close this and one thing I want to demonstrate to you before we get any further into doing the aim-down-sights is the fact that this weapon will clip with geometry in its current state so let's come back to the world and go into our 3d scene and all I'm gonna do is I'm gonna right click and add a mesh instance here just to demonstrate something let's come over to let's make it a let's make it a cube and let's drag it forward here so it's right in front of the player we're going to do it's going to play this scene and now you'll see that we haven't added any collision to this so I can walk through it but what you will see is if we walk through it the gun sort of disappears so it clips with the geometry now given the shape of our first person player capsule that's not really going to be an issue if we're walking into something like a wall however if you make your capsule slightly smaller in width then you might actually end up in a situation where the gun particularly if it's a big gun like a sniper rifle might clip with the geometry so I just we've got our enemy following us there and gave me a bit of a fright okay so um yeah so that's basically what I wanted to show you so we're gonna fix that and the way we are gonna fix it is basically use two cameras okay we're gonna have one camera that renders everything in the world except for our weapons and then we're gonna have another camera that renders only our weapons and overlays that image on top and this is a really really useful technique for layer in different effects and in this instance we're going to use it to layer our weapon on top of our world view and to do that we're gonna use something called a viewport so let's comment into our player scene here and what we're gonna do is you're gonna add in a viewport container so let's add a child to our player and let's search for viewports woops you viewport container there we go let's press create and you'll see here that it's taking us into the 2d view and the reason it's done this is because a viewport container is effectively a UI control it's a rectangle that can have a texture applied to it however it's a special kind of texturing they're only renders a viewport to this texture so let's click on the layout option here and let's click on full rect because we want the the viewport to fill the whole screen and then underneath this viewport container we're actually gonna give it a viewport so let's type in viewport and you can see here if we click on viewport you can see at the bottom it says creates a sub view into the screen it doesn't really make much sense but basically what it means is it's going to create a second screen effectively so let's hover over the little warning triangle here and you'll see it says viewport size must be greater than zero to render anything so you can see currently it's set to zero zero we want the viewport to be the exact same size as our game window so let's come up into our project settings here I just go to general and if we find where we have our window so display window you can see that our current window size is set to one or two four by 600 now you can change that if you want you can set it to 1080p I'm not gonna bother changing that in this instance but we do want our viewport to be the same width and height as this so let's come over here and it 1 over 2 4 by 600 okay and there's a few other settings we need to change on this so basically with a view with a viewport you can have it render a completely different world if you like but we're not going to do that in this instance however we are going to set transparent background to on and for handle input locally we don't want to be hand in handling any input in this viewport it's literally just going to be a view for our weapons so we can uncheck that option there okay so let's just hover over this again and you can see that it still says 0 but that's fine because we've already modified that so the next thing we're gonna do is we're gonna add a camera to this v4 okay so the viewport won't render anything unless you've got a camera attached to it so let's attach this camera here and this is where we're gonna start filtering out the things that we want to render on our two cameras ok so we have two cameras now we have one for the whole player that renders everything in the world and we also have this viewport camera which is effectively just going to render our weapons and in order to filter things if we come up to our camera here and look over on the right you can see we have this coal mask and if we click these double dots you can see that we can select and deselect which layers this camera should render so let's just come back into our project settings here and scroll all the way down till we see 3d render layer names and we're gonna change the first layer to be world and the second one to be guns ok let's close that and then I'm gonna hunt it so again you'll you'll scene in one of my previous tutorials if the layer names don't change you can basically save your scene here and open it up again and now if you come back you'll see that they have taken the names on effectively so what gonna do is we're gonna uncheck guns okay we don't want the gun to be visible within this camera itself and if we come down to our camera inside the viewport we actually want to uncheck everything except for the guns layer which is this second one so that's antic everything here we only want to see the guns on this camera so let's save that and you'll notice now that you know we haven't changed anything here so that's not really gonna work the gun is still going to be visible because it's still effectively on the first layer that world layer so one thing I'm gonna do is I'm going to come to the left hand side here I'm gonna double click on my gun and I'm gonna press new inherited okay that's gonna take our 3d model and create a brand new scene for us one which we can modify the nodes on the left hand side and do all sorts of things with so let's save this scene here to player let's create a folder let's call it weapons and for now let's just say that it's gun I'm not gonna bother with name in it and we're actually going to come down to the mesh instance here inside this scene and on the layers underneath visual instance you can see that it's it's basically assigned to that world layer let's untick that and change it to the guns layer let's save that and what we're gonna do is we're just gonna drag in that weapon so it's dragged that in there and let's just take all of these values across so all I'm doing basically here is just copying across the x y&z values for our translation from our original gun just so that it's in the exact same position and then I'm gonna delete that original gun because that was just the model itself and it wasn't assigned to the correct layer let's just rename this to gun and you can see now that we're actually previewing this camera and the guns completely disappeared so that's fine because this camera no longer is rendering that gun section there and this camera here if we talk preview on this camera and you can see that it's also not rendering the gun that's because it's not in the same position as this camera here so we're going to fix that by adding a script okay and I'm gonna call this the gun camera script so what we're gonna do here is we're gonna get a reference to our to our main camera so let's say export bar let's just call it camera and it's gonna be a new path and what we're gonna do is in our ready function let's create let's just create a another variable here let's rename this one sorry - camera path and let's create a variable here export for camera this is gonna be a node that's it's actually just gonna be a camera and inside our ready function we're gonna say camera equals get node camera path okay let's delete these comments we don't need those however in our process function so it's uncomment this one in our process function what we're going to do is we're going to say global transform equals and we're gonna set it to the camera global transform camera at Global transform so basically what we're saying here is get a reference to our main camera which is up here and then set our global transform for this camera to be the exact same as this one so whatever way that cameras pointing whatever position it's in we're gonna mimic that entirely every single frame and now the only thing we have left to do there is come to our camera and go up to a sign and choose our main camera okay let's give that a whirl and you can see now that we have our gun it appears just like it did before however if we walk up to this cube now you can see that the gun never Clips through that piece of geometry and it won't clip through walls either so that gun is on a completely separate layer now to the rest of the world and that's fantastic that means if you've got a really long weapon like a sniper rifle or a rocket launcher you can be able to walk up to geometry walk up to walls and it's not gonna look we where the end of the barrel just suddenly disappears so that's fantastic so that's one thing sorted the next thing we're gonna do is aim down sights okay so for aim down sights we're going to make a few modifications to our weapon script that we've created in previous tutorials and you can see here in our player we have our weapon object and it's just a node and it has a weapon script attached to it actually has a shotgun script attached to it in this instance which inherits from the weapon I'm actually going to delete that weapon entirely okay we don't really need it anymore what we're gonna do instead is we're gonna come down to our weapon script and let's just drag this weapon script up on to our gun okay so every single gun inside this is going to have its own version of the weapon script with different fire rates clip sizes however we're gonna modify that weapon script now to allow us to do aim down sights or ATS what we need for our ADR system is we need a vector3 position for the gun when it's not being looked down sights and we need a vector3 position for when the gun is looking down the sights and basically depending on whether or not the player is aiming or pressing the aim button so in this case that will be right click we're going to interpolate between those two values okay so let's come up to our project settings quickly and go to our input map and let's just create a new action and I'm just gonna call this a BS let's assign the right mouse button to that and close that input and now let's come into our script let's add some new variables here so we're going to have an export var default position this is going to be a vector 3 and we're gonna have an export var ad s position this is also going to be a vector 3 I'm also going to have an export for ABS acceleration and this is going to be a float and we're just going to default that something like Northpoint 3 okay and two other things we're going to do or sorry one other thing we're going to do is we're all so going to interpolate the field of view value of our main camera so that when you go into ad s the whole world sort of zooms in a little bit to give you a little bit more focus on what you're actually aiming at so we're also going to do an export var FOV oh sorry default fov that's gonna be a float and we're gonna default it too and what our main camera is which is 70 which is just the default fov for all cameras and going up and we're also going to export a variable for adsf for V it's gonna be a float and in this instance we're gonna say I don't know four default value let's try something like 55 ok let's save that and there are a few things that we're gonna want to do now okay so first of all I can see straight away that our raycast node here that's no longer the correct path because we've moved our weapon script it's actually sitting on the gun itself now so what you could do is you could modify this and you could start adding dots however I've started to prefer the option where I basically export in a path variable which is gonna be a node path so let's get rid of that and then just having a completely separate variable for the actual object itself and we say that's going to be a ray cast and then in our ready function what we can do here is we can say break ASCII calls get node great cast pack okay it's I just find it a little bit easier than using the whole dollar path syntax and it's quite nice to be able to basically come into our gun here and change the values that way I just find it I don't know I just find it a little bit simpler so we actually need to change that to an export instead of an unready and basically now what we can do is we can click on this and you can see we have this ray cast path here we can just assign that and click on our ray cast and that's going to fix that issue for us okay so the other thing we need to do is we also need a reference to our camera so I say camera path is also going to be a node path to clothe of aerial for this far camera equals it's going to be a camera and down in our ready function we're going to get a reference to that so camera equals get node camera path okay and we're gonna assign that as well it's just coming here assign assign that to our main camera before we continue let's just press play just make sure we haven't broken anything make sure we can still click and the enemy dies okay it's that all works fine so we know we've got broken array cast which is fantastic okay and now what we're going to do is we're going to assign these two variables here our default position and our ad s position so it's coming up coming to our gun here and let's go to the 3d view and what I'm gonna do is we already know that this position here this 0 2 8 8 etc that is the position we just preview our camera and actually let's just click on this again this Col master so we can see it we already know that the position of the gun there is what we want our default position to be so let's copy those values into our default position vector in the script okay now what we're going to do is we're going to move our gun into the position that we want it to be in when we're looking down the sights so let's come into our translation here let's just try for starters let's just try putting it slap bang in the center on the x-axis let's try moving it in a little bit there on the z-axis and let's try moving it up so it kind of aligns with the horizon there which would be the center of the screen and you can see now that our sort of iron sights that I put on the gun are lining up perfectly with where the center of the screen is going to be so I think that is a pretty good position now you'll notice a that the weapon is sort of clipping with the camera so you sort of get in this weird weird clipping issue here that's absolutely fine what we are going to do is we're going to come down to our viewport camera which is the gun camera and we're absolutely going to change the near clipping near clipping value here from zero zero five we're just going to drag it all the way down and that will fix that issue for us when the gun is actually rendered on that gun camera so what we're gonna do now is we're going to take the position that we have here so it's going to be zero on the x-axis we're gonna paste it into the 80s position let's take the Y and take the Z okay and then what we're going to do is we're just going to revert the position back to our default position it doesn't really matter because we're going to be assigning these values via code but it just sort of looks nice when we're previewing the next thing I'm going to do is going to come over to our main camera and just make sure that we on take that guns layer okay let's save that and now we're going to come into our script and what we're going to do here is inside our process function just after we've done everything else so after our reload as well we're gonna say if input dot is action pressed we're gonna say 80s so if the right button is held down or whatever button you've assigned to the 80s what we're going to do is we're going to take our current position so we're going to say our so we're gonna say our transform origin so the position of this current weapon we're gonna say it's equal to the transform to origin linear interpolate now you can go into the search help function here and type in linear interpolate and you can see that on the vector three there is a linear interpolate function that wants a target vector and a T value which represents the amount of interpolation so let's come back to our weapon script here so let's pass in that target value which will be our ABS position and the interpolation value we're going to pass in is our ABS acceleration okay the next thing we're going to do is we're also going to say our camera fo V equals and here we're going to do some linear interpolation as well but we can use simply the lip function because it's just going from a float to a float so we're going to say loop from our current cameras fov to the ABS fov and again we want to flip that over the exact same acceleration rate so let's use like ABS acceleration again okay so if we're not holding down the 80s then basically we want to do this whole thing again except we want to do it and put everything back to its default position okay so default position there and we want our default fov there okay let's save that and there is one last thing here we've got an error it says the identifier transform isn't declared in the current scope and the reason for that is that our weapon script initially inherited from a node and in this instance now we're using a spatial to let's say spatial and that error now should disappear okay so let's press play and let's see what happens here so we have our gun in its default position and if we hold down the right button to a min you can see that we get a nice smooth animation into our ABS scoped version there and if we fire at the enemy you can see that it all works nicely and if I just come back into this cube here you can see that we aren't clip in wherever we are we can be smack bang in the center of the cube we'd be on the edge of the cube and this weapon will never clip with any geometry in your scene providing that geometry is not also on the guns layer itself so that is everything for today's tutorial guys if you liked this video make sure to hit that like button if you haven't already subscribed please consider hit and subscribe button and clicking on that notification bell I release new content every single Friday you can find all of the source code for today's tutorial over on my github the link is in the description below and if you haven't already done so head on over to the discord become a part of the community we'd love to have you over there if you'd like to support this channel I also have a patreon I'll leave the link for that also in the description and without being said see you guys in the next video [Music]
Info
Channel: Code with Tom
Views: 15,803
Rating: undefined out of 5
Keywords: godot, fps in godot, fps godot, fps godot tutorial, fps tutorial godot, fps tutorial, make a first person shooter in godot, make an fps in godot, make a fps in godot, how to make an fps in godot, how to make an fps game in godot, godot fps weapons, godot raycast, godot fps raycast, godot fps tutorial, godot fps, godot field of view, godot weapon camera, godot aim down sights, godot ads, godot weapon scope
Id: Myr9-6A0wqE
Channel Id: undefined
Length: 23min 49sec (1429 seconds)
Published: Fri Jul 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.