Hi there!
Below is a tutorial i have put together for implementing a pretty basic stamina system into your project.
The tutorial is broken down over 15 steps which should hopefully help with any troubleshooting problems and generally i find i p to work in small chunks of work.
This tutorial is intended to be completed after my previously submitted Sprinting Tutorial however the sprint function has changed quite drastically between the two tutorials so this is not necessary but is recommended if you are maybe unfamiliar with setting up a project and working in the event graph.
Throughout the tutorial, i have hyperlinked various bits and pieces which might provide some clarity as to the functions of some of the nodes used. Might be useful for anybody keen on understanding what the nodes functions are, however the UE4 documentation is a bit scarce and does not provide a great amount of clarity.
When time permits, i will be looking to extend this tutorial to include setting up a UMG progress bar to show our stamina on screen.
Hopefully my explanations are clear and easy to work through, however leave a reply if you there is anything that could have been improved on.
As a sort of TLDR, there is a link at below the tutorial to a BlueprintUE.com pastebin which will allow you to navigate your way around the blueprint via your browser.
Thanks folks
**Step 1.0 - **Set up the below variables in your ThirdPersonCharacter blueprint class and set our Max Walk Speed to 175 in the character movement component.
[table=“width: 450, class: grid, align: center”]
**Variable Name**
**Variable Type**
**Default Value/State**
**IsMoving?**
**Float**
0.0
**CanSprint?**
**Boolean**
False
**Stamina**
**Float**
100.0
**StaminaRegen**
**Float**
-0.5
**IsSprinting?**
**Boolean**
False
**StaminaDecay**
**Float**
0.2
Step 1.1 - To start things off, the first node we must add to our event graph is a ‘Set IsMoving?’ node which we will create by pulling a wire from our ‘Add Movement Input’ node (specifically the ‘Add Movement Input’ node which is wired from the ‘InputAxis MoveForward’ node). From here, we must then create a ‘Branch’ node which will be used to check if our IsMoving? variable is >0.1. Then from the true pin of the branch, we will insert a ‘Set CanSprint?’ node and set the CanSprint? variable to true. Similarly, from the false pin of the branch, we will insert a ‘Set CanSprint?’ node and set the CanSprint? variable to false.
http://i.imgur.com/1wDowle.jpg
Step 1.2 - Now coming back to the ‘Set IsMoving?’ node we created in 1.1, we will pull a wire from the float value input pin and create a ‘Dot Product’ node. Now we must insert a ‘Get Actor Forward Vector’ node and wire this in to our first pin of the dot product node. For our second pin, we can create a ‘Get Velocity’ node and from the return value create a ‘Normalize’ node which we will wire into our second pin of the dot product node.
http://i.imgur.com/xiW2jRg.jpg
Step 1.3 - From here we will now define what is to happen if the IsMoving? variable is not >0.1 by pulling a wire from the set CanSprint? node and creating a ‘Set Stamina’ node. Now we must drag in a reference to both our Stamina and Stamina Regen variables and subtract these. We can clamp our Stamina variable by using a ‘Clamp (float)’ node which restricts our Stamina from going above 100 and below 0.
http://i.imgur.com/nArO9lf.jpg
Step 1.4 - Now we must insert a ‘Branch’ node which will check if our Stamina is <30, then from the true pin of the branch we will insert a ‘Set CanSprint?’ node of which the state will be false. And from the false pin of the branch we will again insert a ‘Set CanSprint?’ node of which the state will be true.
http://i.imgur.com/KpM2PUJ.jpg
Step 1.5 - Create a ‘Branch’ node which will check if the IsSprinting variable is true or false.
http://i.imgur.com/X1rdODp.jpg
Step 1.6 - From the false pin on the branch node we created in 1.5, insert a ‘Set Stamina’ node. Now we can replicate the same set of nodes we made in 1.3.
http://i.imgur.com/yyHFuC9.jpg
Step 1.7 - From the true pin of the branch we created in 1.5, we will create another branch which will check if our Stamina is >=1.
http://i.imgur.com/VkmI0sa.jpg
Step 1.8 - From the true pin of the branch we created in 1.7, we will insert yet another branch to check if we are sprinting.
http://i.imgur.com/xl5FD4P.jpg
Step 1.9 - From the true pin of the branch created in 1.8 insert a similar set of nodes we input in 1.3 and 1.6, however now we want to minus the Stamina Decay from the Stamina.
http://i.imgur.com/2TWykb5.jpg
Step 1.10 - Moving back to the branch node we created in 1.7, we will pull a wire from the false pin and input a branch and we insert a ‘InRange (float)’ node which will we will use to check if our Stamina is in the range of 0.0 and 0.999999999999. From the true pin of the branch, we will insert a ‘Set IsSprinting?’ node with the state being false. We will identify where the ‘Set IsSprinting?’ node is connected to in 1.15.
http://i.imgur.com/4jtaHmO.jpg
Step 1.11 - We now must move back to the nodes we created in 1.4 and from the Set CanSprint? pin we will insert a branch which will check if the IsSprinting? variable is true or false.
http://i.imgur.com/QDdRMvD.jpg
Step 1.12 - From the false pin of the branch created in 1.11 replicate the same node set made in 1.3 and 1.6.
http://i.imgur.com/qo2t2Eq.jpg
Step 1.13 - From the rue pin of the branch created in 1.11 insert another branch which will check if the CanSprint? variable is true or false.
http://i.imgur.com/Dk8OIFn.jpg
Step 1.14 - From the true pin of the branch created in 1.13, replicate the same node set made in 1.9
http://i.imgur.com/NevNMLJ.jpg
Step 1.15 - Now we must amend our original sprint function created in the first tutorial. First we must insert a branch from the ‘InputAction Sprint’ Pressed pin which will identify if CanSprint? is true or false. From the true pin of this branch we will insert a ‘Set IsSprinting?’ node. Then from the ‘Set IsSprinting’ node, we will pull a wire and create a new timeline.
The timeline will have one float track named Sprint Increase. Set the track length to one second and create two keys on the track, the first at 0.0 seconds and the second at 1.0 seconds, now our Sprint Increase float track has a linear increase the duration of the track.
http://i.imgur.com/LNban1m.jpg
Now if we come out of the timeline editor window and from the ‘Set IsSprinting?’ node (true) we will wire this into the Play From Start pin of our timeline. Now from the released pin of the ‘InputAction Sprint’ node we will insert a ‘Set IsSprinting?’ node and set its state to false. From this node, wire this in to the reverse pin of our timeline.
Now drag a reference to our Character Movement component into the event graph from the components pane and from this node insert a ‘Set Max Walk Speed’ node, wiring in the update pin from our timeline to this node. Now wire our Sprint Increase float track from the timeline to our ‘Set Max Walk Speed’ node.
Finally, from the ‘Set IsSprinting?’ node created in 1.10, wire this node in to the reverse pin of the timeline.
http://i.imgur.com/WZu7Blk.jpg
Hopefully the tutorial helped, follow this BlueprintUE link to access the blueprint in your web browser.
Thanks again.