Godot 4.X : Ultimate First Person Controller Tutorial ( 2023 )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's lucky welcome to the first part of this three-part ultimate FPS controller tutorial series we're going to start off by setting up a basic FPS controller with crouching jumping sprinting all the basic stuff in the next couple of tutorials will go more and more in depth on building this amazing controller so without further Ado let's get into it alright so let's get into the basic FPS controller I'm going to supply this assets folder and here we have some basic textures and hdri to set up the scene the textures are the grids from Kenny I'll leave a link to his website and the textures below you can also just download the folder from the GitHub of this tutorial there's also an hdri of hdri Haven it's just a pretty sky so something to look at instead of the default uh godosh guy so once you have your assets folder it's time to start a pado I'm using version 4.1.1 you can use any version you want there's nothing specific for 4 or 4.1 but this is just the latest release right now so I'm going to create a new project I'm gonna drop it in my Godot projects folder I'm gonna name it [Applause] the first person controller I'm going to click create folder to create its own folder and click create and edit let that load up and the first thing we're going to do is drop in our assets folder so you can just drag and drop your assets folder into the res and now that everything is imported let's set up some basic inputs for this you can go to project project settings on the top left going to input map and start naming your inputs so for a first person controller we're going to need a forward button backward button from moving backwards left button right button and in this tutorial we're also going to set up sprinting so we're going to do a sprint button and crouching so we're going to do a Crouch button next we'll set up the key codes for these commands so for forward we're going to use w for backwards we're going to use a or sorry s for left we're going to use a for right we're going to use d for sprinting we're going to use shift and for crouching we're going to use control so now this is all set up you can close this out next up I'd like to do some file organization so in my file browser I'm going to create two new folders the first one is going to be Scripts [Applause] and the second one is going to be scenes so of course in the scripts folder we're going to put our scripts and in the scenes folder we're going to put all our scenes and speaking about scenes let's set up our main scene so on the top left we'll click new 3D scene and we'll call this scene or we call this node by double clicking the node world in this world we're going to have a couple of things so let's also separate those out by right clicking the world nodes ADD child node and creating another node 3D you can just search for node and click node 3D this one is going to be our stage the second thing we're going to add to the world is a container node for the environment and the lighting so I'm just going to add another node 3D because it's nth oh and and here I'm going to drop my sunlight and my environments which will take care of the Skybox and post-processing effects to easily add these to your scene you can click the three dots up top here and click add send to scene and click add environment to scene and you could just leave them right here in the root of the scene but I'd like to put them in their own little notes so everything stays clean and speaking about world environment let's add that hdri that we put in the assets folder so you can click on the world environment on the top left and then on the top right click on the environment and here you have a tab named sky drop this down and click Sky again oh not the drop down just this one in here you can see Sky material right now it's using a procedural Sky material that comes with Godot we're going to use our own hdri so you can click on this one and click the little refresh icon to clear it out you can see the sky disappeared click empty and click new panoramic Sky material this one will say empty you can just click on it and click quick load to find our own hdri you can see it's the first one on the top dot hdri click open this loading always takes a little while sometimes it's even crashes let's see right here it's doing a little weird stuff but finally it works now we got this beautiful sky in our scene but of course we also need a floor so this is what we're going to add next go into your stage note right click it add shout note and search for books we're going to use this CSG box 3D click create and we're using this CSG box here instead of just a normal mesh because it has easy collisions so easy in fact you just have to take this little box here on the right use collisions and now the player and other objects can collide with this box now right now this box is one meter by one meter which is a bit small for a floor so let's change the size here on the top right we can set it I'm going to set the x to 20. the Y can stay 1 because this is the height and is that also 20. now we have a nice big floor to work with then I'm going to scroll down in the CSG box to transform and I'm gonna move it down a little bit so it's on the world's origin you can see right here the lines of the world origin are going to the middle of the Box we would like the box to be a little bit lower so we can still see the world's origin I'm gonna do this by setting the Y to minus 0.5 again it transform in the St CSG box and now you can see it's nice and aligns with the world origin now let's put in that beautiful grid texture that we got from Kenny you can do this by scrolling up in the Box again and here in material we're going to create a new material for this floor so click on empty new standard material click the material to open it up and there's a lot of properties in here but we're only going to care about albado and uv1 in albado there's a tab name texture and in here we're going to set our grid texture so click on empty quick load just like we did with the hdri find a dark texture dark texture 7. open it up now you can see we have this giant grid which we don't want we want it to be small and represent one meter by one meter this is easily done I'm scrolling down to uv1 the tab we opened up earlier and enabling triplaner now you can see beautiful one by one grid on this 20 by 20 floor and that's the stage all set up now let's get into the player but before we do that we're gonna save this scene so hit Ctrl s on your keyboard go into your scenes folder and we're gonna call this Main now let's add our player so we'll close out the stage node in the world and create a new chart notes for the world and this one is going to be a character body because our player is going to be a character body 3D just search for character and double click check the body 3D now you can see the character body is giving us a little warning sign and it's because a character body always needs a collision shape this is the shape used to collide with the floors and the walls and everything around it so we're gonna add this right click the character body in the world node click ADD child node and search for a collision shape we're going to use Collision shape 3D create then right here on the top right we're going to set a shape for this Collision shape 3D so click on empty and we're going to use a capsule now you can see the player is stuck halfway in the floor this is not what we want so you can hold down control on your keyboard grab the little green arrow this is for the y-axis and drag it up by one meter this will align the character perfectly with the floor so now we got a basic player shape let's add a little camera to him we can just add a camera straight into the Collision shape we're going to need a pivot this is so we can move the head independently of the player body so for this we're going to go back into character body 3D right click it add shot node and search for node and add a node 3D this is going to be our head so in a transform we can set the height of our head your player can be any height you want but I'm going to make mine 1.8 meters and then finally in this node we're gonna add a child nose Again by right clicking the note adding a child nodes and searching for camera 3D and that's what we're going to view from that's our lead now let's rename these notes so everything makes a little more sense the character body 3D it can be player Karen and the notes by double clicking them the no treaty that's going to be our head is going to be named head the rest will leave us is for now now in order to control the player move it around and look around we're gonna need to add a script to it this is easily done by clicking the player nodes make sure you're selecting the character body 3D and not one of its child's nodes and clicking the script button attaching a new script for the path right now you can see it once it wants to save it into scenes but we want it into the scripts folder so click the little folder icon next to the next to the path click the little up arrow and go into the scripts folder and we're going to name it player you can also see it selected a basic template for this scripts because we're attaching it to a character body 3D we're actually going to use this template so you can just leave it as is and click create and you can see cuddle already generated some code for us let's see what this code does in the beginning of the code you can see it sets a speed variable and a jump velocity variable next it gets the gravity from the project settings after this it goes into the physics process function this is a function that is run for every physics stick first off it's checking if the player is not on the floor then it's going to add gravity to it so if you're in the air it's going to push the player down to the ground next up it's going to check if we're pressing a jump button and if it's on the floor and if so we're gonna jump by adding our jump velocity to our y velocity and lastly it's going to take an input Direction and this is going to take it from right now you I left through it right UI up and you lie down which are the arrow keys we're going to swap this out in a minute and then it's getting a Direction this is basically applying the arrow keys to the current facing factor of the player so when you press W you move forward and not forward in World space if you don't understand what that means I'll explain in a second and lastly it's checking if we're pushing a button and if we are we're going to move in that direction and we're doing this with the move and slide function so actually this would already work right now so if you just press Ctrl s and we run this project and we select the current scene which is going to be the main scene you can see we're already looking through the camera and if we press the arrow keys the player is already moving of course there's some things we need to improve we want to look with the mouse I want to be able to Sprint and Crouch and some other stuff so let's approve upon this script the first thing I like to do here is remove the naming of speed and jump velocity I don't like these all caps variables so I'm just going to call this speed copy it and paste it everywhere it's used and the same for jump velocity right there next up you can see the current speed variable in this controller is a const which means it's never going to change but in our case we want to Sprint or want to crouch and in those cases the speed will change so we're going to change it to a variable and we're actually going to rename it a little bit again because it's not going to be the constant speed of the player but it's only going to be the current speed oh so we'll prefix it with currents current underscore speed then we'll copy this again and place it everywhere where speed is used and we're going to give the controller a couple of more speeds to use so we're going to create a new variable or this one can actually be a const I'm going to say walking speed is going to be equal to 5.0 you can play with your speeds how you like I hope we're going to make a sprinting speed I'm gonna set this one to eight and last we're going to create a crouching speed and this one will be 3 in my case if you're going to be playing with these variables a lot tweaking them a nice trick is to add in the front add export add symbol plus export and then set it to a variable and this way when you're looking in the 3D scene you select your player you have the variable right here in the inspector and you can tweak it more easily without editing the code for me I'm just going to keep this constant [Applause] so I'm going to change it back to const so next up let's set these speeds when we want them to be set so in the physics process we're going to check which buttons we are pushing and changing the speed accordingly so first we'll check for this printing button you can do this by typing if input with a capital i input for the capital i that is action pressed this means it's the button being held down it's going to Sprint and then we're going to set our current speed equal to the sprinting speed and if not so else we're going to set our current speed equal to the walking speed and with this very basic logic we can change our speed of our player so let's run the scene again and now we walk around still with the arrow keys we press shift you can see we go a lot faster all of a sudden and we'll let go we're walking again now let's change to those keys we said earlier in the beginning instead of just the arrow keys this is very easily done by changing UI left to left your rights too right UI up to forward and your way down to backwards let's run the scene again and now we can move with wasd Sprint we shift all right looking good so far let's get our Mouse looking around for this we're going to need a new variable the mouse sensitivity I'm going to create this as it comes as well and you're going to want a quite low number for this because defaults uh sensitivity is quite High I'm just going to call it Mouse underscore sense I'm going to set it equal to 0.4 now when we run the project you can see our Mouse is free to move outside of the window we don't want this we want the mouse to be locked inside the window and hidden so we can move the camera around without moving our cursor out of the bounds this is done in the ready function the ready function in a Godot script is run once at the beginning of execution so this is perfect for capturing our Mouse and making sure it doesn't escape the screen so type func underscore ready and in here we're gonna change the input mode so again we're gonna grab our input variable with capital i dot set Mouse mode and in here we're going to use one of these suggested variables and we're going to set captured input dot mouse mode captured all capitals and now when you run the scene you will see we can move our Mouse and it's hidden that's perfect now let's make the player look around I'm quitting by the way by using alt tab because you can no longer use your mouse so I'm just all tapping out I'm pressing the stop button right here so let's look around for this we're going to need a couple of things we're going to be able to rotate the head so we're going to need this head Godot has this awesome feature where you can drag notes from your project into your code and you'll instantly get a reference so I'm just gonna click add drag it in holding down control to create a variable and letting go and now you can see we have a head variable which is referencing the head of the player now let's rotate this head around when we move our Mouse left to right we're going to do this in the input function let's create it funk underscore input you can see it auto corrected me with an event variable we're going to need this variable now the input function captures every input so Mouse buttons keyboard inputs everything but we're just going to need the mouse inputs here so let's filter that out so we're going to say if event is inputs Mouse motion input event Mouse motion capitalized then we'll know that it's our Mouse Moving that we're currently getting in the input function so now that we know that our input event is a mouse motion let's rotate our player by the amounts the mouse moved we're going to rotate around on the y-axis by the amount of mouse moves from right to left which is the x-axis sounds a little confusing I'll explain so we're gonna rotate y which is the way we're going to rotate the player but we're going to rotate it by event dot relative .x event relative X is the amount the mouse moved on the x-axis and then we're going to multiply this by our mouse sensitivity and now let's run this see what it looks like you can see it is working we're rotating around by moving our Mouse from left to light right when it's rotating a lot the mouse sensitivity is way too high this is actually not a problem with the sensitivity this is because we're rotating in radians which uh are a lot smaller than degrees what we usually lose so we're just going to quickly convert to degrees by saying degrees to radians and in here we're going to put the whole event relative x times mouse sensitivity and now we run it should be a lot slower yeah it feels a lot better only it's inverted so we'll just quickly add a minus before event relative X to invert it all right awesome we can look around Sprint around we can even already jump because it was in a default template now let's do up and down and this is why we got the little head and we put our camera in the head because if we would just rotate the whole player uh up and down the whole physics would glitch out and we would rotate the whole Collision shape it's not a great idea we're just going to rotate the head you can actually see this if you select the head and rotate the x-axis that's what we want so we're going to do almost exactly the same and instead we're just going to type head go to rotate and we're going to do this on the x-axis like I just showed we can just copy this whole line and just change out X to y and we'll see if this is inverted or not let's check it out it's perfect so now we can look up and down and around Mouse Institute is a little high for me so I'm going to turn it down a little bit 2.5 yeah that's better so now we can look around jump around and already Sprint around but the first problem you'll notice with this is that we can look all the way down and keep going and we can invert our head this is not what we want you can also do it upwards so we're going to use a function called clamp for this which does exactly as it says in a tin it clamps a certain value so we're going to set our head a little rotation dot x equal to clamp head notes rotation X so the clamp function takes three variables first the variable is gonna clamp and then it's minimum and its maximum our minimum is going to be minus 98 Degrees and our maximum is going to be 98 Degrees and like I said their degrees and not radians so we're going to have to convert them again by using the degrees to Radiance function so just copy this put it in front of the minus 98 and in front of the positive 98. and now we're running again you can see we can no longer look all the way up or all the way down so this is already a very basic first person controller there are a couple of things I want to optimize with this the first one being inputs are very Snappy meaning if I go press left I'm instantly going left and if I press forward I'm instantly going forward to a little bit of a better feel to a first person controller you want these things to be gradular when you press left you want a little bit of speeding up and a little bit of slowing down when you stop pushing any inputs this is a personal preference and if your game is more like this then you can of course keep it like this I'm gonna add some snappiness to it and I'm gonna declare this in a variable I'm going to name this variable lerp speed I'm going to set it to 10 it's just what we used before I'll explain later but we're calling it LARP speed because what we're going to use is a lerp function which does what I explained earlier it gradually changes the value instead of just instantly being one or zero so that's the function we're going to use to change the speed of the player and some other things and that's why I'm calling it lerp and we're going to use this variable to lure between our input and our actual Direction for this we're going to need to make Direction global because the input there is just the literal button presses into a vector and in the direction is this translated into the world's space in order to lurp this direction we're going to need to reference itself which we can do right now because it's being declared right here so we're just gonna take out far and Direction Ctrl C move all the way to the top Ctrl V and set it equal to an empty Vector tree right now so Factor 3.0 and empty vector3 and here we don't need the far anymore because it's already declared and now we can learn this so blurp Direction the variable we just created and we're going to set it to the input multiplied by the world space or the forward Vector of the player and then the speed that we want to learn to set is going to be Delta which is a Time indication you can see right here this is so the logic of the lip is not tied to the frame rates but rather tied to actual time and then we're going to multiply this by the lurp speed and when we run this again you can see the player takes a little while to slow down and start up anyway it's personal preference but I like to feel it is more than just a super Snappy instant controller let's tweak the lurp setting as you please or just remove it entirely if it's not your thing now the last thing we're going to add in this first tutorial is crouching we're already set an input button for crouching so let's first change the speed accordingly to pressing the Crouch button now crouching is dominant and what I mean by that is if you're pressing Sprint and you're pressing Crouch you're crouching you're not sprinting so we can wrap this whole sprinting logic in another if statement saying if input dot is action pressed Crouch we're gonna set our current speed equal to crouching speed and if we're not brushing Crouch then we might be able to Sprint or if we're not sprinting walk let's run this right now so this is walking speed when you press Ctrl we slow down to crouching speed let go of control we're walking again and if we're sprinting and we're pressing down control or crouching we're not sprinting now of course for crouching you're going to need to move your player down for this we're going to create a variable called crouching depth I'm gonna set it equal to minus 0.5 this is how much lower the camera will be when we're crouching relative to walking and then we're gonna set the camera to this height and with the camera we're just going to use the head for now so if we're pressing Crouch we'll set head dot position dot y equal to crouching dip and if we're not crouching then head dot position dot y will be equal to zero its starting location let's run this now oh sorry that's my mistake the starting location is of course not zero it is when we go into the edit set it to 1.8 so 0 to 1.8 and those other Heights when crouching equal to 1.8 the player Heights uh plus the crouching depth because this is already minus so we need to add it here Let's test this out right now you can see when you crouch you go down it's very jumpy and I think you can already guess what we're going to do to make this better we're gonna blurb it again so again we're gonna wrap this in a LARP function so it's a blurp open it up and the first variable is the variable we're going to want to learn so at the position of Y then we're gonna where we're going to learn by two which is right there and then again we're going to do it by Delta times our Loop speed we'll copy this whole line and paste it in right here and just remove this crouching depth let's run it again and now you can see crushing looks a lot better so we can Crouch we can Sprint only problem right now is when we go into our 3D scene and we take our floor hit Ctrl D to duplicate it move it up so we can have an example of an obstacle will have to crouch under I moved a little bit to the left as well now we're in the scene again you can see we can Crouch but we cannot move under it because we're not changing our Collision shape we're still this tall guy our head is just lower so let's fix this right now for this we're going to add a second Collision shape so I'm actually going to rename the current Collision shape by double clicking it saying naming it standing Collision shape and then I'm going to click it press Ctrl d and call this one [Applause] crouching Collision shape this one is of course going to be smaller so I'll go into the capsule and right click it and make unique this is very important otherwise we're going to be editing both of the capsules make unique and the height is not going to be two meters but I'm actually going to create maybe it's 1.2 Maybe and then drag it down so it's on the origin might be a little small we might have to adjust it but for now let's use this I also see the naming is not right all right let's save this go into our script and we're gonna need these two Collision shapes to enable them accordingly I'm gonna do this the same way we added in the head we're just dragging and dropping holding down control also for the crouching shape drag and drop pulling down control and then we're going to take these two variables and enable and disable them accordingly first I'm going to disable the crouching shape in the editor so we don't Crouch when we spawn and then right here it's a crouching logic in here the standing Collision shape we're gonna disable it by saying standing Collision shape dot disable equals true oh I'm going to copy this line and into the standing logic I'm gonna flip it so it's false and I'm going to copy this line again grab our crouching shape where it's right there and set this one to false and then right here copy it and set this one to true so now we're using our crouching Collision shape when we're crouching and our standing Collision shape when we're standing let's re-run the project and now when we Crouch we can go under this block let's print around run around and Crouch under this block now you're going to encounter one problem and that is when you're crouching and you go under something and then let go of Crouch you're able to glitch into the block above this is because there's no detection for checking if there's something above your head before you stand up so that's the last thing we'll add in this tutorial I'm gonna go back into 3D scene we're going to add something to detect if there's something on both those before we can stand up for this we're gonna go into the player node click add shout node a search for a Break cast 3D let me quickly drag it off to the side so I can explain what this is so a raycast 3D it's basically an beam an arrow which checks for collisions within this Arrow which is perfect for example because we can just point it up by two meters you can actually do this in Target position type 2 instead of -1 in the Y and now we're checking the whole player Heights if there's anything in the way and if there is we're not going to stand up and if there's not we're going to be able to stand up I'm going to drag this back into our player just transform zero zero zero zero and now when we go back into scripts we're going to need this node so again drag and drop it into your code holding down control trap the raycast variable and in here we're going to make sure that this raycast is not colliding before we stand up so we're going to change this else to elsif which is reading alif and then here we're going to check Graco 3D that is colliding and we're going to add a little exclamation mark in front to check if it's not colliding let's run this card again and now when we Crouch and we go under this block we let go we cannot stand up but when we move out of the block you can see we can stand up again all right perfect that's everything for this tutorial last thing I'm going to do is clean up the code a little bit because it's going to be quite a large file by the end of these tutorials so code organization is important first let's manage all these variables I'm going to add some labels here you can add comments in GD scripts by using little hashtag I'm going to call this player nodes this doesn't actually do anything in the code it's just for readability and then here I'm gonna copy this comment and gotta call this speed first I'm actually going to move this mouse sensitivity down with Direction so these two can be include hurdles then I'm gonna move this crouching depth with uh the speeds and the jump velocity and I'm gonna call these movement first actually the loop can also be right there you can see the template code already has some comments so it's nice we're just gonna have to come with our own little piece of code which is just this it's not a lot of code and this is going to be what we're doing here is handling handle movement States movement stay normal state and here we can add a little tag crouching if we ever need to add anything to crouching [Applause] foreign ERS will be crouching right here we'll have a piece of code that is extending so our standing logic if we're going to add anything we'll go here and right here we'll have our sprinting [Applause] right here walking right now it looks really weird to just come under these single line of lines of code but later it will make look sense and it will be very handy I promise you let's also add some enters to keep something separate like the Collision shaping at position yeah that's all readable all right perfect [Applause] oh perfect so that's our first tutorial done and the next one will handle head bobbing and free looking hope you enjoyed this one and I hope to see you in the next one bye
Info
Channel: Lukky
Views: 96,930
Rating: undefined out of 5
Keywords: godot, godot4.0, godot 4.0 stable, godot4.1, godot 4.1 stable, 2023, godot release, new, tutorial, getting started, start, learn, course, first person shooter, first person controller, godot engine, gaming, learning, rpg, godot 3d, first person, animations, godot game, gamedev, devlog, free
Id: xIKErMgJ1Yk
Channel Id: undefined
Length: 38min 9sec (2289 seconds)
Published: Fri Aug 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.