How to Create a FIRST PERSON Controller in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone in this lesson we're going to begin to create our player object this is going to be the player scene which is going to hold the player its collider and the camera and from there we're going to begin scripting the player so to begin let's go up to scene here and we're going to create a new scene it's going to be of type kinematic body right here now this is very similar to the 2d kinematic body which basically allows us to have some sort of physics interactions it allows us to move along surfaces collide with various different things it's really good for characters and control character controllers pretty much so I'm going to rename this here to player I'm gonna save it as play it up t SC N and as a child of this node we're going to be creating first of all a Collider so we'll just look up collision shape right here and this is going to be basically what the sort of size of the player what they can collide with so a shape here to be a new capsule shape right here right now it's a bit odd so what we're gonna do is we're gonna select the capsule shape we're gonna set the radius to 0.5 and the height to 1.5 now we need to rotate it and position it so in the transform drop-down here we're going to set the rotation degrees to 90 on the X so it's vertical now and one on actually 1.25 on the Y translation so it is just sitting above y0 right here okay great so we got our player right here now what we need to do is add in the camera and the camera is going to be what we see through the players eyes we're going to then be able to move this camera around with our mouse so for the camera I'm going to right click on player add child node and we are going to add in a camera right here by default its position down its feet we're going to be a bit higher up so I'm going to set the Y position of the camera here the Y translation to be 1.5 we want it to actually be 2.5 so it's sitting just above the players head right here and looking outwards and in fact we can click on the preview button right here in the same view to have a look through the camera and through the players eyes alright great so we got that we got the camera set up now what we need to do is set up the players weapon we need to set up the weapon that the player is going to have on their screen so we can see what we're holding on to and to do that we're going to open up our models folder here weapon and I'm gonna drag in the shotgun obj make sure it is a going to be a child of the camera object because when we do look around we do want the shotgun the gun model to follow the camera around wherever we look okay so we select the model I'm going to rename this here to be our gun model now in the inspector I'm going to set the translation to be zero zero zero so it is nice and centered here you might notice it's also pretty small so what we need to do is increase the scale a bit I'm going to set the scale here to maybe five by five by five that's still a tiny bit small so it's actually increase it to ten by ten by ten we'll set the Y rotation degrees to 180 so it is flipped around and facing the right way here and then what we can do is drag it forward a bit maybe move it down a bit move it to the right of it and let's look through our camera so I'm going to slap the camera here click on preview and there we go we've got a gun model now let's just reposition it a bit so it's in a bit of a better position alright so I'm going to set the X translation here to be point two and I set the Y here to be zero which is a bit high so maybe negative 0.5 note we'll probably want to make that negative point two there we go and Zed will put that negative point six something like that so here we go look at our gun model we've got that pointing outwards and we are also in needing to create a new node which is a child of the gun model actually doesn't have to be the child of the gun model in fact we're just going to make it a child of the camera so a right click on camera go add child node and this is just going to be a spatial node and this is going to be for the gun muzzle so where are we going to be spawning the bullet to then shoot okay so with the special here I'm going to rename it to muzzle and we want to position it basically just the muzzle of the gun here now with this view it's kind of hard to orientate yourself so what we can do in Godot is have some automatic orientations applied to the camera here the scene camera and for that you do need a numpad so make sure on your numpad you have a number lock turned on so you have the light with the num lock on and then I'm gonna press 7 to have an above view right here and what we're gonna do over the muzzle is drag it forward a bit and in fact right now we're on perspective view you can see that things change perspective as we zoom in but if we press 5 we're going to an or for the graph orthographic mode so that there is no depth shown so what I'm going to do here is hold down shift and with the middle Mouse we can pan across like this I'm going to position this right about here there we go we can then press one to get a side view and actually we'll press three to get this side view and then drag that down to line it up like so compress 5 to get out of the orthographic mode and there we go we've got a gun model we have the muzzle and everything here should be ready to go if we go back into the main scene now what we can do is drag in our player t sen into the scene set their transform here to be zero zero zero they should be standing in the middle of the field here with a and they should be standing on top of the surface here now if we press play you'll notice that it says no main scene as being defined so we'll select the main scene right here press play and there we go we go to our camera we got our gun model here and we're ready to begin scripting but before we do that it's good practice to actually go to an player here select the camera and enable current right here just in case you do have multiple cameras in the scene it's good to basically tell the game that this here is the camera we want to use so there we go thanks for watching and I'll see you on the next lesson welcome back everyone in this lesson we're going to begin to script our player we're going to have the ability to look around with our camera move around as well and jump and have gravity applied to us we're going to create a first-person controller here in Godot in the previous lesson we set up our player scene right here I will just open it up we set up our kinematic body as our main node then a Collider a camera and as a child of the camera we have the gun model and the muzzle where the bullets are going to come out so we've got that all set up now what we need to do is select the player node here and we are going to create a new script here in the inspector and this script will just keep it called player by default okay so we've got the script right here up and ready to go so the first thing we're going to do is create our variables first up let's create some stats so I'm going to separate these by some comments and a comment you can just go hashtag and then write whatever you want these won't get compiled with the code as these are just for us to see so first up we're gonna have some stats that we're not really going to be working with just yet but we'll be needing them later on so first up we have our curve HP for our current health and that is going to be of type integer and by default that will equal 10 we also need a max HP to know how much a health can go up to that'll also be an int of 10 by default then we've got our ammo which is int again and we'll have that at 15 and we have our score which we'll have as an int and that'll be 0 by default so we're not gonna be using these values just yet instead we're gonna be going down here and we're going to create some physics variables and these are going to be with the actual movement jumping and gravity so first up we have a move speed of type float so we want very precise numbers we can have decimals and by default we're gonna have this equal of 5 then we want to have a jump force so how much force is applied upwards when we want to jump that is going to be of type float again and we'll have that as 5 by default and then we also want to have our gravity which is also going to be float and this is going to be or have it at around 12 we can tweak this depending on if it's a bit too strong or it's a bit too weak okay so we've got that as well as this we also need some variables for our camera looking so with our camera so just look at cam look here we need to figure out is the maximum angle we can look upwards and what's the maximum angle angle we can look downwards because we don't want to be able to look up and down and just do a 360 that way we want to sort of clamp the vertical rotation so here we'll have our 4 min look angle of type float and this will equal negative 90 also have a max look angle so max look angle of type float and this will equal 90 as well so we can look 90 degrees up and 90 degrees down along with this we also need a look sensitivity because when we're moving our mouse around it might be a bit too slow or a bit too fast so we do want to be able to change the speed at which we look around so go VAR and this would just be look sensitivity of type float and this will equal 10 so we got those set up as well as this you also need some vectors we need to store our current velocity and the mouse mouse delta which is basically how far in which direction has the mouse moved in the previous frame so we'll just have our vectors here and these are going to be first of all far 412 for velocity is going to be of type vector 3 so we're storing a x y&z position because we are moving around in 3d space so our velocity can be in any direction and by default woods make this a new empty vector 3 like so and we also want the mouse Delta now since the mouse only works on two axes our horizontal and vertical we only need to have a vector 2 for this so there's give me no type vector 2 and we're going to make it a empty vector 2 by default there we go so without that here now what we need to do is actually get our components because on our player node right here we have a few things that we need access to such as the camera and the muzzle we need to know where the muzzle is in order to spawn bullets and we need to know what the camera is in order to move it around so I'm just going to go here components and we're gonna have var camera which is going to be of type camera and we want to actually be able to access this once the game starts silence which this variable over to an on ready variable which basically means whatever is after this equal sign here will be called once this node here is initialized and this allows us to basically look through our node structure and find the camera so we can go get underscore node and then we can just search camera and it is going to get the camera node that it finds now we also need to get the muzzles so we can go on ready var muzzle is going to be all the type spatial because this year is just going to be an empty node and this is going to equal get underscore node now we need to get the node path to that node now it is a child of camera so first of all we need to enter in camera slash muzzle there we go so that's how we access the muzzle node right here and all that setup let's actually go ahead press play make sure there's no errors with any of these on ready variables right here so we press play and we are all good there's no errors popping up here so we can exit out of this and from here we've got all our variables now what we can do is actually start and setting up our physics setting up our movement setting up our jumping our gravity and looking around with the mouse so first of all before we actually jump into that what we need to do is set up our actions our key actions because right now the game doesn't really know what move left move right move forward move back means they don't it doesn't know what the attack button is it doesn't know what the jump button is so we need to define these things inside of the dough here and to do that we can go over to project project settings input map and over here is where we can enter in our new actions so first up we're gonna have one action this is going to be move underscore forward press ENTER to add it to the actions here we also want to move underscore backward backwards move underscore left move underscore right we also then one jump and we want shoot so we got these are six new actions here and what we want to do is sign a key to it because right now they've got no keys you can see some of these other actions here that are default in Godot do have actions I do have keys corresponding to that action so for move forward let's click on the plus icon right to its right select key and then we can just click on whatever key or we want that to be assigned to so move forward I'm going to set it to W press ok move backwards we're going to set that to s move left we'll set that to a move right here can be d jump or have it as space and shoot here is going to be a mouse button so I'm going to click on the plus choose mouse button and as you can see by default it is left button right here you can choose from all the various different mouse buttons device with device here it doesn't really matter what you select you can select device there but I'm just gonna select all the devices just to make sure because there's no multiple controllers in this game so all devices is just the safe way to go click Add and there we go we'll get our move forward backwards left right jump and shoot actions all set up ready to go so we can start detecting them inside of our script so I'll click close right here and what we're gonna do now is actually start to create our function which is going to run the physics and this is going to be the underscore physics underscore process function so let's create it right here so I'm just going to go func underscore physics and what you should see is it automatically pops up with a selection we can just hit enter and it creates the physics process function now this function is built into Godot more specifically it is built into our players kinematic body node right here which we are extending from at the top so we do have those attributes we can access and this function gets called 60 times a second this is a consistent update function now Godot also does have the default underscore process function which gets called every single frame but doing physics in the process function at times might not be the best because that is not a steady call right that gets called as many times as you have frames per second so you might be fluctuating between 60 100 so the process function can be cool any number of times during a second whereas the physics process function gets called at a consistent 60 frames per second which is good for physics as you don't want variations in the time step so we got the physics process function right here and we're gonna be doing a number of things here and we'll be doing a number of things here such as detecting inputs moving the player jumping having gravity being applied to us and we'll be working on that in the next lesson welcome back everyone in the previous lesson we start to create our player script right here we entered in all of our variables we're going to be using for now and we also created the physics process function which gets called 60 times a second now there is going to be an error right now because we don't actually have anything in the function so it's got some problems of it being empty but let's go fill it in so first of all what we want to do is reset our X and z velocity of this velocity vector R here because when we let go of a bun we don't want to continuously be moving in the same direction as when we were holding a button down so we want to stop once we let go of a button and the reason why we're not just resetting the vector as a whole is because we want to maintain the Y velocity because if we're falling and we like of a button we don't all of a sudden want to reset our Y velocity we want to keep falling down so here we're going to reset the X and Z velocity and to do that we're just going to go Valdo x equals zero and vel z equals zero now what we need to do is create a new vector to to basically get our inputs because what we're gonna do is we're gonna be checking to see if we press WASD and these keys are going to correlate to a moving forward if we're moving back after moving left if we're moving right and we're gonna store that in a temporary variable which is going to be a vector - so we'll go var input equals vector - so we're gonna be storing the left right and up and down inputs in this vector right here and we're gonna do that in movement inputs so we're going to be checking first of all if we're pressing the forward button so if input dot is underscore action pressed so every frame that this action is being held down this is going to be true so we want to check for move forward : at the end and what we're going to do if this is true is we're going to set input dot y- equals 1 so but right now if we press move forward y is going to be negative 1 now when you see this for all 4 movement axes so then we can go if input dot is action pressed and we want to check for move backward then we are going to go input dot y plus equals 1 ok as well as this then we need to check the left right then we're good so I'm just going to copy this right here and paste this twice more we'll check move underscore left and then move underscore right like so okay so for moving left we want to go on the x-axis and go minus equals 1 and if we're moving right we want to go plus equals 1 on the x-axis so we're going our movement inputs and this input vector here is being changed depending on which buttons we have held down now what we need to do is normalize this vector and what normalizing it does basically means that the magnitude or the total of all the axes added together will equal 1 and the reason why we're going to do this especially for a character controller playercontroller is because if we are holding down let's just say the forward and right bun we're gonna be moving at a diagonal angle and this movement is actually going to be faster than if we were just moving forwards or backwards or left or right if we're moving at an angle by default it is going to move us faster so we want to normalize this vector so no matter which direction we're moving in it was always going to be the same speed so to do that we're just going to go input equals input dot normalized like so okay now that we have our input set up and ready to go what we want to do is get our forward and right directions of our player we want to know which way is our forward direction and which way is our right direction and we're going to need these in order to actually figure out which way we should set our velocity because right now if we just set our velocity it's not going to take into consideration which way we're looking it's just going to move us along the global x and z axes so what my new suit is we need to get the forward and right directions and to do this we are going to create a variable here called forward and this is going to be equal to our global underscore transform so we're getting our global properties instead of our local properties because local might be different depending on if we're a child of another object so again the global transform dot basis dot Z so this returns us the direction we are looking forward now to get the direction we are looking towards on the radical direction we're looking but the direction to our right which we'll need for moving sideways we're gonna go VAR rights equals global underscore transform dot basis dot X so now that we have our forward in right direction what we can do now is create a new vector which is going to store the relative direction so this is what we're going to be then assigning to our velocity in order to move in the direction we are facing so I'll create a new var for our vector which is going to be called relative direction here and this is going to be equal in parentheses here we're going to do a bit of a calculation to figure out which way we're facing with our forward in right direction as well as our input here which we're going to be using to figure out which way we're going to be moving so we want to go forward x input Y plus write x input X and this is going to give us a vector 3 which is going to be the direction we're going to move in so we got that now what we need to do is we need to set the velocity now we've our velocity vector we don't want to set the x y&z we just want to set the X and the Z because the Y is going to be set a bit later on after this depending on if we're jumping or not so for this we're just going to go vel x equals relative direction dot x x our move speed because we do want to have that variation in our move speed if we want to increase it or decrease it later on then we got Val Z equals relative z x move speed as well so our velocity vector has been filled in now what we need to do is apply gravity so with gravity here we'll go apply gravity all we're gonna do for this is just go Val Y minus equals gravity multiplied by Delta so each second we are going to be adding this amount of downward velocity to ourselves and this multiplied by delta pretty much it converts this into per second rather than a per frame because if we didn't have this then we'll be subtracting a gravity which is around 12 at the moment so we'd be adding in negative 12 velocity to our player along the y axis every single frame 60 times a second which is a lot instead we're going to be adding that every second which is done by multiplying by Delta which is the time duration between each frame so we're setting our velocity was setting or applying our gravity now what we need to do is just move the player have this velocity vector be applied to the kinematic body so we can move and what's really handy of this is that the kinematic body node comes with a function which allows us to easily do this so move the player what we're gonna do is just go Val equals move underscore and underscore slide which is also includes some collision stuff as well and we just want to give it the property of our velocity and the ground normal so which way is the which direction is the ground facing that is going to be vector three dot up which is a built in vector which just cat which is just zero one zero four up and there we go so we're moving the player here now all we need to do finally for this function is add in the ability to jump and that can be done by going sors have jumping and that can be done by going if input dot is under school action underscore pressed so if we're pressing down the jump button then what we also want to check is are we grounded because if we can just press the jump on then we'll be jumping continuously so we want to check also and is under school on under school floor which is built into the kinematic body as well so if so if we're on the floor and we hold down the jump button then what we want to do is go vel dot y equals jump force so we're setting our Y velocity to be whatever the force is to jump upwards okay there we go so we've got our function right here we got all this functionality built in now let's press play and test it out so press play so if we can't look around right now but if I go WASD you'll see that we can move space jumps jumps us up it's not a big jump but we could tweak that a bit and yep there we go we can move around we can jump we have gravity applied to us in the next lesson what we're going to do is focus on setting up the mouselook so we can rotate our head around and move in the direction we're looking so thanks for watching and I'll see you then in the next lesson welcome back everyone in the previous lesson we set up our ability to actually move around jump and have gravity being applied to us so in this lesson we're going to be focusing on actually their being able to look around with our camera so first of all what we need to do is figure out how and where is our mouse moving what is the direction our mouse is moving in and how far is it moving and we're going to be checking that inside of a built in function to gadot which is called func underscore input and it comes with an event parameter and what this function does is it gets called whenever an input is detected in Godot so whenever a keyboard inputs detected if you click a mouse button even even if you move the mouse this function here gets called with the corresponding information sent over in the event parameter here so what we want to do is check first of all if this event right here is that to do with the mouse motion so is this to do with moving the mouse if it is about moving the mouse then we want to set the mouse Delta to be basically direction that a mouse is moving it so we're gonna go if event is input event Mouse motion right here so if the event is to do of mouse motion then what we want to do is just go Mouse Delta equals event relative like so okay and what we can do now is up inside of the process function which gets called every single frame we're doing it in the process function and not the physics process because if you've got stuff that isn't really to do with physics it's recommended that you just do it in the process function so I'm gonna create the process function here underscore process which gets called our every frame and what we want to do here is first of all we want to rotate the camera along the x-axis because with this camera setup the way we're going to be doing it is we can be rotating our camera vertically so we're gonna be rotating the camera node up and down so we can look up and down but we're not gonna be rotating the camera left and right instead we're gonna be rotating the actual player node here left and right and the camera is going to be staying still so the camera is only ever going to be rotating up and down and if we look left and right we're gonna be rotating the entire kinematic body node left and right this just makes it a lot easier for women calculating stuff such as the forward direction which we done previously so to do this we're gonna go camera dot rotation underscore degrees dot X and what we're gonna do is we're going to subtract equals so we're going to take away from this the mass Delta Y so the up-and-down of the mouse movement multiply x look sensitivity or multiply it by Delta so it's converted into into in terms of seconds so it's a bit smoother and now what I'm going to do is clamp at the rotation because if we have our mouse and we move it upwards the cameras going to rotate upwards and it's going to keep rotating upwards until it loops back around and it's just going to be really weird and not really fit with this first person controller so we want to clamp it so we can only look so far up and so far down so clamp camera x-axis and to do this we're going to go camera rotation degrees dot x equals clamp now this clamp function takes in three values it takes in the value that we want to clamp then it we need to give it a minimum and a maximum so the value we want to clamp is going to be the camera dot rotation degrees X the minimum is going to be the min look angle and the max is going to be the max look angle just like that okay so we've got that all set up now we need a set on set up actually rotating left and right because right now we can look up and down let's look left and right so we're going to rotate the player along their y-axis here and to do this we can just go rotation underscore degrees because this is the script is attached to the player that were rotating so we'll go rotation degrees dot y equals oh we need to go minus equals Mouse Delta X x look sensitivity x Delta and we don't have to clamp this value because looking left and right there's no minimum or maximum angle we can look we can look full 360 degrees horizontally so we can just leave like that finally we've Auto reset the mouse Delta vector so that we don't keep looking in the direction that the mouse is moving so we'll go mouse Delta equals vector two to restart to reset it and that is pretty much it so what we can do now is press play and hopefully it works all right so we can look around and there you go we can look left right up down but something you might want in this game is for the ability for the mouse to not be free like this because right now if we look outside of the window the mouse it doesn't continue to track if we move it in it continues but it's very weird and it's not that usable so what we need to do is lock the mouse cursor in the middle of the screen and hide it so when we move our mouse we don't see the cursor and the cursor can't go off screen now to do that we're going to go to our player script here we're going to go to the top of we're going to go to the top of the script and we'll create the underscore ready function now the underscore ready function gets called once the node is initialized so this is going to be called once right at the start of the game and what we're gonna do here is we are going to hide and lock the mouse cursor so to do this we want to go input dot set underscore Mouse mode and with this we can give it a number of different modes right here there's captured there's confined there's hidden and there's visible visible is what is is what it is right now it's visible on screen and we can move it around freely hidden basically just hides the mouse cursor so we can't see it but it's still going to be moving confined means that it is just going to be confined to the screen that's kind of what we want but we don't want the mouse visible so we're gonna be using mouse mode captured which hides the mouse and locks it into the center of the screen so there we go and now what would happen is if we press play here you'll see that when you press play the mouse cursor disappears and here we are with our character we can look around the mouse doesn't appear off screen and if we move you'll see that we move in whatever direction we are looking at and that is being done by getting the forward and right directions and creating the relative direction in order to actually set this up if we look up you'll see that we don't continue to look up we sort of clamp at the top here it's kind of hard to see so I'll go near our tree you see how we look up sort of stops if we look down it also stops so we can't look all 360 degrees up and down but left and right that is possible jumping works as well and yeah so that overall is our player controller setup we have the movement implemented we have the camera looking implemented and that is pretty much how you set up a basic character controller inside of Godot in the next lesson we're gonna be focusing on actually setting up the ability to shoot bullets as that's going to be one of the main mechanics in this game we got our gun here we got that muzzle object set up now all we need to do is be able to left click on the mouse button and shoot out a bullet and from there we'll also set up enemies that we can shoot pickups and the UI system later on just something quickly also you may notice that you can't really get out of this window right here you click escape nothing really happens it's it's kind of hard to get out of this window so what you can do is you can just hit alt f4 to quit out of that little gameplay window it goes back to the ghetto editor and it stops playing the game see ya there you go I'll see you all in the next lesson
Info
Channel: Zenva
Views: 14,838
Rating: undefined out of 5
Keywords: zenva, zenva lounge, tutorial, programming, coding, software development, web development, online course, godot, godot learn, godo, unity, godot tutorial, 2d, 3d, fps, first person, controller, player, shooter
Id: mzsWfLsjG4w
Channel Id: undefined
Length: 33min 51sec (2031 seconds)
Published: Tue Mar 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.