Godot FPS Tutorial - Part 1 - Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone i'm sky western and in this tutorial series i will be making a basic fps controller as well as a weapon manager with which we can do basic stuff like switching weapons picking them up etc and finally i will be creating a wall running mechanic which as of now is not very polished but more on that later for part one that is this video i will create a basic fps controller with which we can look around move and jump i will start by setting up the game world though i will going to fast forward this part as i assume prior to watching this video you have a basic understanding of working in godot and its 3d interface so if you want you can go straight to the timestamp shown on the screen where i begin creating the player or else if you want you can see the way how i have set up the scene now i will create a new scene for the player then add a kinematic body as a root now i will add a mesh instance and set its mesh to a capsule after doing this i will align the capsule to the general orientation of the player by rotating it by 90 degrees around the x-axis finally i will do some adjustments to the shape in order to set its height to around 1.75 meters which is just to stay consistent with the real world proportions and also move the mesh instance up a little bit so that the bottom of the capsule touches the origin now add a collision shape set its shape to a capsule and change its parameters to the same values as that of the mesh instance [Music] now for setting up a camera i will add a spatial node name it anything you feel like i will name it cam root then add a camera as a child of it just make sure when aligning the camera don't move the camera itself move the cam root node instead as the position and rotation of the camera should remain zero [Music] now save the player scene and add a script to it [Music] now i will create a constant variable for the mouse sensitivity so first of all before we do anything let us better understand how the player scene is set up as we can see the camera by default points in a negative z direction which makes things kind of confusing but i recommend to keep it this way only so from now on if we have to move the player forward then it means we have to go in the negative z direction with respect to the player now to look around we will first do a vertical rotation on the camera root node we will rotate it around the x axis and make sure to convert degrees to radians after that we get the mouse position relative to the last frame times the mouse sensitivity as the mouse relative position gets returned in 2d space so the y value is positive downwards so we will have to multiply the result by negative 1 since the rotation increases when we want to move the mouse up similarly we will do the same thing with the horizontal rotation but this time on the player node itself we will rotate it around the y axis convert degrees to radians and multiply the result by -1 and that's because our rotation around y increases towards left now in the input function let's perform the camera rotation now let's set players into the world scene and place it somewhere over the ground as we can see we are having some trouble moving the mouse around to fix this we will have to set the mouse mode to capture but then in order to make the running window inactive or just move it around for debugging we will need to make the cursor visible too so to make our life a little easier let's create a function to handle the window activity by the way this is temporary and will later be replaced by a pause menu [Music] [Music] the mouse movement feels much better now but you might have also noticed that we are able to rotate the camera infinitely around the x-axis and that is something we don't want to fix this we simply have to clamp the rotation of camera root i will go for 75 degrees but you can choose any value you feel like as we can see the camera movement works much better now now let's do the player movement now let's override the physics process function but before we go any further let's create some actions we will need for the user input now let's declare some variables i will first get a reference to the camera node which we will use for getting the direction then i will declare velocity which is going to be used for player movement whereas current velocity will be used for interpolating in order to smooth out the movement and finally dirt will hold the input directions now i will declare some constants to be used for our calculations like speed acceleration etc i have defined them as constants but if you want you can set them as normal variables too and export them to inspector all of these variables would make sense in just a minute now i will first reset direction every frame to a zero vector then update it by adding the camera z and x axis if a button is pressed which basically means the direction we are looking at finally we will normalize the direction vector now i will get the right speed and multiply it by direction and this will give us our target velocity with this we will interpolate current velocity towards the target velocity and using acceleration as the factor then assign current velocity to velocity and finally pass the velocity variable to move and slide also make sure to return the value to velocity itself otherwise we will get some weird behavior now this seems to work fine but as you might have noticed that we can move vertically too which is something we don't want and also let's implement gravity and jumping as well now let's declare some more variables i will set gravity to minus 40 rather than minus 9.8 but you can choose any value you feel like whereas jump speed is the initial velocity when we start to jump and jump counters to limit the number of jumps we can do in middle while air acceleration would simply delay the movement by a tiny bit while in middle [Music] now in every frame we will add gravity to the y component of the velocity vector and if the player is on ground then we set jump counter to zero finally we would check if the jump button is pressed and the jump counter is less than one [Music] if the following conditions are true we increase the jump counter by one and set velocity.y to the jump speed [Music] then we will get the acceleration value to be used based on whether we are on ground or not and use it in the interpolation finally to fix the vertical movement we only assign x and z components of current velocity to velocity now here we go we have created a basic fps controller in the next part of this video we will be creating a weapon manager for our game i hope this video was helpful and i will see you in the next part [Music] you
Info
Channel: Skyvastern
Views: 44,828
Rating: undefined out of 5
Keywords: Godot, 3D, FPS, Controller, First Person Controller
Id: APc7VXW12yg
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Tue Nov 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.