Juiced Up First Person Character Controller Tutorial - Godot 3D FPS
Summary
TLDRIn this tutorial, the creator guides viewers through building a first-person character controller from scratch, integrating features like head bobbing and field of view changes to enhance the game experience. Utilizing Godot's CSG box for prototyping, the character is set up with basic movement and jump functionality. The video delves into coding details, including handling gravity, jump mechanics, and movement direction. It also covers input mapping, camera rotation, and mouse sensitivity adjustments. Additional game juice is added with head bobbing effects using sine waves and sprinting mechanics, concluding with inertia and field of view adjustments for a more immersive gameplay.
Takeaways
- 🎮 The video tutorial covers creating a first-person character controller from scratch with added 'game juice' features.
- 🏗️ The process begins by setting up a new 3D scene and using a CSG box for ground prototyping and a character body mesh for the player.
- 👾 A script is added to the character for basic movement and jumping, which works for both first and third-person perspectives.
- 💡 Lighting and a world environment are set up with a directional light to mimic the sun for a realistic look.
- 🔒 The camera is added as a child of a node 3D to prevent 'janky' rotation issues, with a reference to Godot documentation for further details.
- 📏 Constants for speed, jump velocity, and gravity are defined, with the physics process handling jumps and character movement.
- 🕹️ Custom input mapping is created to replace default UI actions, allowing for more personalized control settings.
- 🔍 The tutorial discusses handling mouse input for camera rotation, with sensitivity adjustments and clamping to limit rotation range.
- 🏃♂️ Head Bob effect is implemented using sine waves to simulate the character's footsteps, enhancing the game's immersion.
- 🔄 Inertia is added to the character's movement to prevent abrupt stops in mid-air, providing a smoother player experience.
- 🔬 The camera's field of view (FOV) changes dynamically based on the character's speed, adding another layer of realism to the gameplay.
Q & A
What is the main focus of the video?
-The video focuses on creating a first-person character controller from scratch with various game feel enhancing features, also known as game juice.
What are some of the game feel features included in the character controller?
-The game feel features include head Bob, inertia, movement, and field of view (FOV) change.
What is the purpose of using a CSG box for the ground in the 3D scene?
-The CSG box is used for its simplicity and combination with a collision shape, which is good for prototyping.
Why is the character body using an old code character body 3D?
-The old code character body 3D is used because it has a default code template with movement and jumps implemented, which works well for both first-person and third-person control.
How does the video handle the lighting in the game scene?
-The video adds a world environment and a directional light to the scene tree to imitate the sun and ensure proper lighting.
What is the purpose of adding a camera as a child of a node 3D?
-Adding a camera as a child of a node 3D acts as a pivot to avoid janky results when rotating the camera without one.
What is the issue with the default UI actions in the character controller code?
-The default UI actions are replaced with custom ones to allow for more control over the character's movement and interactions.
How does the video address the problem of the character not moving in the direction it's facing?
-The video suggests setting the direction of movement to where the head is facing instead of the character body, as the head is responsible for looking left and right.
What mathematical function is used to implement the head Bob feature?
-The sine wave function is used to implement the head Bob feature, which moves the camera up and down to imitate footsteps.
How is the sprinting mechanic integrated into the character controller?
-Sprinting is integrated by changing the character's speed variable based on whether the shift key is pressed, with separate constants for walk speed and sprint speed.
What is the purpose of the inertia in the character controller?
-Inertia is added to give the player more control over horizontal movement in the air and to ensure a smoother transition when the player stops moving mid-air.
How does the video change the camera field of view based on character speed?
-The video uses a formula that adds a base FOV and a FOV change multiplied by the character's velocity, clamping the velocity to ensure the FOV doesn't become too extreme.
Outlines
🚀 Creating a 3D Character Controller with Game Juice
The video script begins with an introduction to creating a first-person character controller from scratch, focusing on enhancing the game feel with various features known as 'game juice'. The tutorial covers setting up a 3D scene with a CSG box for the character's body, using a mesh and collision shape for better performance. The character will use an old code character body 3D with a bean shape mesh. A script with movement and jumps is added, and lighting is set up with a world environment and directional light. The character is given a camera as a child node to act as a pivot, and the script explains the importance of this setup for smooth camera rotation. Basic movement and jumping mechanics are implemented, and the script is reviewed to ensure proper functionality, including handling jumps and movement on the floor.
🎮 Enhancing Player Experience with Head Bob and FOV Adjustments
This paragraph delves into improving the player's experience by adding head bobbing to mimic the character's footsteps using a sine wave function. Key variables such as Bob frequency and amplitude are introduced to control the bobbing effect. The character's movement direction is corrected to align with the head's orientation rather than the body's. Sprinting is added to the character's movement, adjusting the speed based on whether the shift key is pressed. Inertia is introduced to the jump mechanic to prevent the character from stopping abruptly in mid-air, using the lerp function to gradually adjust the velocity. The tutorial also addresses issues with the character's stopping mechanism on the ground, adding a ramp-up effect for more realistic deceleration. Lastly, the camera's field of view (FOV) is dynamically adjusted based on the character's speed, creating an immersive experience that responds to the player's actions.
🔧 Final Touches and Community Engagement
The final paragraph wraps up the character controller tutorial by addressing the last details and seeking community feedback. The script includes a velocity clamping mechanism to ensure the field of view doesn't become too extreme during high-speed falls. The tutorial concludes with a smooth FOV transition using the lerp function. The creator invites viewers to engage with the project on GitHub and expresses openness to creating a follow-up video if there's enough interest. The video ends with a call to action for likes and comments, encouraging viewers to share their thoughts on the tutorial and suggest additional features for future content.
Mindmap
Keywords
💡First-person character controller
💡Game feel
💡CSG box
💡Node
💡Head Bob
💡Inertia
💡Field of View (FOV)
💡Physics process
💡Sine wave
💡Lerp function
💡Godot documentation
💡Input map
Highlights
Creating a first-person character controller from scratch with enhanced game feel features.
Introduction of head Bob, inertia, and movement FOV change as key features for game juice.
Utilization of a CSG box for prototyping with a simple mesh and collision shape.
Adoption of an old code character body 3D with bean shape mesh for character design.
Optimization through simplified convex collision shapes for better performance.
Scripting for movement and jumps with a default code template suitable for first and third person control.
Setting up a world environment and directional light for realistic lighting.
Camera setup as a child of a node 3D to avoid janky rotation results.
Explanation of Godot documentation and code snippet for camera rotation without a pivot.
Implementation of speed and jump velocity constants with gravity variable adjustments.
Physics process handling for jump mechanics and character movement.
Correction of a mistake in friction implementation affecting player movement.
Custom input mapping for game actions and cursor capture for free movement.
Defining variables for head and camera rotation with sensitivity adjustments.
Use of clamp function to limit camera rotation for a more realistic experience.
Creating a map using CSG boxes with different color materials for visual enhancement.
Addressing the issue of character movement direction in relation to the head's orientation.
Implementation of head Bob using sine waves to mimic character footsteps.
Addition of horizontal head Bob component for more realistic movement.
Adjustment of character speed with sprinting mechanics for varied player speeds.
Fixing jump mechanics to add inertia and prevent immediate stops in mid-air.
Smoothing character movement with lerp function for horizontal control in the air.
Improving the running stop mechanic with a gradual deceleration effect.
Changing the camera field of view based on character speed for dynamic gameplay.
Finalization of the character controller with a complete set of movement and visual features.
Invitation for feedback and potential creation of a second part for additional features.
Transcripts
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
تصفح المزيد من مقاطع الفيديو ذات الصلة
How To Make A Video Game - GDevelop Beginner Tutorial
How To Make a Topdown RPG in Godot 4: Adding The Player (Part 1)
Lecture: 14 | WATER BOAT GAME IN SCRATCH | GAME DEVELOPMENT | SCRATCH TUTORIAL
2023 Unity VR Basics– Continuous Movement
Roblox Studio - 8 Direction Movement System - Scripting Tutorial
UE4. Інтеграція персонажа в гру
5.0 / 5 (0 votes)