hey it's lucky
today we're going to be building a third
person controller
I've shown a lot of third person
controllers in my project videos and
gotten a lot of tutorial requests so I
thought I'd finally get around to it
there are a lot of different types of
third person controllers the most common
ones are the over the shoulder one like
Gears of War pubg fortnite
but you also have the over the head one
used in the World of Warcraft Dark Souls
and similar games I'm going to be
showing you how to build both all the
assets I used in this tutorial are
linked Down Below in the description
first I'm going to show you how to set
up the controller and then I'm going to
talk about some of the problems you're
gonna have to face when building third
person games alright let's get started
alright the first thing you're going to
need is this assets folder
it's going to be the first link in the
description
in here you'll find the player model
it's from miximo it'll be the second
link in the description
and textures from Kenny and now
the great ebook grid textures I'll link
his site as the third link go support
him he's awesome
all right let's set up the stage
so we'll open up Godot
create a new project I'm going to be
using the forward Plus renderer
I'm gonna drop this in my likeado 4
folder
creating a new folder color it
third person tutorial you can name it
whatever you want
select current folder and create
load up
and the first thing we're going to do is
drop in our assets folder so drag and
drop the assets folder into the file
system
it's going to import all the assets
then let's create our world
so in our scene create a new 3D scene
and call this
world
in here I'm gonna right click the root
node click add shot node
and add a node to 3D
this is going to be our map
in here I'm going to add a box
search for books and add the CSG box
we're going to be using CSG shapes
because they have easy Collision I'll
show you in a second
click create
and this is going to be our floor
so I'm going to set the size to 1000
meters by one meter
by 1000 meters here in the top right
and I'm gonna move it down by grabbing
the stop handle holding down shift and
control
and so it's on the world's origin so you
can see the grid lines
all right
let's apply texture to this
so I'm gonna go into my assets folder
textures grids
I'm going to be using a dark grid for
the floor and I'm going to be using grid
5.
so you can drag and drop it onto our box
you can see it's too big
we can change this by going into
geometry on the right When selecting
books
clicking on the material
going down to UV and enabling triplaner
this is going to scale the texture to
the size of the box so now you can see
every square is one meter by one meter
I'm going to rename this one too
floor
and I'm also going to add a couple of
boxes for a reference so on our map
right click a child node
another box
again holding down Ctrl and shift and
grabbing the top handle
drag it up so it's on the floor
let's make this one uh Orange Box
I'm going to be using orange 9 here
drop it on there
oh grab it and let's move it up to the
side a little
right there rotate it
these are just for reference
so the scene looks a little more
interesting
let's click this box and hit Ctrl D to
duplicate it
and let's grab this little square right
here
move it over
let's make this one a little bigger two
by two by two
hold down control and shift and drag it
up
let's set a different material for this
one so in Geometry material overrides
click the little refresh icon
let's make this one purple
I'll use purple 9.
I am going to be enabling triplanar for
this one
so click on the material again uv1
enable triplanar
and let's do one more box so click on
the box Ctrl D to duplicate
let's move it over here rotate it a
little again
go into geometry
refresh the material
let's make this one green
nine
drop it onto the Box
click the material
UV enable triplaner
and I'll make this one tree or tree a
tree
Ctrl shift drag it up
so now we got a couple of boxes so our
scene looks a little interesting
let's rename these
two Just Books
box and add two audience books
and then for all of these so click the
floor shift click the last box
enable collisions so our player can
collide with them
next I want to make the environment a
little more interesting
and an environment is basically the
lighting and the fog and the
post-processing effects you can really
easily add one by clicking these three
dots at the top
and click add environment to scene
we'll also add this sunlight
so click the three dots add sentosine
and now I'm going to click on my world
environment on top right environment
and here we can change some settings
I'm going to be enabling the new Global
illumination
cdfgi
enable that and you can see you got some
nice Shadows coming in I also enable
some fog so in the bottom click fog
enable
I'm going to turn down the density to
0.00
5
and we're going to change the light to
White
I'm actually going to turn on the
intensity a little more
it's like that
yeah it looks pretty good all right so
let's get into the player I'll first set
up some file organization so in our file
system right click new folder
call it scenes this will contain all our
scenes and create a new folder
scripts which will contain all our
Scripts
now we can save this world scene
controls
scenes and I'll call it Main
and in the scenes folder I'll create a
new
scene and call it
player
the root type of the player is going to
be a character body
so we'll click on this little icon next
to root type
a search for character body 3D
click ok
and it's directly giving us an error
that's because it needs a collision
shape so let's add this
right click the player notes add shot
notes
Collision shape 3D
and I'm going to be using a cylinder so
Collision shape in the top right in
shape
click
cylinder
and the default size is actually fine
it's two meters in height and half a
medium radius
and again we'll hold down Ctrl and shift
and drag up the Collision shape
is nice and On the Origin
I'm going to be adding an empty to the
player so right click player ADD child
node
no treaty
this is going to be our visuals
visuals and in here we're going to drop
the model so go into your assets folder
models and the mixable base
drop it into visuals
uh forward in Godot and many game
engines is z negative
so you can see here on the top you have
to see axis and the negative one is
going to be the forward so our player is
actually facing the wrong way now
so I'll just select the base
hold down control and rotate
180 bringing it up here in the bottom
left you could see the degrees
so that looks about right let's actually
scale down the Collision shape a little
bit I'll make it 0.3 and radius
and it's a little better and now let's
add our camera to this
for this we're going to need a camera
mount
I'll show you why in a second but for
now let's just add
a note 3D to player
call this
camera mount
I'm going to move this up this is going
to be the rotation point of the camera
so when you move around your mouse
on what points the camera is going to
rotate
I'm gonna put it right in her chest
yeah like that
and now add the
camera
so now you can decide how you want your
camera to look
you can click preview here in the top
left to see what it's going to see
so this one is going to be
too much in my opinion it's going to be
too much behind her so she's blocking
the scene
so I'm going to move it up a little
this one is kind of like uh World of
Warcraft like over the head a wide view
I'm gonna move it move it upside a
little
preview again
move it in
yeah I like it more like that like pubg
fortnite
over the shoulder
all right now if you grab this camera
mount and you rotate it and you rotated
the grounds
you can see what it means with the
rotation points
and up and down and we're going to want
to do this with our Mouse in game
so let's set this up
I'll quickly save this scene
into our player controller I'm gonna add
a script
drop it in our scripts folder
player is fine
and we want this template enabled the
basic movements
creates
and now let's save this the template
adds some basic logic for moving and
jumping which is really nice we can use
this right now it's using the arrow keys
but we're going to change that
I'm actually going to drop in the player
in our main scene
so in the tab here go back to main 3D
click the worlds right there and drag
and drop in our player
and now we can quickly test this
so you can click the play icon in the
top right
select current
and now when you move with the arrow
keys
you can see you can already move around
your player
and it collides with the boxes
so it's a very nice start let's set up
the logic
all right let's start with the mouse
movements so we'll go into our script
and do our player script
and we'll first capture the mouse it
means the mouse stays within the window
and doesn't move
and isn't shown
so we'll go funk
underscore already this function is run
when the player is ready so at the start
I will
input dots
Mouse equals input.mouse mode captured
and now I run this
you can see our Mouse is no longer
visible and it isn't moving outside the
screen
alt tab install this
and now let's get the mouse motion
Funk underscore inputs events
like this
and in here we're going to check if the
input is a mouse movement so if
event is
input events
Mouse motion
then we're gonna rotate our player on
the y-axis for that X motion
so we'll go
rotate underscore y
and we want these angles in radians and
not in degrees
so we'll go
degrees two radians with this function
and we'll get our
event
relative
so this is our Mouse movement on the
x-axis with this function we're going to
convert this from degrees to radians
rotate our player by those degrees
we're converting to radians because the
rotate function takes radians but if we
just put in one radian per pixel we're
going to spin out of control
so one degree is better and it's easier
to adjust for mouse sensitivity
so let's run this now
and now you can see
we can look around
it's too fast and it's inverted
so go back in here and we'll just
add a minus in front of this
like that and then we'll add the top
variable
sense
horizontal
equals 0.5
and then we can go inside of the degrees
to gradient function
multiply our X motion by sensor
horizontal
it's going to be remote sensitivity on
the horizontal axis
let's run it again
and that's the right way around and a
little slower
like that
now let's do our y-axis so up and down
I'm going to the player scene
and we're not going to re-rotate in the
whole player for the Y because then we
just clip into the floor
we're going to use this camera mounts
which we added
so let's go into our Scripts
click and drag the camera amount into
the code and holding down control
this way Godot automatically creates a
variable for the camera mounts
and we'll
go in here
and we'll
rotate X again degrees to radians
events dots
relative Wireless time because you want
up and down
and we're going to be wanting to rotate
the camera mounts so in front of this
we'll add camera mount Dot
let's run it
and now
we can look up and down let's inverted
again so I'm going to add a minus again
and I'm also going to create a
sensitivity variable
for the vertical
so I'll just copy and paste the
horizontal
everything is still
vertical
and again we'll multiply
the marsh movements by the sensitivity
and now
this is right I can move up and down
left and right
it's still way too strong
I'm actually gonna export these wires
and what that means is
that they'll show up as variables in the
editor so we can just adjust them way
easier
so in front of this I'm gonna go at
export
I'll do the same here
and now when we go into our main scene
in 3D we'll let the lighting update
you can see we have the variables here
so we can tweak them really easily
I'll set them a little lower
it feels way nicer
no let's change the arrow keys to the
wasd Keys
and here you can see the input direction
is coming from the UI left UI right UI
up UI down which are the arrow keys so
we'll add our own inputs
we do this by going into project project
settings input map
and here in add new we're going to type
the name of our inputs so we'll go
forward
backward
oh
left
and right and then by clicking the plus
and pressing the button we want so w
backwards D or s
left a right
d
close that out
and now in here we can swap these four
left
right
forwards
backwards
and now when we run this we can move
around our player
and look around
now of course she doesn't have any
animations yet
so we'll add those
two other animations go into visuals and
a maximum bass you're going to right
click it and make local this will expose
the animation player which we'll need
now we're going to drag and drop the
animation player into the codes to
access it again holding down control
now we have our animation player
and we're actually going to need to fix
these animations a little bit
so go into 3D animation player into
animations and here in the drop down you
can see all the animations on this
character
we're going to be going into locking
and we'll click this little Loop icon
that way at the end of the animation it
will play again
we'll also do this one for running
make that a loop
and for idle I already said it but yeah
all right
then in our scripts we'll set up the
logic for this animating
we'll copy the animation player variable
then here in the if Direction this means
if our player has a direction to walk so
in here the player is walking
I can go animation player does play
walking
now this is going to call the animation
player every frame to run the walking
animation which is not what we want
so we're going to check if
animation player build
is
sorry current animation
is not equal so exclamation point equals
is not equal to
walking
blade animation walking so if our player
is currently not walking will start
walking
and down here uh
well copy this logic so copy these two
lines of code and put them down here and
we're going to do the same for idling
so if we're currently not idle
play idle and this is logic for when a
player is not walking
all right let's run this
now you can see your idle animation is
playing
and when we walk our walking animation
is playing
you can see she's sliding across the
floor
this is because the movement speed is
set to high relative to the animation
so I'll tap stop this one
I'm going to set the speed to
2.8
let's run again
you can see it it's much better
it's actually a little slow now
maybe three
you're gonna have to tweak this
depending on your animations but if
you're using the same one
it's going to be somewhere around three
all right
a problem we're having now is when we
move sideways
our character doesn't look sideways so
let's fix this
to fix this we're gonna need our visual
note
so we'll drag and drop our visuals
holding down control at the top
and we'll copy this visuals variable
and here in our walking logic so if
Direction
we'll add this line
visuals but
look at
position plus
Direction now what does this do
this is going to rotate our visuals so
not our actual player but just the
visual elements so just a model
in the direction that we're walking
I'll show you what it does
so
now our character is actually looking at
the direction in which he's walking
but you can see this other bug
when we just move around our Mouse
the character does this little turn
thing and some games use this the
character turning with the mouse as
you're looking around I prefer
to not have it like this to have the
characters stay static until we move
I'll show you how to do that
we're actually gonna rotate our visuals
in the opposite direction of the camera
so it looks like it's static sounds
complicated it's not
we'll just go in here and input events
input event Mouse motion
we'll go visuals
dot rotate y
and we'll copy this
amount by which we're rotating the
player
I'll just remove the negative
let's run it again
now you can see our visuals are static
but as soon as we start walking the
visuals match with where I'm walking
and then again static
and while we're walking
they're bounds to the camera
so this is the most basic form of a
third person controller
if this is what what you were looking
for then there you go you can enter
right here I'm going to be adding some
more features to this character just to
show you I'll be adding running and
kicking animation
the running logic is actually really
easy we'll just create two new variables
for walking and running speed
so I'll go at the top of my code far
walking speed set is equal to 3.0
far
turning speed and set it's equal to
5.0
[Music]
then we'll quickly map out the shift key
in the input settings so go into
projects project settings input map
run
add an action for running click the plus
icon and press shift
Alright and then in our
physics process at the top
I'm going to be checking if we're
holding down the running action
so if
input with a capital Dot
is action pressed
run
so if we're holding the shift key
our speed is going to be equal to
running speeds and if we're not solving
the shift key
or
speed is going to be equal to walking
speeds
and now we'll have to change our speed
variable
sorry and now we have to change our
speed variable to a variable it's
currently a const
which means it can be changed it's just
a constant value
so we'll change it to far
and now if we run this
you'll see you can walk really fast
and all we have to do now is play the
right animation
so let's do that
in here in our Direction logic our Logic
for when we're walking
you can see
it's playing The Walking animation
we're just going to copy these two lines
below here
and swap walking forward running
at the top we're gonna have to check if
we're running or walking to decide what
animation we're going to run
and we could check the speed but it's
not a very nice way of doing it so just
set a new variable
far
running
equals false
it's going to be a Boolean
and we'll copy this in here in the logic
where we change the speed based only for
holding shift
you just say running if it's true
and running equals false
so then down here in the direction we
can check if
we're running
play The Walking animation we'll swap it
around
else better Running Animation but that's
backwards so I'll just copy this
paste in the top one
copy this
and paste it in the bottom one
so now let's run this
and now you can see we're walking when
we're walking
and we're running when we're running
you can see the animations start and
stop abruptly that's because there's no
blend time set
a blend time is basically the time it
takes for animations to transition
it's really easy to set up though so
just go into our animation player here
in the player scene
and down here in playback options
you can set the default blend time and
we'll set it to 0.2
seconds
and now if we run it again
you can see there's a little transition
between animations looks much smoother
alright I'll also add the kicking
animation I'll add this one because it's
a locked animation
meaning when we start kicking you can no
longer walk
and then when you end kicking you regain
control of the character
so let's create a variable for locking
our player
for
this
looks I'll say equal to false
copy this variable
and down here in move and slides
will go if
exclamation points is looks
so if we're not looks you can move the
character
I'll also not rotate the player so also
in here we go
exclamation points is locked
visuals to look at so it means if the
character is not booked so it's free
we'll look at their Direction and
otherwise uh
we don't so we're stuck
then let's add a key for kicking so
we're going to project project settings
input map
kick
it's edit
and I'll just use uh
F I can use whatever you want
close this out
and then we're gonna go into our process
gonna go if
input dot input is action just pressed
so just at the start of the press
kick
then we're going to check if we're not
already kicking so if
animation player does
current animation current animation is
not equal to
kick
animation player
dot play
kick
and we'll also
oh
this looks true
so we're checking if the kitkey is
pressed when it's pressed we're checking
if we're not already kicking and if
we're not we'll kick and we'll look our
character so we can no longer walk and
look around uh we can look around but we
can no longer walk and rotate the
character
now let's run this and there's going to
be a bug but I'll show you
so
kick
a character it locks in you can see a
little bit of the kicking animation
playing
but then it goes back to idle and it
never unlocks
so let me show you why
it's because we set the locked we never
unset locked so we can stay locked
forever
and we play the kicking animation but
in here this Logic for not moving is
going off and we're playing idle
animation
so how do we fix this it's actually
quite easy
we're gonna have to stop running
animations while the character is locked
because right now even though the
characters looked and playing the
kicking animation it's going back to
idle or running depending if you're
holding shift
so we're going to add this condition
the characters not looks to the
animation
[Music]
logic also down here
and here you can see duplicate code in
the same space so we can just remove
this condition and move them into the
same block
so you can see if looked now
contains the visual lock and the
animation lock
let's run it again
you can run a rock around and one kick
we kick and after that we can do nothing
because we're never unlocked
and to fix that we're going to go here
at the top of this physics process
gonna go if animation player is playing
is playing
we're gonna add an explanation point so
if the animation player is not playing
so we're done with clicking or any other
animation we're locked into
x equals false
meaning we are now no longer looked
so I can walk around
kick
and after that we're unlocked again so
you can walk up to someone kick
and during the kick we cannot
move our players slide around
you can just kick these boxes
so that's our basic third person
controller setup
and there's a lot more you can do with
this I have a project where I build a
shooter with this I'll show some footage
now
and of course there's a million things
you can do with a third person
controller if you want to see something
more specific specific feature or
specific game type using a third person
controller or any other feature you
might want to add let me know I'm always
making tutorials so I'll be glad to do
one so yeah I hope you learned something
if you make something awesome link me
I'd love to see it
alright thank you