Unreal Engine 5 Stamina System Tutorial [2023]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this Unreal Engine 5 stamina system tutorial we are going to create a stamina bar for the player the player will spend stamina when sprinting and it will automatically regenerate when the player is not sprinting anymore the player will also stop sprinting if the stamina bar reaches zero in this tutorial you can use your own project to implement this stamina bar however I am going to implement it in this third person template so I'm going to go ahead and select this and give it a name and remember to have blueprint set as your programming language and go ahead and click on create instead of here the first thing I'm going to do is click on the content drawer and click on dock in layout so this content browser stays up all the time if you're interested in Unreal Engine 5 game development courses you can visit my website pixelhelmet.com here I make both single player and multiplayer courses with videos being released daily in 4K resolution and I'm available 24 7 on my website or Discord server to help you out all the links are in the description below now before we do anything let's create the UI and get that out of the way so I'm going to right click here make a new folder all this one UI and inside of here let's right click go to user interface and create a widget blueprint I also like this one here and let's call this one widget blueprint and I'm just going to call it main as my main UI I'm going to go ahead and open it up and inside of this user interface up here before you do anything you have to create a canvas so you can click on this canvas click and drag it into the hierarchy here and you can see we have created this canvas so we have to do this before we do anything else now as for the stamina bar you can write progression bar or progress bar this year clicking and dragging this into the canvas panel and here we have the progress bar now you can either click and drag it into here or you can click and drag it into the canvas panel that is up to you you can see that's the same thing now as for my stamina bar you can see you can click and drag these sides to make it larger you can also do that here to the side where you can see the size of it size X and size y so I'm just going to just do something like this very very random for now and I'm going to place it down here so I just want my my stamina bar to the to be at the bottom left of my screen now you can see we have an anchor Point here so what this means is if I make my screen smaller and smaller the stamina bar is actually going to be pushed towards that end of the screen and I don't want that I want my stamina bar to be down here so what you can do is click on the stamina bar or progress bar here and you can click on the anchors up here and select this bottom left anchor now you can see when my screen gets smaller just like that if the player in the game is going to minimize their screen like this it's actually going towards this side of the screen and this is what I want okay so over here in the position X and Y if you write 0 0 you can see that it is out of the screen now now in order to make it inside of the screen you have to push it up in the Y and you can do this here in the alignment in the Y if you just write one you can see you can either write 0 0.5 or 1 and if you write 1 it should be at 0 0 it should be inside of the screen so now you can see it's inside of the screen everything is correct I'm just going to click and drag this progress bar a bit up here and you can resize it as you like and what you can do with this progress bar you can see in the percent if you click and drag and write a number you can see you can fill this progress bar now I'm going to change the color clicking on this color down here I'm just going to change it into a yellow color when you are satisfied you can go ahead and click on OK and here we have the UI for the progress bar I'm going to change the name of it here so clicking on the progress bar and calling it prog stamina and we can now use it here in the graph if you go to the graph this is where you code the functionality of the progress bar and here you can see it is called prog stamina and it's a lot easier to work with if you change the name up here because then you know what the variables are called when you work with them inside of the event graph so inside of here let's go ahead and delete all of those so click and drag and select all of them hit delete on the keyboard and I'm going to right click write custom events and now I'm going to create a custom event called update stamina UI something like that whatever makes sense for you update stamina UI and the only thing we want to do with this progress bar is we can click and drag it into here and we can drag from this and say set percent and select this function called set percent and the reason why we do this is remember in the designer if you click on the progress bar you are setting the percent here we want to either increase or decrease the stamina bar so I'm going to go in the graph and take this and say set percent and go ahead and connect with the event you just made and from this one this input here you can click and drag it into the event and it will automatically create an input for you so you can either create inputs by clicking up here and changing the variable and creating it or you can automatically create them if you drag one of the pins into the event so for this one I'm going to call it current stamina now remember we need a percent so if you write for example if your stamina is let's say 90 or 80 this is going to not show correctly because this bar is only going from zero to one it's not going from 0 to 100 so if you have a value of 80 it will keep showing full if your value is 40 it's still going to show full because the full value is 1.0 so you have to make it as a percent from 0 to 1. so what we can do here is we have the current stamina let's say the current stamina is 50 right now we have to divide it by something to get a number that is between 0 and 1. and let's click here and make a new float up here select float and for this one let's call it Max stamina and let's take this minimum or current stamina take it and write this symbol here this will divide or we can just simply write divide and select this operator and now you have to divide this number by this number so let's say my current stamina is 50 and I divided by 100 which is my maximum stamina so 50 divided by 100 that is equal to 0.5 so it will show correctly as 0.5 just like that okay so this is uh this would work correctly Max a current stamina divided by the maximum stamina that you have and this one we will drag into the percentage here and you can go ahead and organize your code all right now we are finished here let's close down this UI and let's go over to third person go to Blueprints and open up my third person character now inside of this third person character blueprint we have this begin play event and this will tell us what should happen if I begin playing the game and here we are going to add the UI so if you have your own project you can go ahead and add it to your begin play event from here I'm just going to drag after this code that they have written I'm going to drag from here and say create widget and if you just search for widget this one create widget and we're going to select the widget we just made called WB main now this one is the player controller reference so you can drag from here and write layer controller and select this one called get player controller now that this is done what we have to do is now a direct from this and say add to viewport because even though you create the UI you have to add it to the viewport in order for it to be displayed to the screen so let's go ahead and click on compile up here and let me minimize this and click on play to see if it works and you can see it works if I click on f11 to maximize my viewport you can see we now have this stamina bar however we are not we don't have any functionality to it so it's not going to work just yet but the UI is working so let's go back to the third person character okay so now let's make a button where we Sprint let's minimize this here inside of this input folder they already have made this input mapping context and also so some input actions as you can see here I'm going to right click and create a new input input action by the way if you're new to this system here please take a look at my YouTube channel I do have a in-depth video explaining how to do this and again you can always visit pixelhelmet.com or my Discord server and I'll help you out so for this print let's call this input action IA Sprint hit enter and let's go back to the input click on the input mapping context and as you can see here they've already made the jump move and look for you I'm going to click up here and select the Sprint that we made down here and as for the buttons I'm just going to click here and click on shift on my keyboard or you can click here and find it under keyboard find the key whatever you like to Sprint with I'm just going to Sprint with my shift key and as the triggers I'm going to click up here and select the pressed and click up here again and select the released so this just tells you when should this Sprint be triggered whenever I press and whenever I release I want to stop sprinting and we will use that inside of the code so go ahead and save this and let's go back to the third person character inside of here let's right click and write I a Sprint now you have to call it whatever you called your input action so the name of the input action it will show as an event here and this icon means that this is an event clicking on it here and now you can see we have the input action just like the other ones we have our own now with this print now before I do anything let's drag from this Boolean and write branch and then connect it to triggered now what this is is if I click on my shift button to Sprint it's going to play the code here from the true if I release my shift button to stop sprinting it's going to play the code here from the false now with further sprinting what we're actually wanting to do is we want to take this character movement component drop it here drag from this character movement component you can see in the character movement component you have something called the max walk speed and the max walk speed is simple is simply how fast you are running so dragging from this and say set max walk speed this is what we're basically wanting to change when you're sprinting you're basically running faster and when you're not sprinting anymore you're walking normally so let's connect it here when we are sprinting we want to put a value that is very high for example I'm just going to put the value of 2000 you can see normally the value is 500 when I'm walking normally whenever I Sprint I'm just going to put it very very high to Showcase it easier to you and as many of you know I don't like to write static numbers I like to have them as variables so I'm going to right click this here and promote this to a variable I'm going to call it Sprint speed and now we have it as a variable so when instead of changing the number here we can always just click on the variable down here and change the default value of it now as for the running speed here now when when it is false meaning we have released our shift key we want to run normally again I'm going to click here copy and paste it so Ctrl C Ctrl V to paste it and then I'm going to connect it here and instead of Sprint speed let's right click here promote this to a variable and call it run speed instead and for the Run speed remember you have to it says please compile the blueprint you have to compile to see the default value so clicking on compile and for the Run speed remember to set it to 500 or whatever it is for your own game here for this game the character movement default is 500 so I'm going to choose that so now if you play the game you would see that it is actually working if you play here and you press on shift you can see that I'm sprinting and if I release my shift key you can see I'm walking normally again so Sprint watching and walking normally so everything is working correctly now we have to connect the stamina bar to the player so the most important thing here is something called a timer by event now you have to be a little bit awake because it's actually going not to get difficult but there are a lot of elements that we need to add for a stamina bar now let's drag from here and say timer by event so you have to use this function called set timer by event and this basically let us run code on a loop for example if we are sprinting we want to keep decreasing the stamina bar and if we are not running anymore we want to keep increasing the stamina bar because we are regenerating stamina so this is done by this one called sets a timer by event for this one from the event let's drag from here and write custom event and here we have to create a custom event and let's just call this one update stamina and it is a loop so I usually just call it loop at the end so this one updates the stamina when we are running and it will decrease the stamina and it is also a looping timer and here we have to select how how quick do we want to Loop this if I write 0.5 for example it will Loop two times a second if I write 0.2 this code will run five times per second and you can actually see that if I click here and write print string so I just print a text to the screen and you can see what is happening the file compile here and I click on play and I hold shift you can see my code is actually printing five times a second so if I click on Play Hold shift it's running five times a second so this timer here I'm just going to right click and promote it to a variable and I'm just going to call it stamina timer speed now we can use this variable later to to calculate the stamina and over here for this pin node I I'm going to right click and promote this to a variable and call it them as band timer handle now for this variable we need to have it as a variable because later we want to stop this timer whenever we have stopped sprinting so very very important that you make this into a variable now for the stamina here what we are basically wanting to do is we want to update the current stamina so let's make a new variable down here and let's create it into a float variable and let's call this one current stamina now we can hold alt and click and drag and place it that will set it instead of just clicking and clicking on set you can hold alt and drag it or hold Ctrl and drag it and it will get and set it automatically like this now we have to set this current stamina this is our ultimate goal so how do we set this current stamina so for the current stamina I'm going to click on this variable clicking on compile to see the default value value and let's say the current stamina is for example 100 currently so I'm going to take this current stamina and we can drag from here and say minus or subtract and select this subtract operator now we have to subtract this current stamina by something let us make a variable called stamina spend per second so you can choose how much stamina you want the player to spend per second when running now drag the stamina spent per second into the graph and you can drag from here and say multiply and I'm going to multiply it with the stamina timer speed so stamina timer speed is over here click and drag it and plug it into here and now you can plug this into the current stamina and subtract it now what you're basically saying here is the current stamina which is 100 and let us compile let's say the stamina spend per second is five so we are spending five stamina per second and in order to calculate this correctly because remember right now the stem a timer speed is 0.2 so this code here is running five times a second right now now if I just write stamina per stem span per second and connect it here without doing all of this you can see that it's actually reducing the stamina by five every 0.2 seconds so in order to tell it that I actually want to spend five five stamina per second and not five stamina per 0.2 seconds or whatever you have written up here I have to divide it by that speed that we write down here or up here and multiply it so for example if you say 5 is the stamina spent per second multiplied by 0.2 that will give you a value of one so we are spending one stamina per 0.2 seconds which will also make it to 5 stamina per second and now that we have this code we can go ahead and connect it here and it will update correctly now in order to make sure that this code doesn't go below zero because we keep subtracting the number and I don't want my stamina to go below zero because this is my minimum value so go ahead and direct from this and write clamp and select this one called clamp float so what a clamp is it just makes sure your number doesn't go under the value you have written here so my minimum stamina is zero and my maximum stamina is Let's Make a new variable called maximum stamina so go ahead and create new variable call This One Max stamina and go ahead and compile and now for this Max stamina I'm going to give it a value now you don't have to write 100 you can write 500 if you want the system we are making is dynamic and you can choose whatever it needs to be here in the maximum stamina it doesn't need to be 100 I'm going to write 100 for this tutorial now I can go ahead and click on compile and I can click and drag this maximum stamina into this value so now it's going to clamp my value my current stamina value is never going under 0 and it's also never going over the maximum allowed stamina go ahead and drag this now into here and now we have the code written correctly alright so now everything is working and if I drag from here and write print string and I take this and connect it into the the string here to print my value to the screen I can compile and click on play and hold shift and you can see the value is now updating correctly when I'm sprinting however we have now we now need to update the stamina bar instead of just printing to the screen so remember in the UI if I go back to the UI and if I go back to the graph we wrote this one in the beginning called update stamina UI I'm going to go back to my player here and now we actually need a variable for this this main widget that we created because remember this event is inside of the widget so we need a variable here so I'm going to drag this away a bit right click this create widget and promote this to a variable called WB main for example you can call it whatever you want I'm going to connect it here and I'm going to hold alt and click here to disconnect this and I'm just going to connect this to here to make it a bit more organized and now we have this variable go ahead and click on compile now down here what you can do is you can take this variable and write the name of the event that you created update stamina UI and right from here update stamina UI and as you can see here it's going to take two inputs because this is what we made here it's going to take the current stamina and also the maximum stamina and we have that as well drag it into here now we have the current stamina maximum stamina and go ahead and click on compile and now if you click on play and hold shift to Sprint you can now see if I maximize the screen so you can see it's spending five stamina per second so now it is working however you can see it's not stopping whenever I stop so we have to continue writing the code okay so down here whenever I don't press shift anymore whenever I don't Sprint anymore actually I'm just going to push all of this code a bit away from here so you can see what's going on I'm going to take this down here because we have to write code here as well whenever I stop sprinting now I'm going to set my run speed to normal but I also want to stop this code here I don't want to spend stamina anymore so again this was very very important that you made this variable so you're going to use this variable now take this variable that we created the timer handle yet and we can drag from here and say clear and invalidate timer by handle this is how you stop the timer from Counting anymore and now we want to use another timer because now we want to regenerate the stamina so drag from here again and write set timer by event just like before nothing new and let's drag from here and create another event just like before custom event and let's call this one update stamina regeneration so now we're regenerating it and I'm just going to call it Loop because it is looping now just like before I'm going to set it to looping we can just use the same variable up here so we don't have to write it here so I'm just going to take this time stamina timer speed and plug it into here so now instead of spending stamina we want to regenerate stamina and again we're just going to set this current stamina so taking this current stamina and let's set it again just like before so we're going to again take this current stamina and instead instead of saying minus let's say plus let's drag from here and say plus because now we want to regenerate stamina and just like before here let's copy this and paste it down here so we have stamina spent per second now let's delete this one and promote this to a variable let's call this one stamina region per second now we're not spending we're regenerating and go ahead and compile and now we have to write how much stamina do you want to regenerate per second you can see you don't have to spend the same number as what you're regenerating so you can even make the players spend stamina faster than what you're regenerating so for this regenerating I'm just going to write 5 is fine and the stamina timer speed is just like before 0.2 and you can go ahead and add it to this to this calculation and we can add this to the current stamina so now we are regenerating stamina we are adding into the current stamina and just like before I don't want my stamina to go over the maximum stamina value so I'm going to drag from here and right clamp select float the minimum value is 0 and the maximum value is this maximum stamina and you can drag from this into here and organize the code and just like before you have to update the stamina UI so I'm just going to copy this into here just copy paste and connect your current stamina just like this and now if I compile and I go ahead and click on play I can see if I hold shift I'm spending stamina if I let go of my shift I am actually regenerating so holding shift will spend releasing will will regenerate however you can see there is a bug we have remember you are running this timer however you you have not stopped it yet just like up here so let's right click this one and promote this to a variable let's call it stamina region timer handle and let's go ahead and put it here and now up here whenever I I hold the shift here and spend my stamina I want to stop the Regeneration because it doesn't make sense to regenerate stamina whenever you are sprinting and I also want to make sure that I can't Sprint when my stamina is zero because you shouldn't Sprint when your stamina is zero so let me take my current stamina I can drag from this current stamina and say not equal so this sign here is not equal or you can just write not equal so if my current stamina is not equal to zero and I can write Branch if that is true I want to Sprint so my current stamina is not zero I can go ahead and Sprint now as for this timer we also have to stop this stamina regeneration if we are regenerating I'm going to drag this a bit away here take this stamina region timer handle and say clear and invalidate just like before go ahead and connect it and go ahead and take this code a bit away for it to stay organized and here we have it now if we click on play we can now hold shift and it will spend and if we will release it will regenerate if I hold shift again it will spend and so on so let's create this here for now now we are missing something if you are sprinting and your stamina gets to zero you don't want to Sprint anymore you want to stop the player so check this current stamina and remember right now we are up here where we are spending the stamina so take the current stamina here right from here and right equal select the sequel and now when the stamina is equal to zero click here and write the branch and connect it here so when the stamina is zero we basically want to take this stamina spend time and again clear and invalidate this timer because we don't want to run this code anymore and we want to start automatically regenerating stamina because now you're not running anymore so to automatically start this code here let's make a new custom event let's right click make a custom event and call this one Regen stamina just like that and go ahead and just connect it to this Max work speed because now we can run this code here we can run all of this code before the player actually releases the button because we're just forcing the player to walk because you basically don't have stamina anymore now region stamina so up here we can now call this event we just made called regen stamina so we're going to stop the spent code here and we're going to run this automatically down here a regen stamina the same thing here if your stamina is regenerating and it is at the maximum stamina so if you have regenerated all of your stamina we don't want to keep running this code this will be unoptimized and your gamer will crash at some point because you're just running code without any reason so let's take the current stamina and let's say if the current stamina is equal to the maximum stamina then we want to stop regenerating because we're basically at the maximum stamina now so dragon from this writing a branch again connecting it and here we want to take the stamina region timer handle and say clear and invalidate timer by handle now before we finish up here I want to make sure that the player is not spamming this timer here because the player can basically hold shift to Sprint and release the shift key like hold shift release the shift like 3 times a second and the player will keep running this timer without any reason before we stop it I'm going to drag from this and take this stamina region timer handle and I'm going to drag here and say is valid timer handle I'm just going to make sure that this is valid or not you can drag from here and write not B this will take you to this one not Boolean and go ahead and create this one and now we can drag from here and write a branch and connect it to here so we're basically saying if this stamina region timer over here this code here if it is not valid and you want to actually regenerate the stamina go ahead and create this timer go ahead and continue the code however if you're already regenerating so we have not cleared this timer you're actually already regenerating we don't want to do anything because you're already running this code here we don't want the player to keep spamming it without any reason and here you have it now if I compile and I go ahead and click on play and I hold f11 and I hold shift we are sprinting if I release the shift we are not sprinting anymore and we are regenerating stamina and if it reaches 100 it will stop regenerating now you can see to make the bar more smooth you can see it's not it looks laggy when it is spending stamina this is because of the timer at the time you have chosen it to be so here's the stamina timer speed instead of writing 0.2 you can write 0.05 and it will automatically update down here so go ahead and compile and if you click on play now and hold shift you can now see it is more smooth and it is spending in a smooth way now just be aware optimization wise you don't want to run this too many times so if you don't want to write for example 0.001 basically you are running this code x amount of times and if you are running this code too many times a second this will uh basically use the optimization of the game and make your game like so I usually just like to stay up here around 0.3 which is the minimum or 0.2 I don't like to go below that so 0.5 it's very nice if you can go higher that will even make your game more optimized so go ahead and compile and thank you for watching this video again you are very welcome to join me on pixelhelmet.com I have a lot of game development courses for shooting games or Platformers and whatever you want even 2D games in Unreal Engine 5 or you can join my Discord server and I will also help you out there remember to like And subscribe and I hope to see you soon
Info
Channel: Pixel Helmet
Views: 12,421
Rating: undefined out of 5
Keywords: unreal engine 5 stamina, unreal engine 5 stamina system, unreal engine 5 stamina bar, ue5 stamina system, ue5 stamina, unreal engine 5 stamina tutorial, ue5 stamina bar, ue5 sprint stamina, unreal stamina bar, ue5 sprinting, unreal engine stamina bar, unreal engine stamina system, unreal engine 5 sprint, unreal engine stamina tutorial, unreal engine stamina, ue4 stamina system, ue4 sprinting tutorial, ue4 stamina, unreal engine 4 stamina system, unreal engine 4 stamina
Id: ZVeDKJlXqpY
Channel Id: undefined
Length: 30min 17sec (1817 seconds)
Published: Sun Apr 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.