Adding a Sprint ability

Now that we have a basic understanding of how the movement input nodes take the controller input and apply it to our in-game character, we'll extend that functionality with a Sprint ability. We'll set up a new series of nodes within the FirstPersonCharacter Blueprint. They will look like this:

First, we will need to create a trigger that will activate our Sprint Action. Recall that we previously mapped the Sprint Action to the Left Shift key. To access that input trigger, right-click on the empty grid space to the left of the other movement Functions and search for Sprint. Select the InputAction Sprint Event to place the node.

Now, we want to modify the movement speed of the player. If you try adding a new node and searching for speed with context-sensitive search turned on, you will find only those nodes that are meant for retrieving the maximum speed and checking whether it is being exceeded. Neither of these will help you set the maximum speed of the player. To accomplish this, we need to retrieve a value from the Character Movement Component attached to the FirstPersonCharacter Actor. Look at the Components panel of the Editor and select CharacterMovement (Inherited). The Details panel should change to show a long series of variables, as seen in the following screenshot:

In this list of variables, you can find Max Walk Speed in the Walking category. This is the value that determines the maximum speed at which the player can move, and it should be the target of our Sprint Function. However, changing the value in the Details panel from the default of 600 would modify the player's movement speed consistently, regardless of whether Left Shift was being pressed or not. Instead, we want to pull this value out of the Character Movement Component and into our Blueprint's Event Graph. To do so, click on the Component in the Components panel and drag it onto Event Graph, near our Left Shift trigger. This will produce a Character Movement node, as seen in this screenshot:

Click and drag the output pin from the Character Movement node to empty grid space, ensure that you have Context Sensitive checked, and type walk speed. This time, the Set Max Walk Speed Action will appear. Select it to connect the Character Movement node to the new node, setting the maximum walk speed value. Connect the Pressed output execution pin from the InputAction Sprint trigger to the input execution pin of the Set Max Walk Speed node, in order to enable you to press Left Shift to modify the maximum movement speed. Finally, change the Max Walk Speed value within the node from 0.0 to 2200, in order to provide a nice boost of speed over the default of 600.

We also need to ensure that the player slows down again once the Shift key is released. To do so, drag the output pin from the Character Movement node again, and then search for and select another Set Max Walk Speed node to place it on Event Graph. This time, connect the Released output execution pin of the InputAction Sprint node to the input execution pin of the new node. Then, change the Max Walk Speed value from 0.0 to the default of 600. To keep up with our good commenting practice, click and make a selection box around all four of our nodes, right-click on one of the selected nodes, and select Create Comment from Selection to label the group of nodes as Sprint.

Now, compile, save, and press Play to test your work. You should notice a significant boost in speed as long as you press down the Left Shift key.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset