Sprinting And Stamina - Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another horizon 5 tutorial in today's video we'll be covering some of the basics of gameplay once again and today that is going to be sprinting and stamina and we're also going to put the stamina on screen for the player to be able to see so let's hit play and i'll show you what we're going to make today so as you can see on screen in the bottom left we have our stamina bar if we walk around that's not going down but if i were to hold down left shift we can sprint so we are now actually sprinting and you can see our stamina is going down and if i to let go we'll stop sprinting start walking again and our stamina goes back up now if i were to continue sprinting until my stamina goes all the way down what you'll notice is it will automatically stop me from sprinting make me start walking again and our stamina will go all the way back up as you can see perfectly like so and this is very easy to customize so you can change how quickly the character sprints and walks and how quickly the stamina drains and regens so i've got mine to drain at a relatively fast pace it was regening a very fast pace so it's very easy to customize and change to work how you want but this is what we're going over and creating today so without further ado let me delete this code and i'll show you how i've done it so the first thing we want to do is we want to open up our character blueprint as that is where we're going to be doing this code so we're going to hit control space to open the content browser go to third person blueprints bp third person character now this can be in whatever character blueprint it is for you but for me i'm just using the default third person character once we're in here we want to go up to edit in the top left go to project settings and once this loads we're going to go down to input and create an action mapping here so we're going to hit plus next to the action mappings naming this one sprint and we're going to set this to be the key we want to use so for example i want to use left shift which is the most common sprinting button in games so i'm going to press left shift there and that's all we need to do in here you can set up multiple keys if you wanted so if you want to be left shift or control you can put both in here if that's what you wanted but i'm going to close this as that's all i want to do then i'm going to right click and search for what i just named it i need mine sprint and you can see we have action events sprint here so whenever the player presses the button you set up which goes to me is left shift it's going to fire off this event here so now we've enabled the player to actually press the sprint button we want to actually start sprinting so i'm going to be doing this via functions to make it nice and efficient and so we can also call these later on the code for the stamina so it can stop us from sprinting and start spreading automatically if we want so i'm going to hit the plus function in the left over here naming this one start sprinting and what i'm going to do in here is get a reference to the character movement so drag in holding control to get it out of this we're going to set max walk speed as the max walk speed is actually just the speed in which the player is moving at so that is how we can change them to be walking and sprinting so this value here is the speed which you want for sprinting i want it to be 600 for me however you can set this to be absolutely whatever you like the values i'm using here are also the values you saw at the beginning of the video so if you like those values you can use these but you might prefer different values for your own animations it might look a bit different obviously just mess about with it to see what works perfectly for you but after this we want to hit the plus variable in the bottom left down here naming this one is sprinting question mark leaving that as a boolean so it is a true or false value then hold down left alt and drag it in to set it and what we're going to do is we're going to tick it so it is true so the code now knows that we are sprinting and then i'm going to copy this so select it and hit ctrl c compile and save and we're going to come back to this later on but for now we're going to hit the plus function again naming this one stop sprinting so we've got start sprinting and stop sprinting then we're going to hit ctrl v to paste this in here like so and all we need to do is just change our max walk speed to now be our walking speed so for me i want it to be 150 and we're just going to untick is sprinting there so we're no longer sprinting so it's false so we'll compile and save that it's that simple now what you're going to want to make sure you do is select the character movement and in the top right search for max walk speed and you're going to make sure that your max walk speed by default is the walking speed which you set up so for example by default for me it's 600 but i want my walking speed to actually be 150 so i'm just going to change that over like so so it's now 150 centimeters a second by default and that's what it'd be when i set it back to walking like so so just make sure you've done that and we'll compile and save so now we're going to go back to the event graph and off of our sprint axe mapping we can now actually set up the sprinting so off of pressed i want to do start sprinting call function and at least it'll be call function stop sprinting and so what this is going to do now is the player has to hold down left shift in order to sprint when they hold it down they'll start sprinting when they let go they will stop sprinting now if you want this to be a toggle instead of hold that's very simple what you can do is just disconnect it from released and from pressed actually and then out of press we're going to get a flip-flop a going to start sprinting b going into stop sprinting so what this is going to do is when the player first presses left shift it will start sprinting when they let go nothing will happen but when they press it again if they're going to be so it will stop sprinting so that's how you toggle and what i had before is how you hold i prefer hold for sprinting so that's what i'm going to have it set up as so this is what i've got here and we'll compile and save that so now we're going to start doing the stamina code so underneath this we're going to come down right click and add a custom event naming this one drain stamina so we'll do the training first so since we're now doing the stamina we also need a stamina variable so in the bottom left we're gonna hit the plus variable once again naming this one as you may have guessed stamina then we're gonna change this from a boolean to be an integer you can use a float if you want to but i'm going to leave it as an integer just to keep it as a whole number we're going to compile and save that now hold ctrl and drag in the stamina variable to get it and out of this we're going to drag out and get a subtract node now i'm just going to leave this value as 1 so it's going to subtract by 1 each time you can increase this if you want but i'm just going to leave it as 1. and if you are just doing one you can just use a decrement instead like this which means you don't need to set afterwards however i'm doing a subtract node so if i want to change it later on i can very easily do that then out of this subtract node we're going to get a clamp integer with the minimum value being zero and maximum we're going to right click promote a variable naming this max stamina and then what the clamp is doing is it is preventing this value from going below the minimum and above the maximum now as we're draining here we're only caring about the minimum so essentially we can continue draining the stamina but it will not go below zero so let's say you have three stamina and you're subtracting by five each time we obviously don't want the stamina to go to minus two so what it's going to do instead is just cap out at zero so we'll continue going down until it reaches zero which is then perfect and what we want and after this all we want to do is now actually just set the stamina again so hold down alt drag in the stamina connect it into the custom event and also into the return value with the clamp so now we're actually setting the stamina as well so this is how we're going to be draining our stamina so that's perfect that's technically all we need to do however what you also want to do is stop the player sprinting automatically once they reach zero and we also just want to stop draining the stamina and we also want to loop this so it continually drains the stamina while the player is sprinting so out of the set stamina we're going to get an equal equal integer like so and we're going to leave this value as zero then hold down b left click to get a branch with that being the condition connecting this into the set there so now we're just seeing if the stamina is equal to zero or not if it is equal to zero so for true we're going to stop sprinting perfectly like so and if it's not equal to zero so false we want to continue draining the stamina if the player is sprinting so to check that we're going to hold down b left click to get another branch connecting that to false there the condition of this is going to be our ear sprinting boolean which we created earlier so if the player is sprinting i.e true we want to join the stamina if they're not sprinting ie false we're not going to do anything so out of true we're going to hold down d left click to get a delay like so and the duration of this is how quickly this is going to drain the stamina so i'm going to put mine at 0.1 and again that's the value i use at the beginning of the video once again obviously just mess about with this to get it perfect for you and completed we're going to drain stamina and that is now all we need to do for this it is perfectly done so like i say this is going to be draining one stamina every 0.1 seconds for while the player is sprinting so that is going to work perfectly for me and my stamina is actually if we compile this we can set a default value which for me is going to be 100 and the max stamina i will also set to be 100 like so so if we compile and save that that is now working perfectly so again one stamina every 0.1 seconds so now what we want to do is we want to do the regenerating stamina the gaining stamina code so what we're going to do is select all of this and hit control c underneath this we're gonna hit ctrl v as the code is very very similar so we just copy and paste it and then remove and change anything that we need to first of all we need to obviously change the name of the custom event so i'm going to name this one to be regen stamina or gain stamina whatever makes sense for you and we'll compile that there then we obviously don't want to be subtracting stamina here so we're going to delete the subtract node drag out stamina and get an add node doing the same thing connecting that into the value leaving all the values in the clamp the same we now obviously need to give this a value to actually add so i'm going to give this a value of five you can obviously set this up to be whatever value you want but for me five is going to be good and again that's the value i used at the start of the video so compile and save that so now what else do we need to change if the stamina is equal to zero which we don't want that because we're not checking to see if the stamina has fully drained we're now checking to see if the stamina has fully regenerated so this wants to be if it's equal to the maximum stamina which is again the variable we created earlier if it is equal to the maximum stamina what do we want to do we don't want to stop sprinting we just don't want to do anything so if we've regenerated stamina up to the maximum we're just going to stop regenerating stamina we don't need to do anything else if it's false so it's not fully regenerated what we want to do is we don't want to drain the stamina we want to instead regen stamina so this is going to be a loop once again and the delay i'm going to set this to be 0.05 instead of 0.1 just because again i want this to be really quick and this also doesn't want to be if we are sprinting because we obviously don't want to regen simona if we're sprinting we want this to be if we're not sprinting so we can hold down alt and left click on true to disconnect that and then connect it into false instead so if we're not sprinting we're going to regen the stamina so we can compile and save that and that is now our code for draining and regenerating stamina completely set up however these custom events aren't going to be cooled yet and so this is why i said earlier we're going to revisit the functions we created so if we go back to our start sprinting function at the end of this what we can do is call function drain stamina and on the stop sprinting at the end of it we can call function regen stamina perfectly like so and if we go to the event graph you'll notice again when we drain stamina and once we reach zero we're going to stop sprinting which will then automatically start regenerating the stamina like so so that works perfectly for us and what i'm going to do is actually set my stamina by default to be 50 and on event begin play so we get event begin play here out of this i'm going to then just do regen stamina just to show it working straight away so we'll compile and save that now the only thing left to do now is just put this on the screen so the player can actually have a visual and see how much stamina they've got so we can minimize this hit control space to open the content browser just go to content and i'm going to right click go to user interface create a widget blueprint creating user widget and i'm going to name this one stamina widget like so opening it up straight away in here what we want to do is go to panel and add a canvas panel then we just want to simply add in a progress bar like so now i'm going to do this pretty rough as it doesn't matter too much for me the visuals of it i'm just showing the functionality so i'm then going to put this in the bottom left-hand corner making sure to anchor it where i have it as well so it stays in this position like so then with that still selected what we can do is change all these about if we want to so we can change the color and we can change the bar field type so if i set the percent up you can see we can change it to be from fill from center top to bottom left to right right to left anything which you want very easy to customize and i'm also going to leave the stamina as blue so back up to the percent here we're going to hit bind and create binding then before we do anything in here we're going to go to the event graph delete event tick and event pre-construct and just go to event construct off of this we're going to cast to our character which for me is bp third person character but views could be third first or whatever you named it and the object is going to be get player character like so connecting this into here what we want to do now is right click as bp third person character promote it to a variable naming this one character reference like so so it's a reference to our character and we can compile save and go back to our binding for the progress bar drag out the return load hold control get your character reference and out of this we now want to just get our stamina because we also want to put the stamina into our progress bar so we'll get stamina like so and we're also going to get the max stamina and i'll show you why we're doing that in a second let me just get it here like so what we also want to do for out of these is convert these to be floats instead of integers as a progress bar goes between a value of 0 and 1 not zero and a hundred so we need these to be floats so we can actually get the decimals between zero and one so to do that you can very easily drag out them and get a two float integer like so getting that for both of them like this then out of the top one we're going to get a divide connecting the bottom one into there as well so we're essentially dividing the stamina by the max stamina connecting that into the return value like so so this now is nice and simple so if we divide the stamina by the max stamina we get the progress bar value so for example if we have 50 stamina out of our maximum of 100 we're doing 50 divided by 100 which is 0.5 so that's halfway through our zero and one so it will be fifty percent on the progress bar and obviously fifty is fifty percent of a hundred so that works perfectly for us so we can compile and save that now the final step is to actually put this on screen so go back to our character blueprint go to event begin play out of this we're going to create widget with the class being our stamina widget and the return value is just going to be add to viewport very simply like so now we can compile save minimize this and hit play to test it out and you'll notice by default when we begin the game it should be regening stamina perfectly like so we walk about we've got our normal walking speed we can hold left shift we're now sprinting and it is draining our stamina once it reaches zero stamina it should stop sprinting and start regening stamina by default as you can see it does that perfectly it's regen the stamina and we can now also make sure we can sprint perfectly again like so afterwards once it's regen the stamina and if we stop it will start rejoining perfectly again like so so i think that'll be it for this video if we've done everything we want to do like i just went over there what we've done is we set it up so we can sprint and stop sprinting and while we're sprinting it's going to drain the stamina and when we stop sprinting it will regen the stamina and it will cap between zero and our max stamina which we want and also while we are sprinting if we reach our minimum stamina of zero it will stop it sprinting and start reaching stamina automatically as you can see here so thanks so much for watching this video i hope you enjoyed it and hope you found it helpful and if you did make sure to like subscribe down below so thanks much for watching and i'll see you in the next one [Music]
Info
Channel: Matt Aspland
Views: 69,166
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, set, walk, max, speed, run, running, sprint, sprinting, max walk speed, velocity, anims, animations, system, mechanic, ue5 stamina, ue5 sprinting, ue5 running, get, is, drain, regen, regenerate, decrease, increase, fill, up, down, go, when, while, not, until, value, integer, easy, quick, learn, to, start, action, energy
Id: seGQy-GBfhY
Channel Id: undefined
Length: 17min 0sec (1020 seconds)
Published: Thu May 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.