Below is a beginner friendly (i hope) tutorial i have put together on how to implement a very basic sprinting system into your project.
The tutorial is broken down into sections which should allow for easy following and brief explanations are provided for those who may be new to the Unreal Editor.
I am aware that there are various tutorials/blueprint examples on the UE4 wiki, UE forums and Youtube which explains how to implement this much faster, however after this tutorial i shall also be submitting my stamina decay/regen tutorial which will build on the information provided in this tutorial.
I hope the tutorial helps those who are new to the editor, if you get confused at any point or something is not working as intended then let me know.
Additionally i have included an image at the end of the tutorial to act as a sort of TLDR, which provides a snapshot of the Blueprint setup if you wish to skip reading the less fun parts :).
If you think the tutorial is long winded or would be better presented an alternative way, please do let me know.
Hope this helps somebody!
1.0 - Project Creation
**1.1 - **The first thing we want to do when we open Unreal Editor 4 is create a new project using the Third Person Game example with starter content. I have named my project ‘Sprint Tutorial’, however for sake of implementation your project does not necessarily need to have the same name.
2.0 - Input Actions
**2.1 - **The first thing we must do once our project has been created is set up our new input actions that are going to allow our character to sprint. To do this we must go into the project settings, which can be accessed along the top of the editor screen.
**2.2 - **Within the Input section of our project settings, we are going to create a new action mapping bound to the Left Shift key. This action map will be named Sprint.
3.0 - Sprint Blueprint
**3.1 - **Now that we have our action mapping set, we must now create two variables within our ThirdPersonCharacter blueprint which will set the walk speed and run speed of our character.
**3.1.1 - **With the ThirdPersonCharacter event graph open, nagivate to the variables of the blueprint which are typically on the left side of the editor. Click the + to create two new variable called Walk Speed and Run Speed. These variables will as standard be Boolean variables (true or false), however for our purposes we must ensure that they are Float variables (decimal numbers). To change our variables from Boolean to Float we can click on them and change their Variable Type to Float in the details panel at the right side of the editor.
Now we have our variables created, we must now set their default values, to allow us to do that we must first compile our blueprint. With that done, if we click on either of the variables we can set their default values in the details pane (typically on the right side of the editor). In this instance, I have set my walk speed to 175.00 and run speed to 600.00.
**3.2 - **With our variables now created and set, we must now work in the ThirdPersonCharacter blueprint to create our sprinting action.
**3.2.1 - **The first thing we must do right click and create our InputAction Sprint node. We can do this by right clicking anywhere in our event graph and typing ‘Input Action Sprint’.
**3.2.2 - **Now we must get a reference to our character movement as this allows us to manipulate the default movement values of our character. To get this reference, either drag and drop it from the components section of the editor (typically top left of the screen) or right click in the event graph and type ‘get character movement’ and add the Get Character Movemenent node.
**3.2.2 **Now that we have reference to the character movement, we can now manipulate the max walk speed of our character. We can do this by adding a Set Max Walk Speed node to our event graph, this is done by dragging a wire from our character movement reference and adding the Set Max Walk Speed node.
**3.2.3 - **We must now join our InputAction Sprint Pressed pin to our Set Max walk Speed node pin. Also we must set the value of our Max Walk Speed pin when our InputAction Sprint is pressed, we do this by referencing the ‘Run Speed’ variable we created in part 3.1.1. This can be done either by dragging and dropping the node from our variable sections on the left side of the editor or by right clicking and typing ‘get run speed’ and adding the Run Speed node. Now drag a wire from the Run Speed node and connect it to the Max Walk Speed pin of our Set Max Walk Speed node.
**3.2.4 - **Now if we compiled and tested our project, our characters max walk speed changes from 175.00 to 600.00 when our InputAction Sprint is pressed, but nothing happens when we release our InputAction. The desired effect is for our character to return back to a walking speed when released and to do this we insert a very similar set of nodes that we used for the InputAction Sprint Pressed pin. We can copy the Set Max Walk Speed node and link this to the InputAction Sprint Released pin. Then we must set a value for the max walk speed when released just like in 3.2.3. We will do this by creating a reference to our Walk Speed variable we created earlier and linking this to our Max Walk Speed pin. Multiple wires can be taken from the character movement reference, so again link this to our target of the Set Max Walk Speed Node.
**3.2.5 - **The last thing we must add within our blueprint is an Event BeginPlay node which we will use to set the Max Walk Speed of our character when our game is first started. These nodes will be set up similar to the previous set of nodes, where we are setting the Max Walk Speed by referencing our Walk Speed variable.
4.0 - Compile and Test
**4.1 - **Now we can compile our blueprint and test our project to ensure our sprint is working as intended. When in play, we can press and hold the shift key to increase our characters movement speed and when released our character returns back to a walking speed.
Set up your Event Graph as per the below image and ensure your variables have been set as per section 3.0.