How do I make a one Directional sprint?

So I have set up a sprint feature where when I hold Left Shift, the speed changes to 600. However, I want to only make a one Directional sprint so when you are only holding the forward key (w)m you move forward, kind of like Call of Duty and Fortnite. I also want it to be able to sprint also when going left and forward. For example holding down w and a. Can someone send an example blueprint? Help is appreciated. Thanks!

You gotta stop duplicating this post my man.

Hi there,

One way you can do this is by checking if the controlled pawn/character’s current actor rotation is within a certain angle of the control rotation (aka, the rotation of where you’re looking) while the sprint button is held down. If the character is rotated too far compared to the control rotation then you can set the movement speed back to the non-sprinting speed, but when it is within the desired angle then you set it to the sprinting speed. You’d probably want to do this on tick so that it’s very responsive to your inputs. Also, you’d probably want to only use the Yaw value of each rotation when doing the comparison.

So it’d be something like this in blueprints:

For reference, left shift is my “sprint” key in this case. Sprint Speed is 600.0, Default Speed is 400.0, and Sprint Angle is 45.0 degrees. The sprint angle is the most important variable in this case as it is what determines when you are sprinting “forward” or not.

Also, if you’d like to have a smoother interpolation between the default and sprint speeds you can add the following code to the function.

Please note that 1) I’m using the “Delta Seconds” pin from the Event Tick function for the “Delta Time” of the FInterpTo functions, 2) that Interp Speed is a new variable I created that helps determine how slowly/quickly the interpolation occurs (I went with a value of 10.0 by default), and 3) this won’t smoothly speed up or slow down your character unless the sprint key is down so you may need to play around with it to get the desired outcome you want.

Hopefully this helps you achieve what you’re going for, or at the very least gets you thinking about how you can achieve it. With this setup though you should also be able to set up other “zones” where the speeds are different (maybe making the character move even slower than default while running backward?).

On a side note, a potentially better way to do this in C++ would be to create a custom character movement component and override the GetMaxSpeed function to include this calculation since it’s already being executed every tick. But don’t worry about that if you don’t know or have access to C++.

1 Like

Easier way is to just set a bool (e.g. “bForwardPressed”) true when the w key is pressed (can be done with event W key pressed). When sprint button is held, only set sprint true if bForwardPressed is true. If you need an example blueprint just comment below. Im more than happy to make one

Hello! Thank you for your help. If you can send me an example that would be nice!

Thanks for the help! I will try that later today.

Hello! I out in your exact same code and it doesn’t seem to be working. I entered in the correct values. When I hold left shift, it sprints, then when I let go, it keeps sprinting. What should I do?

Sorry for the wait, I’ll do that now. (I didn’t have access to my laptop for 4 days)

Here it is. If it works, please accept the answer. If not, leave a comment to tell me what went wrong :slight_smile:

Ok Thanks! It works now :slight_smile: