Juiced Up First Person Character Controller Tutorial - Godot 3D FPS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the channel today we'll be making a complete first person character controller from scratch with a bunch of features to enhance the game feel also known as game juice these features will include head Bob inertia and movement fov change so let's Dive Right In we're going to create a new 3D scene and call it world for our ground we're going to use a node called CSG box which is basically a simple mesh combined with a collision shape that's good for prototyping our character is going to use an old code character body 3D we're going to add a mesh to it by choosing the bean shape and the mesh drop down we're going to create a collision shape from the match by going to this menu and selecting creates simplified convex Collision sibling if we turn off the visibility on the mesh we can see that we end up with this disheveled beam that has less vertices than their original which is good for performance let's add a script to our Bean character bodies get a default code template with movement and jumps implemented which works well for both first person and third person control in order for the lighting to work properly in our game let's add a world environment and a directional light to imitate the sun to our scene tree let's take a closer look at our Bean the last bit of setup we need to do is adding a camera to him and adding it as a child of a node 3D which will act as our pivot if we don't have one and try to rotate the camera we get some very janky results if you're curious as to why this happens I will link to a page in the Godot documentation that explains it really well and also has a code snippet that would let us do it without a pivot now if we start up our game we are able to move around and even jump out of the box but we can't look around just yet so let's get cracking on our code we have the speed and jump velocity constants here the gravity variable is taken from the default project settings which is 9.8 screw that it's now 9.8 and the physics process we handle our jump when the player presses space we set the velocity so we're going up and then every tick we lower this velocity if we are not on the floor until the velocity is in the negatives and we are going down at the end of each tick the move and slide function is called which moves around our character depending on this velocity vector and then changes the velocity depending on collisions so when we're about to end our y velocity is negative 4.5 and then bam it's zero and it stays at zero because we are now on the four and are no longer changing it this last code block takes care of movement on the floor it tells us that we should replace the default UI actions with our own which we will do in a second here we get a vector 2 movement Vector from the inputs and change it to a vector 3 so we can multiply by where our character is facing which gives us this movement Direction Vector if it's non-zero which means that we pressed a button we set the velocity components by multiplying the direction vector by our speed this next part has a bit of a mistake in it the intention here was to add friction and swell the player down gradually but as this thread on the Godot GitHub suggests it just sets the velocity to 0 and instantaneously so let's make that clear and the code the next step is adding our own input map we can do this by going into the project settings naming all of the actions that we want to use in our game and setting bindings for them we add our new actions to the code and then see what we need to do we can see our cursor so it's not captured and we can't walk around so the first step then is to get rid of the cursor we can do that in a ready function which runs at the beginning of a scene by setting the mouse mode to captured after that let's define variables for the head and the camera so we can access them easily in the code references to nodes have to be saved in on ready variables and if we want to get the path to them we can just drag them from the scene tree next to actually rotate the camera we use the unhandled input function which is called every time up where it does anything like press a button or move the mouse we are working specifically for Mouse motion events so we check if it's of this type we change the rotation of the head depending on how much the mouse moved relatively multiplied by the sensitivity which we're going to add as a constant here we're keeping the rotation axes separate so we're only rotating the head on the y-axis and the camera on the ax axis to avoid the whole rotation order Mass you'll also notice we're rotating the head y angle depending on relative X distance this is not a mistake but rather just a byproduct of how translating Mouse movement to camera rotation works when we move the mouse left and right we want to rotate around the y-axis which is pointing up and when we move the mouse up and down we want to rotate around the x-axis which is pointing to the side so after putting all of this together we can finally look around our sensitivity might be a little too high and also we can do cart goes with our head so let's use the clamp function which can limit the rotation of our camera to a minimum and a maximum value I'm also getting tired of staring at a white four with nothing on it so let's use a bunch more CSG boxes with different color materials to create a map much better the last thing we need to fix with the basic movement is the fact that we're not really moving in the direction that we're facing this is an easy fix in the code right now when we set the direction of our Movement we are setting it to where the character body is facing instead we need to set it to where the head is facing since that's the component that's looking left and right for us nice now let's juice this game up the first thing that we're going to add is head Bob that will help us imitate the character's footsteps by moving the camera up and down maybe something like this can you guess which mathematical function we can use that's right the function of my emotional well-being while I was waiting for Godot 4 to release I can't believe it's here or the Western known sine wave to implement this we are going to need a couple of variables and constants first is Bob frequency which will affect how often our footsteps happen second Bob amplitude which will affect how far up and down our camera will go and finally we will need a variable that we will pass to the sine function that will determine how far along the sine wave we are at any given moment we're going to increment this variable during every tick of the physics process by multiplying a couple of things that affect our head bar first we want to account for Delta or how much time has passed since last tech we will multiply this by the speed of our character since we want to add Bob more often when we're running and finally we want to make sure that we're only bobbing when we're on the four and walking rather than jumping or falling so we will multiply the result by there is on 4 function which converted to a float will either return one or zero we can then assign the position of the camera to the result of the head Bob function that we will pass the T Bob variable 2. let's code this function real quick it will return a vector 3 variable that we are setting the camera position to within the physics process initially this Vector 3 is going to be a zero Vector but we are going to assign its y-coordinate to the sine of T Bob times Bob frequency and then multiply that by the Bob amplitude so now instead of going from negative 1 to 1 to X sine it will go from negative amplitude to amplitude the result is a pretty good head ball but if you're looking for a bit more realism we can also add a horizontal component to this since our head is also moving left and right when we're walking this time we're going to assign the position X component to the cosine of time or t Bob times Bob frequency divided by two still keeping up multiply this by Bob amplitude as well I think this will look way better for a lot of games now let's add sprinting to make sure that our Bob looks good at different player speeds to do that we'll change what we set our character velocity to and this code block since we now want our speed to depend on whether we press shift or not it has to be a variable so let's create a speed variable and change our speed constant name to walk speed as well as create a new constant called Sprint speed back in the physics process we will run a check on whether the player has pressed shift or not and then set our speed variable to either Sprint speed or walk speed with that all together we can now run like the wind in our game but let's look closer on some other mechanics specifically there's an issue with the jump where we can stop immediately while in the air if we let go of the movement button so let's fix that by adding a little bit of inertia to the game we're going to do this by only giving the player full control of the horizontal movement when they are on the floor when they are in the air we are instead going to interpolate towards the desired velocity using the lerp function which changes our speed incrementally this function takes three variables in this case it's our initial velocity then our Target velocity and the final one as the decimal percentage of the distance between the two variables that we want to cover in each step since our Delta variable is usually around 0.05 multiplying it by 2 would make our current velocity move toward the target velocity by 10 percent each tick now when we let go of the movement button in midair we won't just stop right there you can play around with this value to figure out how much control you want while you're Airborne but I eventually settled on Delta times 3. another thing we can do is fix that running stop mechanic that wasn't quite working in the character template just ramp up a little how fast we come to a stop here and Bam inertia done there's one last thing I want to do to juice this character controller up and that's changing the camera field of view depending on character speed for this we're going to need two constants the base fov which you could change to a variable if the player can set the fov on your game as well as fov change which will multiply by our speed and add to our base fov and the physics process we will be setting a local Target fov variable to this equation which is base fov plus fov change multiplied by velocity the reason why we want to clamp our velocity is to make sure that our fov doesn't go too crazy when we're falling when our velocity reaches super high numbers so let's create a velocity clamped variable which will set to the result of the built-in quam function and restrict it to the minimum of 0.5 so that we don't change that for V when the velocity is super low and a maximum of running speed times 2. after that we'll use our favorite lerb function to set the camera for movie smoothly every tick of the physics process again the first parameter is their original value the second is the target value and the third is the decimal percentage distance that we want to cover and with that our character controller is complete the link to the GitHub project will be in the description so you can start where I left off I might make a second part for this controller if you guys want me to add any specific features to this and if there's enough interest if the video was helpful drop a like and let me know what you think
Info
Channel: LegionGames
Views: 132,705
Rating: undefined out of 5
Keywords: Godot, Godot 4, FPS Controller, First Person, First-Person, Character Controller, First Person Character Controller, Game Juice, Game Feel, Headbob, head bob, sprinting, fov change, field of view, inertia, Godot tutorial, Godot 3d Tutorial, Legion Games, adventure game, first person game, platforming game, Godot FPS, FPS, game development, godot engine, survival game, horror game, Godot 3d
Id: A3HLeyaBCq4
Channel Id: undefined
Length: 10min 47sec (647 seconds)
Published: Fri May 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.