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!
asked Oct 30 '18 at 03:36 AM in Blueprint Scripting
Here it is. If it works, please accept the answer. If not, leave a comment to tell me what went wrong :)
answered Nov 03 '18 at 08:22 AM
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++.
answered Oct 30 '18 at 05:33 AM
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
Follow this question
Once you sign in you will be able to subscribe for any updates here