00:00:00
what's up guys mizo frizzo from
00:00:01
Pitchfork Academy here and in this
00:00:03
Unreal Engine 5 tutorial I'm going to
00:00:06
show you how to set up a basic stamina
00:00:10
system with a progress bar so as you can
00:00:13
see here I am walking and if I hold the
00:00:16
left shift I can run and it will use up
00:00:18
my stamina and just to demonstrate I've
00:00:22
got two kinds of progress bars here I've
00:00:24
got the standard horizontal one and I've
00:00:27
got the radial one which you can can
00:00:30
make in another one of my tutorials so
00:00:32
I'll be showing you how to set that one
00:00:34
up as well but basically my stamina will
00:00:37
replenish if I'm not sprinting if I
00:00:39
Sprint it will use it up and if I stop
00:00:42
sprinting it will pause for 1 second
00:00:44
before it starts replenishing and if it
00:00:46
runs out all the way to zero it will be
00:00:49
empty for 2.5 seconds before it starts
00:00:52
replenishing and I will not be able to
00:00:55
sprint in that time until I get some
00:00:57
stamina back so without further ado guys
00:01:00
let me show you how to do this all
00:01:02
righty guys now all I've done is create
00:01:04
a new project using the third person
00:01:06
template and the first thing I'm going
00:01:08
to do is just set up my character's
00:01:11
ability to Sprint so I'm going to open
00:01:13
my BP third person character find some
00:01:15
empty space and I'm going to right click
00:01:17
and find left shift and on pressing of
00:01:21
the left shift the first thing I'm going
00:01:23
to do is set a Boolean uh to know if I'm
00:01:26
running I'm going to call it is running
00:01:29
question mark
00:01:30
and I am going to set this as true on
00:01:35
pressed and as false on released like so
00:01:41
uh the next thing I'm going to do is
00:01:43
change the walk speed of my character
00:01:45
inside of the character movement but as
00:01:47
you can see if you select the character
00:01:49
movement the default Max walk speed is
00:01:51
500 and just to accentuate this I'm
00:01:54
going to change this to
00:01:56
250 and then I'm going to set it to
00:01:59
something like 800 so I'm going to find
00:02:02
set max walk speed I'm going to
00:02:05
duplicate this I'm going to set it to
00:02:07
800 on pressed and on released I'm going
00:02:10
to set it back to
00:02:13
250 make sure that you connect the
00:02:16
character movement component to the
00:02:18
Target
00:02:20
here nice and that should be working so
00:02:25
I'll just double check that if I walk
00:02:28
I've got 250 and if I hold left shift
00:02:31
I've got that 80000 Max walk speed so I
00:02:33
can Sprint very nice okay uh now the
00:02:38
next thing I'm going to add before we
00:02:40
add in the stamina is just to check uh
00:02:44
if we can run uh because we will want to
00:02:47
set this to false to stop our player
00:02:49
from running when for example the
00:02:52
stamina reaches zero and it takes a
00:02:54
couple of seconds to replenish so I'm
00:02:56
going to add a variable I'm going to
00:02:58
call it can run question
00:03:01
mark and I'm going to compile and I'm
00:03:04
going to set the default value of can
00:03:06
run to True and then I'm going to get
00:03:08
can run hold B and click to get a branch
00:03:12
put this on a branch right here and now
00:03:14
we will only run if we indeed can run
00:03:18
the next thing I want to do is start
00:03:20
setting up our stamina consumption so
00:03:23
I'm going to right click down here add a
00:03:25
custom event and call it
00:03:28
consume stem
00:03:31
and I'm going to call this custom event
00:03:33
right here every time we run uh we are
00:03:35
going to call consume
00:03:39
stamina and all I'm going to do uh to
00:03:42
get this started is create a new
00:03:45
variable and call it stamina it's going
00:03:48
to be a float and I'm going to compile
00:03:51
and I'm going to set the default value
00:03:53
of stamina 2 1 now you may want to set
00:03:58
this as 100
00:04:00
but uh progress bars typically use a
00:04:03
value between 0 and 1 Z being empty and
00:04:06
one being full so I'm actually just
00:04:09
going to make uh my full amount of
00:04:12
stamina equal 1 so I'm basically going
00:04:15
to divide everything by 100 uh so that I
00:04:18
don't have to actually do that uh you
00:04:21
know with nodes I don't have to divide
00:04:23
everything by 100 to set the progress
00:04:25
bar so I'm going to set stamina here and
00:04:29
I'm going to set stamina as the
00:04:33
stamina
00:04:34
minus something like
00:04:37
0.025 so 22% of my stamina every time
00:04:41
this is called and then I am going to
00:04:44
check if I am running so I am going to
00:04:49
grab my is running Boolean hold B and
00:04:52
click to get a branch and if we are
00:04:55
running we are essentially going to uh
00:04:58
continue cons assuming stamina um and I
00:05:03
will put a print string here actually I
00:05:05
will just put it before the branch just
00:05:08
put a print string
00:05:10
here and plug in my
00:05:13
stamina and then if we are still running
00:05:17
what I'm going to do is get a
00:05:19
retriggerable delay and I'm going to set
00:05:21
this to
00:05:24
0.05 and then I am going to call assume
00:05:28
stamina
00:05:30
again so when we start running it's
00:05:33
going to call this event consume stamina
00:05:36
it's going to consume some stamina and
00:05:39
then it's going to check if we're still
00:05:40
running and if we are still running it
00:05:42
is going to delay and call itself again
00:05:47
so this will keep running over and over
00:05:49
again so if we press play now and I hold
00:05:52
shift to run you'll see uh that it is
00:05:55
consuming stamina and one of the things
00:05:58
you'll noticed is that it is going into
00:06:00
the negative there so we can very easily
00:06:03
fix that by putting a clamp right here
00:06:07
clamp float
00:06:09
whoops clamp float and very conveniently
00:06:13
the Min and Max is already zero and one
00:06:17
so we don't need to change anything
00:06:18
there nice um now we want to start
00:06:24
adding some other stuff to the
00:06:26
consumption of this stamina such as as
00:06:30
uh if the stamina reaches zero uh we
00:06:32
want to stop our player from sprinting
00:06:36
so what we can do is uh just disconnect
00:06:41
this right here and move this out a
00:06:43
little bit and we are going to check if
00:06:46
our stamina is
00:06:49
equal to zero and put another Branch
00:06:56
here and if the stamina is equal to zero
00:07:00
the first thing that we want to do is
00:07:03
set this is running uh so we can just
00:07:07
grab these nodes right here that is
00:07:09
running and the set back to the walk
00:07:11
speed right here so if it equals zero
00:07:14
it's going to set is running to false
00:07:16
and it's going to stop us from running
00:07:19
like so and
00:07:22
um we will create our stamina
00:07:27
replenishment in a moment um but what
00:07:30
we'll do is just grab these two nodes
00:07:32
here and plug these into the false so if
00:07:34
our stamina is not equal to zero it will
00:07:38
delay and continue consuming stamina so
00:07:43
we can see this at work now if I run and
00:07:47
I let go before it's zero I should still
00:07:49
be able to run and when it gets to zero
00:07:52
it is stopping me from running nice and
00:07:54
if I try to run now it just says my
00:07:56
stamina is zero so that's all working
00:07:58
correctly but we just need the ability
00:08:00
to replenish our stamina so what we can
00:08:04
do is we can create another custom event
00:08:06
and we'll call it
00:08:08
replenish
00:08:11
stamina let double check that I spelled
00:08:13
that correctly and I have not replenish
00:08:16
stamina and uh what we're going to do
00:08:20
with this is we're going to call it on a
00:08:24
timer by event and this will allow us to
00:08:27
sort of um have a bit more control over
00:08:32
when this is called and for example when
00:08:35
we run out of stamina or we stop
00:08:39
sprinting uh we can add a little pause
00:08:41
in there so that's kind of why I have
00:08:44
this is running Branch right here
00:08:47
because this will keep calling itself
00:08:49
over and over again but if we release
00:08:52
the left shift button and we are no
00:08:56
longer running what we can do is set
00:08:59
timer by
00:09:01
event the event is going to be this
00:09:04
replenish stamina right here and this
00:09:06
will be the uh amount of time it will
00:09:10
pause for before replenishing your
00:09:13
stamina if you stop running so I'm going
00:09:16
to put this as 1 second and the first
00:09:19
thing I'm going to do when we call this
00:09:21
replenish stamina is check if we are
00:09:25
totally out of stamina so I can grab
00:09:27
this stamina equals z and Branch right
00:09:30
here and duplicate it down here and if
00:09:34
we are indeed out of stamina what we're
00:09:37
going to do is set can run to
00:09:40
false and then we are going to put in a
00:09:46
delay and I'm going to make the
00:09:50
delay 2.5 seconds decent delay um and
00:09:55
then what we will do is reduce our
00:09:58
stamina so so we can get our stamina and
00:10:02
we can sorry we can replenish our
00:10:04
stamina increase our stamina not reduce
00:10:07
so I'm going to make this 0.01 cuz I
00:10:10
want it to replenish quite slowly and we
00:10:13
can grab a clamp float duplicate this
00:10:17
down
00:10:18
here and set our stamina as the output
00:10:23
of that clamp so after we've stopped our
00:10:27
player from sprinting we are delaying by
00:10:29
2.5 seconds before we start to replenish
00:10:32
our stamina and then we can give them
00:10:35
the ability to Sprint back so we can
00:10:39
change can run to True and then what we
00:10:43
want is a retriggerable delay
00:10:47
retriggerable delay and this is going to
00:10:51
be our very small number
00:10:53
0.05 and we're going to call replenish
00:10:57
stamina again
00:11:00
so if this is called and we are out of
00:11:04
stamina we're stopping the player from
00:11:06
sprinting for 2.5 seconds and then we're
00:11:10
starting to increase the stamina give
00:11:12
them the ability to run again and
00:11:14
calling itself again so the second time
00:11:18
this will call it will not be zero
00:11:21
anymore because we've added 0.1 to it oh
00:11:24
sorry this should be 01 uh my mistake
00:11:28
and uh and so here we can basically grab
00:11:32
these nodes right here and duplicate
00:11:35
them so if we do have some stamina we're
00:11:39
going to continue increasing it and we
00:11:42
can grab this retriggerable delay right
00:11:47
here and then we want to check um if
00:11:50
it's full and if we're running basically
00:11:53
so what we can do is drag off of this
00:11:55
pin right here and get a less node and
00:11:59
and check that it is less than one so
00:12:02
less than full if it's full we don't
00:12:04
want to continue replenishing it and I'm
00:12:08
going to also get an and Boolean here
00:12:12
and I'm going to also get a not
00:12:16
Boolean and check that we are not
00:12:19
running so plug is running into here
00:12:24
so if the stamina is not full and we're
00:12:27
not running then we will will continue
00:12:30
to replenish our stamina so put a branch
00:12:33
on this and we already have our delay
00:12:37
here and we can get our call to our
00:12:41
replenish stamina like
00:12:44
so nice and if I get this print string
00:12:48
and I put it down here uh in front
00:12:54
of wherever we set the stamina
00:13:00
like
00:13:01
so just before we set up our progress
00:13:03
bar we can see this at
00:13:06
work and this should all be working
00:13:09
correctly now so if I compile and hit
00:13:11
play and I start running see we've got
00:13:14
our
00:13:15
stamina it is pausing for 1 second if I
00:13:20
stop running and filling back up
00:13:23
again and if I use it all the way to
00:13:26
zero I am unable to Sprint until it
00:13:30
starts increasing
00:13:32
again which it is not it is not
00:13:36
increasing again so something is up here
00:13:38
ah I uh I didn't plug this execution pin
00:13:42
back in when I put the print string in
00:13:44
there I forgot to put a call to the uh
00:13:47
replenish stamina on the end of my
00:13:50
consume stamina here so when we run out
00:13:52
of stamina completely um I've just
00:13:55
forgot to put in a call to our replenish
00:13:59
stamina that's all that was there sorry
00:14:02
about that guys so now when it gets to
00:14:05
zero it should pause for 2.5 seconds and
00:14:08
then start increasing again and then I
00:14:10
can indeed run again and if I try to run
00:14:14
well it's at zero it won't let me until
00:14:17
it replenishes and I actually have some
00:14:20
stamina to run nice so uh this uh looks
00:14:25
a bit crappy with the print string so
00:14:28
let's set up a progress bar very very
00:14:30
quickly so I'm going to delete these
00:14:33
print strings
00:14:35
here and I'm going to create a widget
00:14:38
very very quickly to display this
00:14:41
stamina on screen so let's just in our
00:14:46
third person blueprints folder right
00:14:47
click and go to user interface widget
00:14:49
blueprint user widget and I'm going to
00:14:51
call this wbp
00:14:53
uncore stamina and open this up I am
00:14:58
going to add a a canvas
00:15:01
panel and I'm also going to add a
00:15:04
progress bar to that canvas panel I'm
00:15:07
going to leave it anchored to the top
00:15:08
left here but because I'm a Dark Souls
00:15:11
fan I'm going to make this long and
00:15:13
skinny like so I'm going to change the
00:15:17
color to
00:15:20
green and I'm going to change the
00:15:23
default percentage to one so it is full
00:15:27
by default and then I can click bind
00:15:29
create binding and in here I am going to
00:15:33
cast to my third person
00:15:37
character and get player
00:15:42
porn and
00:15:44
then get my stamina from my
00:15:50
porn and I just need to add this widget
00:15:53
to the viewport so in my third person
00:15:56
character I'm just going to
00:16:00
do this on event begin play right here I
00:16:02
am going to create
00:16:05
Widget the widget I'm going to create is
00:16:07
wbp stamina and then I will add to
00:16:16
viewport and that's it that should all
00:16:18
be working so I'm going to compile and
00:16:20
save and hit play we see we've got this
00:16:23
stamina bar up the top left um I'm
00:16:26
losing a bit of FPS here
00:16:29
it's tanking um it'll probably just take
00:16:31
a moment to catch
00:16:33
up there we go but you can see my
00:16:36
stamina is filling when I stop
00:16:38
sprinting I can Sprint if I stop it's
00:16:41
going to pause for 1 second before it
00:16:43
starts to fill up and if it runs out to
00:16:45
zero it will pause for 2.5 seconds and
00:16:49
it will stop me from sprinting and then
00:16:52
refill
00:16:54
again nice just as a bonus guys I'm
00:16:57
going to set up my radial progress bar
00:17:00
and uh show you how to make this work
00:17:03
with my radial progress bar so that's it
00:17:05
for the stamina system and a basic
00:17:08
horizontal progress bar as you can see
00:17:10
here but uh if you'd like to follow
00:17:13
along with the next part you can check
00:17:14
out my how to create a radial progress
00:17:17
bar in Unreal Engine 5 tutorial um and
00:17:21
I'm going to set this up to work with
00:17:23
that progress bar now all righty guys so
00:17:26
I've added my radial progress bar to
00:17:28
this project
00:17:29
I have just deleted that other progress
00:17:31
bar from the widget and added my radial
00:17:33
progress bar and um you know this is set
00:17:37
up with everything from the tutorial
00:17:39
where we create it so in the graph it
00:17:43
has an uh update percentage function
00:17:46
just like we did in the other tutorial
00:17:48
so I've got this function where we
00:17:49
update the percentage of the radial
00:17:51
progress bar and to make this work all
00:17:54
I'm going to have to do is create a
00:17:58
reference to this widget that contains
00:18:00
the radial progress bar so I'm going to
00:18:03
promote this to a variable I'm going to
00:18:05
call it UI
00:18:08
Main and just connect this in here like
00:18:13
so and now that we have this UI main
00:18:16
anytime that we update the percentage
00:18:19
here we set the percentage we can just
00:18:21
get the UI main get the radial progress
00:18:25
bar inside of it and then call that
00:18:29
update percentage
00:18:30
function and pass
00:18:33
through the stamina like so and the
00:18:36
stamina just has to be a value of
00:18:38
between Zer and 1 so if for example you
00:18:40
set the stamina to 100 you would have to
00:18:44
uh divide it by 100 to get the correct
00:18:46
percentage for the progress bar I can
00:18:48
grab these three nodes here and I can
00:18:51
duplicate them and put them in front of
00:18:54
anywhere where we calculate the stamina
00:18:58
plug in the execution pins and plug the
00:19:01
stamina into the percentage like
00:19:05
so and that is literally all we have to
00:19:08
do to make this stamina system work with
00:19:10
my radial progress bar so if I hit play
00:19:13
you'll see we've got our radial progress
00:19:15
bar and once again my FPS is tanking I
00:19:19
don't really know why um I think it's
00:19:21
because I've got um multiple Unreal
00:19:24
Engine editors open right now uh there
00:19:27
we go it's caught up so you can see that
00:19:30
that is draining just like our other
00:19:32
progress bar if it runs out we are
00:19:34
unable to Sprint for 2.5 seconds nice
00:19:38
and then it is
00:19:40
replenishing and if we stop sprinting
00:19:42
it'll pause for 1 second and start to
00:19:45
replenish again nice that's it guys if
00:19:49
this tutorial has been of any use or
00:19:51
value to you whatsoever please hit like
00:19:54
And subscribe and I will see you on the
00:19:56
next one